Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sojkadavid's avatar

cURL CookieJar file

Hi everyone,

I am trying to build an app that is going to use cURL for working with another website, but that website requires logging in and uses CSRF protection. For that reason, I need to work with cookies that require CookieJar. How does it work and where the file is actually stored when I use code like this?

$ch = curl_init( 'somedomain.com/login' );

$options = [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => true,
    CURLOPT_COOKIEJAR => '/temp/cookies.txt',   // Where is this?
    CURLOPT_COOKIEFILE => '/temp/cookies.txt',  // Where is this?
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query([
        'email' => $accEmail,
        'password' => $accPass,
        '__csrf__' => $csrf_token   // Just assume that I have a valid CSRF Token
    ]),
    CURLOPT_FOLLOWLOCATION => true
];

curl_setopt_array( $ch , $options );

$response = curl_exec( $ch );
$response_code = curl_getinfo( $ch , CURLINFO_RESPONSE_CODE );  // Usually 403


/* Testing the CookieJar */
$cookieJar = fopen( '/temp/cookies.txt' , 'r' );
echo fread( $cookieJar , 100000000 );   // Laravel err: fopen(/temp/shoptet_cookies.txt): failed to open stream: No such file or directory

I haven't found how to handle cookies from cURL in Laravel anywhere. Any ideas on how to work with that properly? Thank you :)

0 likes
0 replies

Please or to participate in this conversation.