I would like to perform an http request and pass all cookies received by the current script (in particular session identifying cookies) to this request. Then I would like to save the result in a string for further manipulation. What is the best way to do this in PHP ?
cURL ? - it is simple and supprot cookies .
Edit 19.1 - Here is example
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt'); $output = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch);
CURLOPT_COOKIEJAR is file where cURL put cookies sent from server and CURLOPT_COOKIEFILE is file with cookies for sending by cURL ( setting it to same one will make it cookies file ).
Another option is manually read cookies from result ( set CURLOPT_HEADER to '1' - it will put result header into $output ) and send cookies via CURLOPT_COOKIE ( set it to list of cookies in format 'foo=bar;bar=foo;' )
Note - libcurl must be enabled in php.ini
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With