0

I am sometimes encountering an http error code 302 when using cURL in PHP. For instance with the following code, I get a 302 "Access denied", "You don't have permission to access the requested URL on this server." error.

$url = 'https://www.macys.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0');
$contents = curl_exec($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);
print_r($curl_info);
echo $contents;

I searched and found this thread, which says:

This is because your setup has either safe mode or open_basedir settings in the php.ini.

However, it seems safe mode is removed (I have PHP 8.1), and in my cPanel MultiPHP Ini Editor I couldn't find open_basedir. I did find a setting enable_dl (dynamic loading) which mentioned open_basedir, and enabled that, but still got the 302 error.

What might be the issue here? Why is cURL not following the redirect? Is there something wrong with my code?

Tristan
  • 1,561
  • 3
  • 18
  • 22

0 Answers0