I'm using curl to get the contents of a webpage.. The website sets cookies when i visit them using browser..
Can i use the cURL same way and send a request to that specific website with the cookie information...????
Here are some of the option I found useful regarding curl and cookies.
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt' ); //use this cookie file
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.jar'); //if you close the session the cookies will be saved here
curl_setopt($ch, CURLOPT_COOKIE,"cookie_test=yes; domain=.google.com; path=/"); //set the cookie for the current session
Related
I already have a cookie file saved that I want to reference and update. I also want to specify my own additional cookie values via CURLOPT_COOKIE and save those to my existing cookie file as well.
However, I am unable to get this to work.
My code is:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $website); // Define target site
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Return page in string
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_COOKIE, "fruit=apple;");
curl_setopt($ch, CURLOPT_COOKIEJAR, "usercookies/cookie_$user.txt"); // Tell cURL where to write cookies
curl_setopt($ch, CURLOPT_COOKIEFILE, "usercookies/cookie_$user.txt"); // Tell cURL which cookies to send
curl_setopt($ch, CURLOPT_TIMEOUT,15);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
$returnx = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
My saved cookie file does not reflect the changes I made via curl_setopt($ch, CURLOPT_COOKIE, "fruit=apple;");. The cookiefile saved should show "fruit=apple" but it's still showing the old values or the values returned by the cURL request.
Do I need to reference the entire domain name in order to get it to save?
The cookie file looks something like this:
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
.go.com TRUE / FALSE 1754020486 one AE4F4981
.go.com TRUE / FALSE 1468965260 two B9A1
Cookies you add manually using CURLOPT_COOKIE won't get saved to the cookie jar at the end of the request.
The only case in which it would is if the server sent back a Set-Cookie header for the cookie you sent in order to update it.
The reason is because cURL requests have a cookie structure that holds cookies which gets written at the end of the request. Data only gets in this structure by a) being read from the cookie file in the first place or b) Set-Cookie headers in the response headers.
With a little care you can append your own cookie to that file with something like this:
$domain = '.go.com';
$expire = time() + 3600;
$name = 'fruit';
$value = 'apple';
file_put_contents($cookieJar, "\n$domain\tTRUE\t/\tFALSE\t$expire\t$name\t$value", FILE_APPEND);
Does anyone knows how I could get a list of cookies for an external URL using php?
I think this could be done with cURL?
When i'm using cookiejar or using get_headers, I only see one cookie (the PHPSESSID) for example. But when you open chrome console (F12) and go to cookie storage, you see a much bigger list. Also the google analytics cookies for example. I want to be able to display that list of cookies. So also 3rd party cookies..
Is there any way to retrieve those cookies? Maybe store them temporary or something?
If I got you right & you mean getting cookies that a page sets when visiting it using cURL,
You can use CURLOPT_COOKIEJAR option of cURL for auto manage cookies with cURL (curl_setopt):
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$cont = curl_exec ($ch);
curl_close ($ch);
Or search response header for Set-Cookie: line:
how to get the cookies from a php curl into a variable
I am using cURL to display the contents of a page behind a login system. I am able to successfully login and display the first page behind the login system, but any subsequent pages are unable to be displayed.
My understanding of the problem is that cURL follows the headers that are provided after logging in. So, if the order is login.php -> home.php, and I want to go to account.php, I would need another header pointing to that page.
Is that correct? Can I use cURL to display the contents of other pages after logging in?
You probably need to save and transmit cookies. This can be done in PHP cURL like this:
$ch = curl_init();
// set your regular options
curl_setopt($ch, CURLOPT_URL, 'http://somedomain.com/login.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('user'=>'foo', 'pass'=>'bar'));
// set where cookies are saved
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
// set where cookies are retrieved from when sent to the server
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
// execute login
curl_exec($ch);
// do another request
curl_setopt($ch, CURLOPT_URL, 'http://somedomain.com/restricted_page.php');
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_exec($ch);
You need to handle cookies. They're needed for the session authentication.
Get the Cookie header from the first call and send it with all subsequent calls.
You must know how login pages (usually) work. When you log into a site the site will send you a session cookie that you will send again to the website everytime you request a new page. So if you want to emulate the login you must store cookies returned by the login and then include them in every other request to the site.
I have a situation whereby when a page loads, I send some authentication data (in this case the associative array $data) which is verified by a script on another domain. Code below:
$cookie_path = 'cookies.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.mysite.com/verify');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
$result = curl_exec($ch);
the site then sets a session (in this case I am using the codeigniter framework and sessions are set like: $this->session->set_userdata('logged_in', true); )
however when I load the external site in an iframe it does not seem to be able to detect that the session is set and redirects to the login page.
How do I ensure that my session cookie is being sent properly and can be accessed by an iframe?
Your curl script is running server side and storing the cookie for the second site there, but your browser is loading the second site in the client. You can share cookies across domains.
If you control the site you are attempting to create the session on, you may be able to pass the session ID to the PHP script, then generate the iframe URL dynamically, including the session ID as a query string, eg:
http://www.brainbell.com/tutors/php/php_mysql/Encoding_the_session_ID_as_a_GET_variable.html
Edit
To clarify, if you control the script on the second site, you can modify it to provide the SESSIONID of the authenticated session to your CURL script, which your PHP script making the cURL request can then incorporate into the dynamically generated iFrame src URL.
You can set cookies via:
http://php.net/manual/en/function.setcookie.php
However, you can't set cookies for domains outside of your script's domain.
I am tring to read a cookie value which I got after login by sending a POST request.
Then I want to sent that cookie value with another POST request using Curl to another action. But after sending this when I am trying to see all posted header it does display that I have send any cookie value. This value is not available to my posted URL so not able to access the information due to authentication. Please tell me where I have done something wrong:
$URL1 = "http://www.getinf.com/iconf/user?action=buGroup";
$postfields1 = "device=mapp&type=ajax&name1=ra&cc1=91&min1=90name2=imm&cc2=91&min2=97";
// sends a post request
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL,$URL1);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_COOKIE,'JSESSIONID=199FFF6355DEA87F3D72E692E7514AD2');
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch1, CURLOPT_HEADER,true);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $postfields1);
$result = curl_exec($ch1);
print_r(headers_list());// displays all post request data
$ret = ReturnVal($result);
print_r(get_headers($URL1, 1)); //
curl_close ($ch1);
So what is wrong in this code that is preventing JSESSIONID value accessible as a cookie value?
Check the comments (or search "cookie") on this page in the php docs:
Whats not mentioned in the
documentation is that you have to set
CURLOPT_COOKIEJAR to a file for the
CURL handle to actually use cookies,
if it is not set then cookies will not
be parsed.
Try changing CURLOPT_cookie to CURLOPT_COOKIE
CURLOPT_COOKIESESSION is used to start a new Cookie session and ignore all cookies stored.
From PHP.net: Use CURLOPT_COOKIESESSION = TRUE to mark this as a new cookie "session". It will force libcurl to ignore all cookies it is about to load that are "session cookies" from the previous session. By default, libcurl always stores and loads all cookies, independent if they are session cookies or not. Session cookies are cookies without expiry date and they are meant to be alive and existing for this "session" only.
use: curl_setopt($ch, CURLOPT_COOKIESESSION, false);