Cookies are not being sent with file_get_contents or CURL requests

By | August 11, 2018

I am trying to send a file_get_contents request to a URL with POST data and a cookie set.

My code is like that:

$postdata = http_build_query(
    array(
        'search' => 'test',
        'token' => '0'
    )
);

// Create a stream
$opts = array(
      'http'=> array(
            'method'=>"POST",
            'content' => $postdata,
            'header' => "Content-Type: application/x-www-form-urlencoded\n"."Cookie: session_hash=123456789"
      )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('https://example.com', false, $context);

echo $file;

This is posting the data as I can see, but the Cookie is not being sent during the request...

I also tried to use the same request but with CURL and I have the same problem with that.

Any help would be appreciated.

Thanks!