Is it possible to receive the cookies set by the remote server when doing a file_get_contents request?
I need php to do a http request and store the cookies and make a second request.
Is it possible to receive the cookies set by the remote server when doing a file_get_contents request?
I need php to do a http request and store the cookies and make a second request.
you should use cURL
for that purpose, cURL
implement a feature called the cookie jar which permit to save cookies in a file and reuse them for subsequent request(s).
Here come a quick code snipet how to do it:
/* STEP 1. let’s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ("http://somedomain.com/");
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
/* STEP 3. visit cookiepage.php */
$ch = curl_init ("http://somedomain.com/cookiepage.php");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
note: has to be noted you should have the pecl extension (or compiled in PHP) installed or you won't have access to the cURL API.
You can either install and use the PECL extension for HTTP, or make sure your php installation was compiled with the optional curl library.
I believe you co do it pretty easily with the Zend_Http object. Here is the documentation about adding cookies to a request.
To get the cookies from a request (automatically retrieved I believe), just use getCookieJar()
on the Zend_Http object.
That should be easy to implement; however, the php manual has a user comment on how to deal with cookies using the http stream.