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 ?
Asked
Active
Viewed 6,990 times
5
-
possible duplicate of [PHP - Send cookie with file_get_contents](http://stackoverflow.com/questions/3431160/php-send-cookie-with-file-get-contents) – Gordon Jan 14 '11 at 11:14
-
possiblr duplicate of [Copying cookies from first response to next request](http://stackoverflow.com/questions/4212442/copying-cookies-from-first-response-to-next-request) – Gordon Jan 14 '11 at 11:16
-
Gordon - one more such example and you may convince me to close this question ;) – agsamek Jan 14 '11 at 16:07
1 Answers
7
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

SergeS
- 11,533
- 3
- 29
- 35
-
Could you provide some sourcecode here that passes cookies and parametrs of the currenct script and is this library available on a standard PHP hosting? – agsamek Jan 17 '11 at 12:53
-
-
It is not working for ASP.NET session cookies. cURL is not capturing them. – Volatil3 Jan 06 '21 at 14:30
-
@Volatil3 can you check if cookies are really sent (using CURLOPT_HEADER) ? As they may be not sent or they may be HTTPS only for example – SergeS Jan 06 '21 at 15:56