curl 302 found
在使用curl 抓取网站内容时 ,可能会遇到302 found,即HTTP中的30X(redirection,重定向 )
301 redirect: 301 代表永久性转移(Permanently Moved)。
302 redirect: 302 代表暂时性转移(Temporarily Moved )
此时如果不设置自动跳转倒新url的话,可能会出现问题。
解决办法:
$ch = curl_init($url);注意 CURLOPT_FOLLOWLOCATION,1); //是否抓取跳转后的页面
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1",
"Cache-Control: no-cache",
"Pragma: no-cache"
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); //是否抓取跳转后的页面
$response = curl_exec($ch);