2010-04-02 35 views
6

Tôi đang sử dụng đoạn mã sau:Chuyển hướng curl ,, không hoạt động?

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

Nhưng nó chuyển hướng như thế này:

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

Thay vì để các trang URL.

Bất kỳ ai cũng có thể giúp tôi giải quyết được sự cố của mình không?

+0

Nếu bạn thụt lề mã của bạn bằng bốn dấu cách, nó sẽ dễ đọc hơn. –

Trả lời

13
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

http://docs.php.net/function.curl-setopt nói:

CURLOPT_FOLLOWLOCATION
TRUE để làm theo bất kỳ "Location:" tiêu đề mà máy chủ gửi như là một phần của tiêu đề HTTP (lưu ý đây là đệ quy, PHP sẽ làm theo như nhiều "Location : "tiêu đề được gửi, trừ khi CURLOPT_MAXREDIRS được đặt).
+1

+1 Tôi thích nó khi tôi tìm thấy chính xác những gì tôi đang tìm kiếm trên SO với một tìm kiếm – Endophage

8

Nếu chỉ chuyển hướng URL sau đó thấy mã sau, tôi đã ghi tài liệu đó cho bạn để bạn có thể dễ dàng sử dụng nó trực tiếp, bạn có hai tùy chọn chính cURL kiểm soát chuyển hướng 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); 

Mã trên xử lý vấn đề chuyển hướng URL nhưng không xử lý cookie (URL cục bộ của bạn dường như đang xử lý cookie). Nếu bạn muốn để đối phó với các tập tin cookie từ các nguồn cURL, sau đó bạn có thể phải từ bỏ các tùy chọn cURL sau một cái nhìn: CURLOPT_COOKIE CURLOPT_COOKIEFILE CURLOPT_COOKIEJAR

Để biết thêm chi tiết vui lòng theo đường dẫn sau: http://docs.php.net/function.curl-setopt

Các vấn đề liên quan