What is the difference between using fopen and cURL for handling redirects in PHP?
When handling redirects in PHP, using cURL is generally preferred over fopen as cURL provides more control and flexibility when dealing with HTTP requests and responses. cURL allows you to easily follow redirects, set custom headers, and handle various response types. On the other hand, fopen may not handle redirects automatically and may require additional code to achieve the same functionality.
// Using cURL to handle redirects in PHP
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;