[英]php curl with browser cookies
我有一个像这样的Web服务http://www.sample.com/api/v2/exchanges/Web/stocks/Stock/lastN=200 ,它返回json,主要问题是您应该登录http://sample.com来查看此json,否则会看到403禁止错误,并且此网站使用google authenticate登录,我可以使用带有curl的浏览器cookie来获取此json吗? 这是我找到的代码,但对我没有用
function get_content($url,$ref)
{
$browser = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init();
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // browsers keep this blank.
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $browser);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $ref);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
$html = curl_exec($ch);
curl_close ($ch);
return $html;
}
该网站使用谷歌身份验证登录
然后先登录gmail,gmail将为您提供一个特殊的cookie,该cookie应该允许您获取经过身份验证的json。
,我可以使用带有curl的浏览器cookie来获取此json吗?
对。 您可以通过在浏览器中登录并在js终端中检查document.cookie来获取cookie,但这听起来是非常不方便的解决方案
我找到的代码但是对我没用
该代码根本不会尝试进行身份验证。 有关通过gmail进行Google身份验证的示例,请检查
https://gist.github.com/divinity76/544d7cadd3e88e057ea3504cb8b3bf7e
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.