2012-09-13 149 views
3

我想下面的代碼,因爲我不能使用由於重定向PHP的file_get_contents,但是這回什麼:如果您運行捲曲返回空白

echo curl_error($ch); 

右後

function file_get_contents_curl($url) { 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

    $data = curl_exec($ch); 

    curl_close($ch); 

    return $data; 
} 


$url = 'http://www1.macys.com/shop/product/michael-michael-kors-wallet-jet-set-monogram-ziparound-continental?ID=597522&CategoryID=26846&RVI=Splash_5'; 
$html = file_get_contents_curl($url); 
echo "html is ".$html; 
+0

你試過curl_getinfo()和curl_error() )看他們報告什麼? – Robbie

回答

9

快速望着輸出:

curl --max-redirs 3 -L -v $url顯示,302重定向重定向到相同的URL ..而設置的cookie。

設置餅乾罐,似乎 '修復' 它:

curl -c /tmp/cookie-jar.txt --max-redirs 3 -L -i $url

所以..能餅乾(JAR)

編輯:

您可以將行添加到您現有的代碼使其工作:

curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).DIRECTORY_SEPERATOR.'c00kie.txt'); 
+1

非常感謝,現在添加了以下內容,現在都可以(給cookie.txt 777 perms)$ cookie_file =「/home/***/cookiejar_keep/cookie.txt」; curl_setopt($ ch,CURLOPT_COOKIEJAR,$ cookie_file); curl_setopt($ ch,CURLOPT_COOKIEFILE,$ cookie_file); –

0

curl_exec,你會看到這一點:

最大(20)重定向其次

找出重定向的原因並在其中

+0

我跑了同樣的事情。我沒有錯誤。雖然運行'print_r(curl_getinfo($ ch));'顯示一個重定向到同一個URL。 –

2

您可以使用CURLOPT_VERBOSE選項來調試您的請求。

將輸出寫入STDERR或使用CURLOPT_STDERR指定的文件。

$curl_debug_file = __DIR__.DIRECTORY_SEPARATOR.'curl.log'; 
$fh = fopen($curl_debug_file, "w+"); 
curl_setopt($handle, CURLOPT_STDERR, $fh); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
+0

看起來你有一個錯字,它應該是curl_setopt($ ch,CURLOPT_STDERR,$ fh);對? – Mike