如何从网站上获取cookie?

时间:2018-04-13 21:58:38

标签: php curl cookies

我有一个网站登录,因此我的浏览器上保存了Cookie,我想获取这些Cookie。

我试过CURL

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

//Save Cookies To cookie.txt .
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");

//Use Cookie Saved In cookie.txt .
curl_setopt($curl, CURLOPT_COOKIEFILE,"cookie.txt");

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0');
curl_setopt($ch, CURLOPT_HEADER  ,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$content = curl_exec($ch);
curl_close($ch);

但它没有用。

在cookie.txt中有如下文字:

#HttpOnly_.website.com  TRUE    /   TRUE    1681336501  gt_ab   rn:ODUy
#HttpOnly_.website.com  TRUE    /   TRUE    1681336501  gt_p    id:Y2YxNDM3YjUtYjdlNC00YTc2LWE4ZjctYjFiOThkMWQ5NGZi

有没有办法让它工作我得到了饼干?

1 个答案:

答案 0 :(得分:0)

该行:

curl_setopt($ ch,CURLOPT_HEADER,1);

使curl_exec函数同时获取响应的标题和正文。

此链接可能有所帮助:

how to get the cookies from a php curl into a variable

祝你好运