curl返回302怎么处理?浏览器访问正常。 2C

<?php
set_time_limit(0);
error_reporting(1024);
ob_start();
header("Content-Type:text/html;charset=GBK");
header('Cache-Control:no-cache,must-revalidate');

header('Pragma:no-cache');

ini_set('date.timezone','Asia/Shanghai');

$url = 'http://i.sporttery.cn/odds_calculator/get_odds?i_format=json&poolcode[]=hhad&poolcode[]=had';
$content = getCurlData($url);
var_dump($content);

function getCurlData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Host:info.sporttery.cn",
    "Cache-Control: max-age=0",
    "Accept-Encoding: gzip, deflate",
    "Accept:*/*",
    "Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
    "Connection: keep-alive",
    "Cookie:COLLCK=3282378182; Hm_lvt_860f3361e3ed9c994816101d37900758=1505871680; COLLCK=4081554587; Hm_lpvt_860f3361e3ed9c994816101d37900758=1505889209",
    "Upgrade-Insecure-Requests: 1",
    ));
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.04');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_MAXREDIRS,20);

$cdata = curl_exec($ch);
$info = curl_getinfo($ch);
print_r($info);
/*
if($info['http_code']==302){
    getCurlData($url);
}
*/
curl_close($ch);

return $cdata;

}

ob_end_flush();
exit;
?>

2个回答

302重定向之后获取真实的 url。然后在根据真实的url进行操作

$info = curl_getinfo($ch);

if($info['http_code']==302){
getCurlData($info['url']);
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐