php - cURL 返回请启用 cookie?

标签 php curl cookies

似乎我需要一些帮助来设置我的 cURL,我正在尝试从我的服务器对外部网站进行 API 调用。
让我感到困扰的是,有时此代码有效并返回预期结果,但有时它会返回此消息:

enter image description here

这是我的代码:

$path_cookie = 'cookie.txt';
if (!file_exists(realpath($path_cookie))) touch($path_cookie);

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/api/findusers/".$nick_list."?key=".$voobly_key);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath($path_cookie));
$headers = [
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
        'cookie: __cfduid=d0d14dfd36e0da8e7858b58873b4263181523751395'
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);


$response = curl_exec($ch);
curl_close($ch);
echo "rep : $response";

是我这边的问题还是问题可能来自外部服务器?

我的 cookie.txt 看起来像这样:
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_.example.com  TRUE    /   FALSE   1555287395  __cfduid    d0d14dfd36e0da8e7858b58873b4263181523751395

最佳答案

curl 内置了一个完整的 cookie“引擎”。
如果您激活它,您可以让 curl 像浏览器一样接收和发送 cookie。

Command line options:

-b, --cookie

tell curl a file to read cookies from and start the cookie engine, or if it isn't a file it will pass on the given string. -b name=var works and so does -b cookiefile.

-j, --junk-session-cookies

when used in combination with -b, it will skip all "session cookies" on load so as to appear to start a new cookie session.

-c, --cookie-jar

tell curl to start the cookie engine and write cookies to the given file after the request(s)



引用:https://curl.haxx.se/docs/http-cookies.html

关于php - cURL 返回请启用 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49837132/

相关文章:

c# - 如何从 AuthorizeAttribute 自定义类的 HttpActionContext 获取 cookie?

php - 如何按自定义附加关系对模型进行排序

php - 使用三元条件的两个 if 语句

php - ReCaptcha 总是要求额外验证?

javascript - 为任何文件类型 JS 创建下载链接

php - 验证 curl 是否使用 TLS

curl - cURL NTLM代理授权

CURLOPT_FOLLOWLOCATION 无法激活

cookies - 对于存储 sessionId,Web 存储是否比 cookie 更好?

session - 我需要使用 JSON Web Token token 的 session 存储吗?为什么不只使用 cookie?