2010-04-02 11 views
6

Sto utilizzando il seguente codice:Curl reindirizzamento, non funziona?

$agent= 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0'; 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_USERAGENT, $agent); 

curl_setopt($ch, CURLOPT_URL, "www.example.com"); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt($ch, CURLOPT_HEADER, 0); 

$output = curl_exec($ch); 

echo $output; 

Ma reindirizza come questo:

http://localhost/aide.do?sht=_aide_cookies_ 

Invece di per l'URL della pagina.

Qualcuno può aiutarmi a risolvere il mio problema, per favore?

+0

Se induci il tuo codice di quattro spazi, sarà più leggibile. –

risposta

13
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

http://docs.php.net/function.curl-setopt dice:

CURLOPT_FOLLOWLOCATION
TRUE per seguire qualsiasi "Location:" intestazione che il server invia come parte dell'intestazione HTTP (notare che questo è ricorsiva, PHP seguirà come molti "Location : "intestazioni da inviare, a meno che non sia impostato CURLOPT_MAXREDIRS).
+1

+1 Mi piace quando trovo esattamente quello che sto cercando su SO con una singola ricerca – Endophage

8

Se tocca a reindirizzamento URL solo allora vedere il codice riportato di seguito, ho documentato per voi in modo da poter usare facilmente & direttamente, hai due opzioni principali CURL URL di reindirizzamento di controllo (CURLOPT_FOLLOWLOCATION/CURLOPT_MAXREDIRS):

// create a new cURL resource 
$ch = curl_init(); 

// The URL to fetch. This can also be set when initializing a session with curl_init(). 
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); 

// The contents of the "User-Agent: " header to be used in a HTTP request. 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0"); 

// TRUE to include the header in the output. 
curl_setopt($ch, CURLOPT_HEADER, false); 

// TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

// TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set). 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

// The maximum amount of HTTP redirections to follow. Use this option alongside CURLOPT_FOLLOWLOCATION. 
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 

// grab URL and pass it to the output variable 
$output = curl_exec($ch); 

// close cURL resource, and free up system resources 
curl_close($ch); 

// Print the output from our variable to the browser 
print_r($output); 

Il codice sopra riportato gestisce il problema di reindirizzamento dell'URL, ma non tratta i cookie (l'URL dell'host locale sembra occuparsi di cookie). Se si desidera trattare con i biscotti dalla risorsa CURL, allora potrebbe essere necessario fornire le seguenti opzioni CURL uno sguardo: CURLOPT_COOKIE CURLOPT_COOKIEFILE CURLOPT_COOKIEJAR

Per ulteriori dettagli si prega di seguire il seguente link: http://docs.php.net/function.curl-setopt

Problemi correlati