0

I need to fetch some data from external site. To do this I need to load a site that creates some cookie and gives a simple math calculation to generate new link. This part is easy:

$cr = curl_init($url);
curl_setopt($cr, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cr, CURLOPT_COOKIEJAR, 'cookie.txt');
$html = curl_exec($cr);
curl_close($cr);

After those calls I have cookies stored in cookie.txt:

# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

domain   FALSE   /       FALSE   0       PHPSESSID       o89t753egbp9pq9084n38eg2m1

Now, the question is: how can I load this cookie to use it in my next call to some other site (in the same domain ofcourse)?

2
  • 1
    can't you use curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); and curl_setopt($cr, CURLOPT_COOKIEJAR, 'cookie.txt'); in both requests? Commented Apr 11, 2014 at 10:15
  • cookiefile was the thing I was missing. Thanks. (please, add this as an answer so I'll be able to accept) Commented Apr 11, 2014 at 10:19

2 Answers 2

2

You can use

curl_setopt($cr, CURLOPT_COOKIEFILE, 'cookie.txt'); 
curl_setopt($cr, CURLOPT_COOKIEJAR, 'cookie.txt'); 

in both requests.

Sign up to request clarification or add additional context in comments.

Comments

0

A cookie is a header, then you can simply use the command setcookie($name,$value), or if you prefer, using the header php call, using Cookie for header name. Remember, you don't have to send output to the client before the setcookie command.

You have to read file and split it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.