curl返回302,浏览器访问正常,求助!

kandk522 2017-09-20 03:53:57
<?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;
?>
...全文
694 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongmei85 2017-09-20
  • 打赏
  • 举报
回复

set_time_limit(0);
error_reporting(1024);
ob_start();
header("Content-Type:text/html;charset=utf-8");
//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){
    $cookie = tempnam("/tmp", "cookie");
   //先获取 cookie
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_exec($ch);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,TRUE);

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

    return $cdata;
}

ob_end_flush();
exit;
xuzuning 2017-09-20
  • 打赏
  • 举报
回复
没有捕获和回传 cookie

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧