gpt4 book ai didi

php - cURL 返回请启用 cookie?

转载 作者:行者123 更新时间:2023-12-03 23:16:36 28 4
gpt4 key购买 nike

似乎我需要一些帮助来设置我的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com