简体   繁体   English

使用cURL将cookie传递到网站

[英]Passing a cookie using cURL to a website

Some website I need to make a request to, using cURL in PHP, contains a captcha, which can be deactivated via setting a cookie "downloadcaptcha=1". 我需要使用PHP中的cURL进行请求的某些网站包含一个验证码,可以通过设置cookie“ downloadcaptcha = 1”将其禁用。 Now how do I pass that cookie to the website on a cURL request? 现在,如何通过cURL请求将该Cookie传递到网站? Before, you mark as duplicate, I've already made research and came across to using cookie jar files. 在您将其标记为重复之前,我已经进行了研究,并开始使用Cookie jar文件。 I have never done something like that and couldn't find any newbie-friendly documentation. 我从来没有做过这样的事情,也找不到任何对新手友好的文档。

The JS-equivalent code of setting the cookie, taken from the website's function that disables captchas: 设置cookie的JS等效代码,取自禁用验证码的网站功能:

var exdate=new Date();
var exdays = 1;
var c_name = "downloadcaptcha";
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(1) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value+";path=/";
window.location.reload();

I don't want to necessarily make it expire in 1 day. 我不想让它在1天后过期。 Help is appreciated. 感谢帮助。

Here's the output. 这是输出。 Not a single mention of the cookie 没有提及Cookie

Request Header is missing the Cookie: 请求标头缺少Cookie:

Host: www.emuparadise.me
Accept: */*
Accept-Encoding: deflate, gzip
Referer: https://m.emuparadise.me/

Change the $request to: 将$ request更改为:

$cookie = '[downloadcaptcha=1'

$request = array();
$request[] = "Host: www.emuparadise.me";
$request[] = "Accept: */*";
$request[] = "Accept-Encoding: deflate, gzip";
$request[] = "Referer: https://m.emuparadise.me/";
$request[] = "Cookie: $cookie";




I have tried that already but the website still asks for a captcha. 我已经尝试过了,但是网站仍然要求提供验证码。 Take a look at the JS code I posted. 看一下我发布的JS代码。 It's taken from the website's function to disable captchas 它取自网站功能以禁用验证码

I could help you much better if I had a URL to test with. 如果我有要测试的URL,我可以为您提供更好的帮助。

If there is a redirect you must use curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 如果存在重定向,则必须使用curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

You may have to make the HTTP Request Header look identical to the JS HTTP header. 您可能必须使HTTP请求标头看起来与JS HTTP标头相同。

You need to look at the cURL HTTP request and response and compare it to the JS HTTP request and response. 您需要查看cURL HTTP请求和响应,并将其与JS HTTP请求和响应进行比较。

To see the JS HTTP request and response: 要查看JS HTTP请求和响应:

  • right click the page 右键单击页面
  • Select Inspect (Chrome) or Insect Element (FireFox) 选择检查(Chrome)或昆虫元素(FireFox)
  • Select "Network" tab 选择“网络”标签
  • Go to the page with the JS request or if already there Refresh. 转到包含JS请求的页面,或者如果已有刷新,则进入页面。

Trouble shooting PHP code: 排除PHP代码故障:

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);


$data = curl_exec($ch);
if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
  $info = rawurldecode(var_export(curl_getinfo($ch),true));

  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $responseHeader= substr($data,0,$skip);
  $response = substr($data,$skip);
   var_export($info);
  echo "----------------------------------\n$responseHeader\n--------------------------\n$response \n------------------------------------\n";

}


Original Post 原始帖子

curl_setopt($ch, CURLOPT_COOKIE, $cookie );

Single Cookie 单曲奇

$cookie = 'CookieName=CookieValue' 

Multiple Cookies separated by semi-colon. 多个Cookie以分号分隔。 There is no semi-colon after the last cookie. 最后一个Cookie之后没有分号。

$cookie = 'CookieName=CookieValue; CookieName=CookieValue' 

curl_setopt($ch, CURLOPT_COOKIE, $cookie );


Cookies can also be set using CURLOPT_HTTPHEADER 也可以使用CURLOPT_HTTPHEADER设置Cookie
See last line of the following example. 请参见以下示例的最后一行。

curl_setopt($ch, CURLOPT_HTTPHEADER, $request);

$request = array();
$request[] = "Host: www.example.com";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";
$request[] = "Cookie: $cookie";

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

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