How to set session through curl in php - php

I have created a script for load test. In this there are 2 curl request to the remote server. First request is for login, if login success then other curl request to adding a new user. In this the first curl request is done but when i call the second request for adding new user it rejects, because session is not set.
In the first call for login i believe the session is set but how do i use this in my second request.
I have seen some answers saying to use cookie jar but i don't understand how to do that. How to create a cookie like in the below code.
$ckfile = tempnam ("/tmp", 'cookiename'); //Plz explain this line
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);

tempnam- Generate file with random name in '/tmp' directory with 'cookiename' prefix.
$ckfile = tempnam ("/tmp", 'cookiename');
CURLOPT_COOKIEJAR - Option for setting the name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
CURLOPT_COOKIEFILE - Option for setting the name of the file containing the cookie data.
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
Algorithm:
In first request you should use option CURLOPT_COOKIEJAR to store your cookies;
In second request you should use option CURLOPT_COOKIEFILE to use stored cookies from the first request.

Related

PHP CURL Request Failing When Site Requires Cookies Enabled

I am trying to grab the meta data from a news article on the NY Times website, specifically http://www.nytimes.com/2014/06/25/us/politics/thad-cochran-chris-mcdaniel-mississippi-senate-primary.html
Whenever I try however I am getting redirects from the sight because my "browser" does not accept cookies. I have enabled the curl options to save cookies and tried following the accepted answers in a few other StackOverflow questions (here, here, and here) and while the answer worked on those websites it doesn't seem to work on the nytimes site.
My current php curl function looks like this:
function get_extra_meta_tags_curl($url) {
$ckfile = tempnam("/public_html/commentarium/", "cookies.txt");
$ch = curl_init($main_url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
}
The problem appears to be that when I request the URL, nytimes.com checks if the browser accepts cookies. I checks a couple of times before redirecting to the login page with a REFUSE_COOKIE_ERROR. Instead of posting the full redirect list here you can see it on my test page here along with the raw html that the final redirect returns and what my current get_extra_meta_tags_curl function is returning under CURL test
Thanks for any help!
You enable cookies auto-handling in wrong manner. CURLOPT_COOKIEJAR only enables cookies saving (storing), but you need also enable cookies loading and passing them with request (by CURLOPT_COOKIEFILE option). Otherwise cookies auto-handling won't work and you will experienced mentioned "Browser does not accept cookies" problem.
So you have to set both CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE options to the same value ($ckfile) at each CURL request:
...
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
...

Is there way to retain session when curling into a url?

Let's say I have a domain foo.com and it has a page called foo.com/bar.php
In one of the functions in the site, I try accessing foo.com/bar.php via cURL.
However, some stuff in foo.com/bar.php is dependent on the current session.
Hence, I want to retain the current session of the calling method into the cURL method.
Is there a way to do this?
You have to setup some additional curl options:
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
With above settings the cookies will be stored in cookie.txt :)

PHP & cURL: is it possible to get a cookie into variable instead of file without parsing headers?

I have a php proxy server to which email and password are posted on the beginning (login). I then use
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt( $ch, CURLOPT_HEADER, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
to post forward to some API that expects certain parameters. As you can see I save a cookie in a cookie jar file.
I can then use this cookie file to call any other requests to proxy -> API and successfully get the response. Everything works just fine. I use
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookieJar);
to make other requests after user successfully signed in.
The problem is that only one user can login(and call other requests) at the time, because there is only one cookie jar file. I could probably generate unique cookie files on proxy and access them somehow with each new request by each user. But that is a load on the server and definitely not a good idea.
So what I would like to do is to save a cookie that is received into variable instead of a file and then send this to user...
This didn't work for me unfortunately; I can probably manage to write my own regex but I am wondering if there is a possibility to directly save a cookie into variable with curl or do I have to parse headers manually? (I want to be able to feed CURLOPT_COOKIEFILE with cookie in variable rathen than cookie in file)
Lets try this with a single curl handle($ch):
Making my first request:
$url= "http://www.google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, '-'); // <-- see here
$result = curl_exec($ch);
// remember i didn't close the curl yet!
Now make another curl request with the same handle:
$url= "http://www.google.com";
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec($ch);
// if you are done, you can close it.
curl_close($ch);
In this example, I have used the - for the cookiejar. Which means it will not use any file. So during second curl request, it will use the cookiejar of previous call.
One problem: It will print the cookie jar values into std-output.

Login to page with curl in php and then visit link "logged in"

I need to login (make post) with curl in php to a website, then save a cookie with authentication, and use it to visit normal link from same website.
They have only half of text available in free version, but the whole in premium. So if it's possible, I would like to save a cookie for multiple times later.
I know something about cURL and PHP, but I've didn't come up with a solution yet.
Thanks guys!
You need to use the cookiejar parameter to share a text file between both requests.
/* Create a temporary cookie file */
$ckfile = tempnam ("/tmp", "COOKIES");
/* Visit the first page */
$ch = curl_init ("http://somedomain.com/");
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
/* Visit the second page */
$ch = curl_init ("http://somedomain.com/cookiepage.php");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
Example modified from http://coderscult.com/php-curl-cookies-example/
Edit: I should have read the docs for tempnam... It creates a unique filename, so adding an additional user specific prefix is not necessary unless you want to be able to identify the file later. You can also look at tmpfile() which is automatically removed when fclose is called (normally at the end of the php request).

curl - get cookies into file

There are 3 files in one folder:
curl.php:
$ch = curl_init('http://localhost/url.php');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_exec($ch);
url.php:
setcookie('test', 'foo', time()+60*60*24, '/');
cookies.txt: (empty)
When I run curl.php, I expect that cookie will be saved in cookies.txt.
But nothing happens.
What is wrong with this, why it does not working?
Does it have permission to write the file in that directory with the user that it executes as?
If no cookies have been received, there will be no file saved and you should note that the cookies are saved to the file when the handle is closed.
It might be necessary to give the absolute path of the text file where the cookies should be saved to be sure you know where they end up.
According to PHP documentation:
CURLOPT_COOKIEJAR - The name of a file to save all internal cookies to when the handle is closed, e.g. after a call to curl_close.
So you need to call curl_close($ch).
Just define path.
Change curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); to curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . 'cookies.txt'); and should work.
And remember. COOKIEJAR is to save cookies, and COOKIEFILE is to load again to work with them.
Happy cURL ;P

Categories