2012-09-13 150 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