【问题标题】:php curl with browser cookies带有浏览器cookie的php curl
【发布时间】:2019-11-23 15:21:43
【问题描述】:

我有这样的网络服务 http://www.sample.com/api/v2/exchanges/Web/stocks/Stock/lastN=200 返回 json 主要问题是你应该登录到http://sample.com 来查看这个 json 否则你会看到 403 Forbidden 错误 这个网站使用谷歌身份验证登录,我可以使用带有 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;
} 

【问题讨论】:

    标签: php curl cookies


    【解决方案1】:

    本网站使用谷歌身份验证登录

    然后先登录 gmail,gmail 会给你一个特殊的 cookie,它应该允许你获取经过身份验证的 json。

    ,我可以使用带有 curl 的浏览器 cookie 来获取这个 json 吗?

    是的。你可以通过在浏览器上登录并在js终端中查看document.cookie来获取cookie,但听起来是一个非常不方便的解决方案

    我找到了代码,但它对我不起作用

    该代码根本不尝试进行身份验证。有关通过 gmail 进行 google-authentication 的示例,请查看

    https://gist.github.com/divinity76/544d7cadd3e88e057ea3504cb8b3bf7e

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      • 2015-04-18
      • 1970-01-01
      • 2012-02-02
      相关资源
      最近更新 更多