Hooman Ahmadi
Hooman Ahmadi

Reputation: 1397

PHP CURL By Passing a Cookie Key/Value Pair

I was wondering if someone knew the equivalent of doing (from terminal):

curl --cookie "session_id=12345" http://www.example.com

Using CURL in php. I would prefer to do it without using a cookies.txt file by just doing the php curl calls by passing a cookie key/value pair. Please let me know if this makes sense, otherwise I can clarify further. I'm using this to connect to an API that requires sending a session variable via a cookie.

MORE CLARIFICATION:

The spec specifies this...

"The first thing that has to be done is to login. The response has a session id in it. This should be stored and used for subsequent calls. This should be added as a cookie, session_id, for further calls into the API."

Upvotes: 0

Views: 1527

Answers (2)

John C
John C

Reputation: 8415

You want CURLOPT_COOKIE as specified in the curl_setops page.

$ch = curl_init('http://www.example.com'); 
curl_setopt($ch, CURLOPT_COOKIE, 'session_id=12345');
curl_exec($ch);
curl_close($ch); 

For multiple cookies, separate with a semicolon and a space:

curl_setopt($ch, CURLOPT_COOKIE, 'session_id=12345; fruit=apple');

Upvotes: 2

jeremy
jeremy

Reputation: 10057

You may be looking for the following flags:

  • CURLOPT_COOKIESESSION

And:

  • CURLOPT_COOKIE
  • CURLOPT_COOKIEFILE
  • CURLOPT_COOKIEJAR

Upvotes: 1

Related Questions