简体   繁体   English

PHP curl与浏览器cookie

[英]php curl with browser cookies

i have a webservice like this http://www.sample.com/api/v2/exchanges/Web/stocks/Stock/lastN=200 that return json the main problem is you should login to http://sample.com to see this json otherwise you see 403 Forbidden error and this website use google authenticate for login, can i use browser cookie with curl for get this json? 我有一个像这样的Web服务http://www.sample.com/api/v2/exchanges/Web/stocks/Stock/lastN=200 ,它返回json,主要问题是您应该登录http://sample.com来查看此json,否则会看到403禁止错误,并且此网站使用google authenticate登录,我可以使用带有curl的浏览器cookie来获取此json吗? this is the code i found but it didnt work for me 这是我找到的代码,但对我没有用

    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;
} 

this website use google authenticate for login 该网站使用谷歌身份验证登录

then login on gmail first, gmail will give you a special cookie which should allow you to fetch the json as authenticated. 然后先登录gmail,gmail将为您提供一个特殊的cookie,该cookie应该允许您获取经过身份验证的json。

, can i use browser cookie with curl for get this json? ,我可以使用带有curl的浏览器cookie来获取此json吗?

yup. 对。 you can get the cookie by logging in on your browser and check document.cookie in a js terminal, but sounds like a very inconvenient solution 您可以通过在浏览器中登录并在js终端中检查document.cookie来获取cookie,但这听起来是非常不方便的解决方案

code i found but it didnt work for me 我找到的代码但是对我没用

that code does not attempt to authenticate at all. 该代码根本不会尝试进行身份验证。 for an example of google-authentication via gmail, check 有关通过gmail进行Google身份验证的示例,请检查

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM