2010-04-02 1 views
6

Ich verwende den folgenden Code:Curl Redirect ,, funktioniert nicht?

$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; 

Aber es leitet diese mögen:

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

Statt auf die URL-Seite.

Kann mir bitte jemand helfen, mein Problem zu lösen?

+0

Wenn Wenn Sie Ihren Code um vier Leerzeichen einrücken, wird er lesbarer. –

Antwort

13
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

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

CURLOPT_FOLLOWLOCATION
TRUE jede "Location:" folgen Header, den der Server als Teil des HTTP-Header sendet (beachten Sie diese rekursiv ist, wird PHP als viele „Location folgen : "Header, die gesendet werden, außer CURLOPT_MAXREDIRS ist gesetzt."
+1

+1 Ich liebe es, wenn ich mit einer einzigen Suche genau das finde, wonach ich auf SO suche – Endophage

8

Wenn es nach URL-Umleitung ist nur dann sehen Sie den folgenden Code, habe ich es für Sie dokumentiert, so dass Sie es & leicht direkt verwenden können, Sie haben zwei Haupt cURL Optionen Steuer URL-Umleitung (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); 

Der obige Code übernimmt die URL-Umleitung Problem, aber es befasst sich nicht mit Cookies (localhost URL scheint mit Cookies zu tun haben). Wenn Sie mit Cookies von der cURL Ressource behandeln wollen, dann können Sie die folgenden cURL Optionen einen Blick geben müssen: CURLOPT_COOKIE CURLOPT_COOKIEFILE CURLOPT_COOKIEJAR

Für weitere Informationen folgen Sie bitte den folgenden Link: http://docs.php.net/function.curl-setopt