Phpcookie jar不保存cookie

问题描述 投票:0回答:1
function 3rd_party(){
    $cookie_path = __DIR__ . '/cookies.txt';
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'xyz.com');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
    $headers = array();
    $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7';
    $headers[] = 'Accept-Language: en-IN,en-GB;q=0.9,en-US;q=0.8,en;q=0.7';
    $headers[] = 'Connection: keep-alive';
    $headers[] = 'Sec-Fetch-Dest: document';
    $headers[] = 'Sec-Fetch-Mode: navigate';
    $headers[] = 'Sec-Fetch-Site: none';
    $headers[] = 'Upgrade-Insecure-Requests: 1';
    $headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36';
    $headers[] = 'Sec-Ch-Ua: \"Not-A.Brand\";v=\"99\", \"Chromium\";v=\"124\"';
    $headers[] = 'Sec-Ch-Ua-Mobile: ?0';
    $headers[] = 'Sec-Ch-Ua-Platform: \"Linux\"';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);
    echo(file_get_contents($cookie_path));
}

it说:

warning:file_get_contents(/storage/emulation/0/htdocs/cookies.txt): 无法打开流:没有此类文件或目录 /storage/mulation/0/htdocs/k.php在第31行

从第二请求开始返回数据,但以前的数据就像我第二次运行时返回从第一个请求接收的cookie
问题是,即使我关闭了curl

,它也不会保存cookie

解决方案是什么

当我在功能关闭后检查cookie时,它起作用,但从内部检查函数检查时无法正常工作。

trone,如果不存在cookie.txt文件,则将创建它,并且应防止错误

函数third_party(){ $ cookie_path =

dir
php curl php-curl
1个回答
0
投票

// Check if the cookie file exists, if not create it if (!file_exists($cookie_path)) { file_put_contents($cookie_path, ''); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'xyz.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate'); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path); $headers = array(); $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7'; $headers[] = 'Accept-Language: en-IN,en-GB;q=0.9,en-US;q=0.8,en;q=0.7'; $headers[] = 'Connection: keep-alive'; $headers[] = 'Sec-Fetch-Dest: document'; $headers[] = 'Sec-Fetch-Mode: navigate'; $headers[] = 'Sec-Fetch-Site: none'; $headers[] = 'Upgrade-Insecure-Requests: 1'; $headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'; $headers[] = 'Sec-Ch-Ua: \"Not-A.Brand\";v=\"99\", \"Chromium\";v=\"124\"'; $headers[] = 'Sec-Ch-Ua-Mobile: ?0'; $headers[] = 'Sec-Ch-Ua-Platform: \"Linux\"'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch); // After ensuring the cookie file exists, read it echo file_get_contents($cookie_path);

}

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.