简体   繁体   中英

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". Now how do I pass that cookie to the website on a cURL request? Before, you mark as duplicate, I've already made research and came across to using cookie jar files. 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:

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. Help is appreciated.

Here's the output. Not a single mention of the cookie

Request Header is missing the Cookie:

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

Change the $request to:

$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. 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.

If there is a redirect you must use curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

You may have to make the HTTP Request Header look identical to the JS HTTP header.

You need to look at the cURL HTTP request and response and compare it to the JS HTTP request and response.

To see the JS HTTP request and response:

  • right click the page
  • Select Inspect (Chrome) or Insect Element (FireFox)
  • Select "Network" tab
  • Go to the page with the JS request or if already there Refresh.

Trouble shooting PHP code:

$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. There is no semi-colon after the last cookie.

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

curl_setopt($ch, CURLOPT_COOKIE, $cookie );


Cookies can also be set using CURLOPT_HTTPHEADER
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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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