Gracie williams
Gracie williams

Reputation: 1145

Use cookie saved by curl in stream_get_contents

I wanted to send POST request , Actually curl is 100ms slower than stream_get_contents, so i wanted to use the latter.

I have a cookie.txt saved by curl , how do i use that to my stream_get_contents function below.

function poster($url, $data, $optional_headers = null)
{
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
return $response;
}

My cookie.txt stored by curl looks like below

secure.domain.com   FALSE   /   FALSE   0   InterSecure AyDzUp5AEKz0ErJYeJF2221lIA$$
#HttpOnly_secure.domain.com FALSE   /   TRUE    0   ASP.NET_SessionId   eqyrgo545czouimlnqc223f0qyi

I tried something like

 $header = array(
    'Referer: https://secure.domain.com/IDirectTrading/customer/login.aspx',
    'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.20 Safari/537.36',
    'Cookie :   InterSecure = AyDzUp5AEKz0ErJYeJF2221lIA$$;ASP.NET_SessionId = eqyrgo545czouimlnqc223f0qyi'
 );
$CE = poster("https://secure.domain.com/Handler.ashx",$str,$header);

But cookie doesnt seems to work , Is there any way to parse cookie text file into array and use that into header ?

Upvotes: 1

Views: 221

Answers (1)

miken32
miken32

Reputation: 42696

According to RFC 6265:

Serialize the cookie-list into a cookie-string by processing each cookie in the cookie-list in order:

  1. Output the cookie's name, the %x3D ("=") character, and the cookie's value.

  2. If there is an unprocessed cookie in the cookie-list, output the characters %x3B and %x20 ("; ").

In other words, no spaces around the equal sign, and each cookie should be separated by a semicolon and a space:

$header = [
    'Cookie: InterSecure=AyDzUp5AEKz0ErJYeJF2221lIA$$; ASP.NET_SessionId=eqyrgo545czouimlnqc223f0qyi',
];

Upvotes: 2

Related Questions