2010-04-02 19 views
6

मैं निम्नलिखित कोड का उपयोग कर रहा:कर्ल रीडायरेक्ट, काम नहीं कर रहा है?

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

के बजाय यूआरएल पृष्ठ पर।

क्या कोई मेरी समस्या को हल करने में मेरी सहायता कर सकता है, कृपया?

+0

हैं: CURLOPT_COOKIE CURLOPT_COOKIEFILE CURLOPT_COOKIEJAR

अधिक जानकारी के लिए नीचे दिए गए लिंक का पालन करें आप अपने कोड को चार रिक्त स्थान से इंडेंट करते हैं, यह अधिक पठनीय होगा। –

उत्तर

13
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

http://docs.php.net/function.curl-setopt का कहना है:

CURLOPT_FOLLOWLOCATION
किसी भी "स्थान:" का पालन करने के सही हैडर सर्वर HTTP शीर्ष लेख के भाग के रूप भेजता है (ध्यान दें कि यह पुनरावर्ती है, पीएचपी के रूप में कई "स्थान का पालन करेंगे : "हेडर जो इसे भेजा जाता है, जब तक कि CURLOPT_MAXREDIRS सेट न हो)।
+1

+1 मुझे यह पसंद है जब मुझे लगता है कि मैं एक ही खोज के साथ एसओ पर क्या खोज रहा हूं – 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); 

उपरोक्त कोड यूआरएल पुनर्निर्देशन समस्या को संभालता है, लेकिन यह कुकीज़ से निपटता नहीं है (आपका स्थानीयहोस्ट यूआरएल कुकीज़ से निपट रहा है)। आप cURL संसाधन से कुकीज़ के साथ सौदा करना चाहते हैं, तो आप निम्न cURL विकल्प एक रूप देने के लिए हो सकता है: http://docs.php.net/function.curl-setopt

संबंधित मुद्दे