Error cURL with PHP

0

I have a PHP that makes cURL requests to a URL and sometimes when I receive data I receive the following:

307 Temporary Redirect
307 Temporary Redirect
nginx

The PHP with cURL is as follows:

$fp=fopen("../datosAPI/bitcoin.json", "w");
// Se crea un manejador CURL
$ch=curl_init();
// Se establece la URL y algunas opciones
curl_setopt($ch, CURLOPT_URL, "https://api.cryptonator.com/api/ticker/btc-usd");
//determina si descargamos las cabeceras del servidor [0-No mostramos|1-mostramos]
curl_setopt($ch, CURLOPT_HEADER, 0);
//determina si mostramos el resultado en el nevagador [0-mostramos|1-NO mostramos]
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//determina donde guardar el fichero
curl_setopt($ch, CURLOPT_FILE, $fp);
// Se obtiene la URL indicada
curl_exec($ch);
// Se cierra el recurso CURL y se liberan los recursos del sistema
curl_close($ch);
//se cierra el manejador de ficheros
fclose($fp);
    
asked by charli 04.05.2018 в 17:51
source

1 answer

0

The error indicates that there is a temporary redirect, the "new url" where the request will come in a header location . For cURL to retry with the new location, it must be specified before the curl_exec :

 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

to know what was the redirection put before the curl_close

 $redirectURL = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL );

Depending on how eloquent this new url is you can get to know what condition causes the redirect to occur (it requires login, many connections, forward credentials, etc ...) is usually something that can be solved with a new request or following the new url that the server gives you.

reference: link

Note the difference between a 302, 303 and 307 is that in the latter you have to repeat / follow the request using the same HTTP method (if it was GET to continue with GET, if it was a POST follow with POST)

    
answered by 04.05.2018 в 20:06