What are common methods for retrieving data from a URL in PHP?

When retrieving data from a URL in PHP, common methods include using functions like file_get_contents() or cURL. These functions allow you to fetch the contents of a URL and store it in a variable for further processing. Using file_get_contents() is simpler and suitable for basic requests, while cURL provides more flexibility and control over the HTTP request.

// Using file_get_contents()
$url = 'https://www.example.com/data.json';
$data = file_get_contents($url);

// Using cURL
$url = 'https://www.example.com/data.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);