2010-04-02 5 views
6

Я использую следующий код:Curl redirect, не работает?

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

Но перенаправляет нравится:

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

Вместо на страницу URL.

Может ли кто-нибудь помочь мне решить мою проблему, пожалуйста?

+0

Если вы отступываете свой код на четыре пробела, это будет более читаемым. –

ответ

13
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

http://docs.php.net/function.curl-setopt говорит:

CURLOPT_FOLLOWLOCATION
ИСТИНА следовать любому "Location:" заголовок, который сервер отправляет как часть заголовка HTTP (обратите внимание, что это рекурсивное, PHP будет следовать, как много «Расположению : "заголовки, которые отправлены, если не установлен CURLOPT_MAXREDIRS).
+1

+1 Мне нравится, когда я нахожу именно то, что я ищу на SO с одним поиском – Endophage

8

Если это до URL перенаправления только тогда смотрите следующий код, я документально это для вас, так что вы можете использовать его легко & непосредственно, у Вас есть два основных варианта Curl управления URL перенаправления (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); 

Вышеупомянутый код обрабатывает проблему перенаправления URL-адресов, но он не касается файлов cookie (ваш URL-адрес локального хоста, похоже, имеет дело с куки-файлами). Если вы хотите иметь дело с печеньем из локонов ресурса, то вы можете дать следующее Curl опционов вслушайтесь CURLOPT_COOKIE CURLOPT_COOKIEFILE CURLOPT_COOKIEJAR

Для получения более подробной информации, пожалуйста, следуйте по следующей ссылке: http://docs.php.net/function.curl-setopt