grc
grc

Reputation: 324

Retrieve cookie from browser will work with php curl?

I have a simple php application that get prices from a website, but it is only working if I copy the cookie from the chrome browser request of the page. Can I simply copy/paste this cookie line in the CURLOPT_HTTPHEADER and it will work forever?

Also, It works perfectly when I set a proxy too. I don't know why. Can anyone explain me?

Thanks

Upvotes: 1

Views: 1345

Answers (1)

Rafael Rotelok
Rafael Rotelok

Reputation: 1182

If you have the contents of the cookie ( copied from your browser ), you have a few options if you are using curl with php if the cookie rarely changes and/or if you have a simple script, you can embed the cookie in the script with CURLOPT_COOKIE as in the example.

curl_setopt ($ch, CURLOPT_COOKIE, <COOKIE_STRING_CONTENT>);

It's the easiest way, but the caveat is every time the cookie changes, you need to update script.

If you need something more malleable, easier to update and maintain you can use this 2 directives CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR as bellow,

$cookieFile = "name_of_the_cookie_file_on_the_server"
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookieFile);

What this does is instruct the script to load the cookie file, and stores changes on the same file ( if any ), this way if you ever need to update the contents of the cookie you just need to update de cookie file.

curl have a lot of nice options don't forget to read the documentation at http://php.net/manual/en/function.curl-setopt.php

Upvotes: 2

Related Questions