php file_get_contents()问题

浏览器打开地址:https://mbd.baidu.com/webpage...
正常获得 json数据;

使用php 的 php file_get_contents()函数就打不开,高手帮忙看看怎么写;

$listUrl="https://mbd.baidu.com/webpage?tab=video&num=4&uk=F7MoqdQ9gg-uzOeznsMx7A&type=newhome&action=dynamic&format=jsonp";
echo file_get_contents($listUrl);
阅读 3.4k
1 个回答

楼上的回答是对的,是你没有携带cookie导致的,你在首次去请求的时候,其实是可以获取到cookie的,拿着这个cookie,再进行一次请求就可以了

// 第一次获取cookie
$url = "https://mbd.baidu.com/webpage?tab=video&num=4&uk=F7MoqdQ9gg-uzOeznsMx7A&type=newhome&action=dynamic&format=jsonp";
file_get_contents($url);
$cookie = "";
foreach ($http_response_header as $header) {
    if (preg_match('/^Set-Cookie:\s*([^;]+)/', $header, $matches)) {
        $cookie = $matches[1];
    }
}

// 第二次携带cookie请求
$options = array(
    'http' => array(
        'header' => "Cookie: " . $cookie,
    )
);
$context = stream_context_create($options);
$contents = file_get_contents($url, false, $context);
print_r($contents);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题