$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "youku.com"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); curl_exec($curl);
大神求解,获取百度淘宝的都可以,优酷的为什么获取不到呢?
我这里需要取到优酷的cookie模拟一次请求........大佬们帮帮忙
没有cookie就当然获取不到cookie,你要用浏览器看看cookie是从哪里来的
这是我分别在浏览器和postman上请求的结果,现在用CURL模拟总是失败,麻烦问一下具体要怎么做呢?这是我的代码:
header("Content-type: text/html; charset=utf-8"); $headers = array( 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36', 'Referer' => 'http://static.youku.com/', ); $url = 'http://ups.youku.com/ups/get.json?vid=XMTQ4ODM5Mjk2MA==&ct=10&ccode=0502&client_ip=0.0.0.0&utid=Ga3jEdWulXoCAXZwOs6IYOEY&client_ts=1501211617'; //获取-开始---------- $cookie = tempnam(__DIR__."/tmp", "cookie"); $t = parse_url($url); //先获取 cookie $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://static.youku.com/"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); curl_exec($curl); //再获取数据 $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie); $data = curl_exec($curl); curl_close($curl); //获取-结束---------- print_r(json_decode($data, true));
@小二寒:
用浏览器访问youku.com,找出带set-cookie响应头的url;找出有设置document.cookie的js;或者一步一步调试看哪一步请求开始带cookie
@TCG2008:
我刚刚清空了浏览器cookie,postman(开启interceptor)也可以返回数据,是不是说明和cookie没关系呢?
http://ups.youku.com/ups/get.json?vid=XMTQ4ODM5Mjk2MA==&ct=10&ccode=0502&client_ip=0.0.0.0&utid=Ga3jEdWulXoCAXZwOs6IYOEY&client_ts=1501211617
这条请求用postman可以返回数据,为什么用CURL模拟就始终不行呢 T T 求指导....
在获取cookie的地方记录下日志,然后试试下面的代码
//先获取 cookie $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://static.youku.com/"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar); curl_exec($curl);