Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP How to perform an http request passing cookies and save result to a string

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 ?

like image 727
agsamek Avatar asked Jan 14 '11 11:01

agsamek


1 Answers

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

like image 173
SergeS Avatar answered Oct 03 '22 00:10

SergeS