'How avoid Moved Permanently The document has moved here

I'm in a site and I would call an API that is in another site. So I build a curl

$url = ........
$curl_data = array('name'=>$name);
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_data);

$output = curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);

so when I execute the curl I print the value "$output" and I obtain Moved Permanently The document has moved here. This is wrong because I would call this api I would obtain value and come back to the page when the process started. Anyone can help me?

After a day I resolve add this line before to call the function:

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);



Solution 1:[1]

Check the URL if it is indeed throwing error 301 (move permanently). Use fiddler since it can capture HTTP error codes.

Solution 2:[2]

see this PHP cURL says Moved Permanently when POSTing to a virtual host

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTREDIR, 3);

Solution 3:[3]

This work for me by adding this line

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Solution 4:[4]

I have faced the same issue, You can fix it by changing your url prefix from http to https (if it was http otherwise change it to http)

The problem is in .htaccess

Something like

RewriteRule ............ [R=301,L]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vhortex
Solution 2 Community
Solution 3 Peternak Kode Channel
Solution 4 ZBorkala