2010-04-02 18 views
6

estoy usando el siguiente código:Curl redirect ,, ¿no funciona?

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

Pero vuelve a dirigir a como este:

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

lugar de a la página URL.

¿Alguien me puede ayudar a resolver mi problema, por favor?

+0

si usted sangra su código por cuatro espacios, será más legible. –

Respuesta

13
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

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

CURLOPT_FOLLOWLOCATION
TRUE para seguir cualquier "Ubicación:" encabezado que el servidor envía como parte de la cabecera HTTP (tenga en cuenta que esto es recursivo, PHP seguirá tantos "Localización : "encabezados que se envía, a menos que se establezca CURLOPT_MAXREDIRS).
+1

+1 Me encanta cuando encuentro exactamente lo que estoy buscando en SO con una sola búsqueda – Endophage

8

Si le toca a la redirección de URL sólo entonces ver el siguiente código, he documentado por usted para que pueda utilizarlo fácilmente & directamente, usted tiene dos opciones principales CURL una dirección URL de control (CURLOPT_FOLLOWLOCATION/opción 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); 

El código anterior maneja el problema de redirección de URL, pero no se ocupa de las cookies (su URL de host local parece tratar con cookies). Si desea tratar con cookies del recurso CURL, entonces puede que tenga que dar los siguientes opciones CURL un vistazo: CURLOPT_COOKIE CURLOPT_COOKIEFILE CURLOPT_COOKIEJAR

Para más detalles, por favor siga el siguiente enlace: http://docs.php.net/function.curl-setopt