最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - cookie jar not saving cookies - Stack Overflow

programmeradmin3浏览0评论
function 3rd_party(){
    $cookie_path = __DIR__ . '/cookies.txt';
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'xyz');
    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 says:

Warning: file_get_contents(/storage/emulated/0/htdocs/cookies.txt): Failed to open stream: No such file or directory in /storage/emulated/0/htdocs/k.php on line 31

and from second request it start returning data but previous one like when i run it second time it return cookie that received from 1st request

The problem is that it don't save cookie even i closed curl

What is the solution

When i check cookies after function close it works but not working when check from inside function

function 3rd_party(){
    $cookie_path = __DIR__ . '/cookies.txt';
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'xyz');
    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 says:

Warning: file_get_contents(/storage/emulated/0/htdocs/cookies.txt): Failed to open stream: No such file or directory in /storage/emulated/0/htdocs/k.php on line 31

and from second request it start returning data but previous one like when i run it second time it return cookie that received from 1st request

The problem is that it don't save cookie even i closed curl

What is the solution

When i check cookies after function close it works but not working when check from inside function

Share Improve this question edited Feb 16 at 17:18 DarkBee 15.6k8 gold badges72 silver badges116 bronze badges asked Feb 16 at 12:40 PradeepPradeep 112 bronze badges New contributor Pradeep is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2
  • 2 You are not really giving us a good description of what you are doing, but, unless I am missing something it is quite possible that on your first connection, no cookies are created. Remember when using cURL you are pretending to be a "real boy" i,e a user on a browser. If the first connection to that server created no cookies, then you have to write your code to cope with that possibility – RiggsFolly Commented Feb 16 at 17:02
  • If there are no cookies, the file will not be created, compare: curl.se/libcurl/c/CURLOPT_COOKIEJAR.html – hakre Commented Feb 16 at 20:03
Add a comment  | 

1 Answer 1

Reset to default 0

The cookies.txt file is created if it doesn't already exist, and it should prevent the error

function third_party() {
    $cookie_path = __DIR__ . '/cookies.txt';
    
    // 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');
    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);
}
发布评论

评论列表(0)

  1. 暂无评论