Dar*_*ney 3 php curl file-get-contents
我正在尝试以下代码,因为重定向不能使用php file_get_contents,但这没有返回任何内容:
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;
Run Code Online (Sandbox Code Playgroud)
小智 9
快速查看输出:
curl --max-redirs 3 -L -v $url 显示302重定向重定向到相同的URL ..设置cookie时.
设置cookie jar似乎'修复'它:
curl -c /tmp/cookie-jar.txt --max-redirs 3 -L -i $url
所以..启用cookie(jar)
编辑:
您可以在现有代码中添加一行以使其正常工作:
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).DIRECTORY_SEPERATOR.'c00kie.txt');
Run Code Online (Sandbox Code Playgroud)