简体   繁体   中英

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."

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');

You may be looking for the following flags:

  • CURLOPT_COOKIESESSION

And:

  • CURLOPT_COOKIE
  • CURLOPT_COOKIEFILE
  • CURLOPT_COOKIEJAR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM