我需要在PHP中使用cURL发出请求的某个网站包含验证码,可以通过设置cookie" downloadcaptcha = 1"来取消激活。现在如何在cURL请求中将该cookie传递给网站?之前,你标记为重复,我已经做过研究并且遇到了使用cookie jar文件。我从来没有做过类似的事情,也找不到任何新手友好的文档。
设置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();
我不想让它在1天内过期。 感谢帮助。
答案 0 :(得分:0)
这是输出。没有一次提到cookie
请求标头缺少Cookie:
Host: www.emuparadise.me
Accept: */*
Accept-Encoding: deflate, gzip
Referer: https://m.emuparadise.me/
将$ 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";
我已经尝试过,但网站仍然要求验证码。 看看我发布的JS代码。它取自网站 用于禁用验证码的功能
如果我有一个要测试的网址,我可以帮到你更好。
如果有重定向,则必须使用curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
您可能必须使HTTP请求标头看起来与JS HTTP标头相同。
您需要查看cURL HTTP请求和响应,并将其与JS HTTP请求和响应进行比较。
要查看JS HTTP请求和响应:
$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";
}
原帖
curl_setopt($ch, CURLOPT_COOKIE, $cookie );
单个Cookie
$cookie = 'CookieName=CookieValue'
多个Cookie用分号分隔。最后一个cookie之后没有分号。
$cookie = 'CookieName=CookieValue; CookieName=CookieValue'
curl_setopt($ch, CURLOPT_COOKIE, $cookie );
也可以使用CURLOPT_HTTPHEADER
设置Cookie
请参阅以下示例的最后一行。
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";