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
Related
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.
I have below curl code to GET.
<?php
function get_content($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_URL, $URL);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo get_content("http://www.domain.com/cool.php");
?>
I have used http headers and cookie looks like below
xidseq:22
xid:b05f251c-8a72-4c2b-a230-e03b9c5c87b7&&BAYIDSAMFE1C013&343
data:dsfsfssdss
I need to send GET request to http://www.domain.com/cool.php with some cookies.
how do i put the cookie in cookie.txt ?? is there any specific format for cookies..or just posting it works ?
If script above doesn't work try:
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt'); /* make sure you provide FULL PATH to cookie files*/
Check file permissions and ownership on dirname(__FILE__) . '/cookie.txt' . Make sure file is writable.
To put the cookie in cookie.txt you need to visit certain web page that contains them - only server side cookies you can fetch with cURL, javascript cookies is not reachable vie cURL, at least not directly.
If you want to create cookies manually for your GET request, read about Netscape cookies format or - best way, save some 'real website' cookies via CURLOPT_COOKIEJAR to see and understand format.
I've got a cURL PHP script which works. It gets my schedule from my school site. Though there is one strange thing: On my webhost it creates the cookie.txt and on my localhost it doesn't.
Why doesn't it create a cookie on my localhost? Any suggestions? Something with relative paths and wampserver?
And the questions that follows the latter:
Is there any (speed) advantage of already being logged in on the school site (storing the cookie and thus saving an cURL request)?
I could for example check after the first cURL request if there is evidence in the response that I am already logged in.
If the answer to the above question is: 'no, this doesn't make the script faster' I've got another question:
Is it than best to specify only the CURLOPT_COOKIEFILE option? With an empty value? So no cookie jar?
I can't give you my login information, though here is the script:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,
'http://www.groenewoud.nl/infoweb/infoweb/index.php');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$tokenSource = curl_exec($curl);
print_r (curl_getinfo($curl));
if (!$tokenSource) echo 'token problem';
// Get the token from within the source codes of infoweb.
preg_match('/name="csrf" value="(.*?)"/', $tokenSource, $token);
$postFields = array(
'user' => $userNum,
'paswoord' => $userPass,
'login' => 'loginform',
'csrf' => $token[1]);
$postData = http_build_query($postFields);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
$tableSource = curl_exec($curl);
print_r( curl_getinfo($curl));
if (!$tableSource) echo 'post problem';
curl_close($curl);
1) /cookie/cookie.txt means you'd need to have your cookie directory in the ROOT directory of your entire server. cookie/cookie.txt (note: NO leading slash) means the cookie directory would be a sub-directory of your script's CURRENT directory. E.g. your script is running in /a/b/c/, then you'd have /a/b/c/cookie/cookie.txt.
2) For speed advantages, there's no change in HTTP speeds - you're still stuck with the same pipes and transfer rates. But having the cookie initially MIGHT save you a few extra hits on the server to simulate the login-sequence, so would effectively be SLIGHTLY faster.
3) As for creating the cookies, that's entirely up to curl's settings. If you don't specify a cookie file or cookie jar, it won't create or look for the cookie file. Check the configuration/compile options between the two servers to see if one specifies some curl defaults that the other doesn't have.
4) str_pos WOULD be faster than a curl request. Think of it as the difference between looking in your fridge for some food versus driving to the grocery store. Fridge is local and therefore faster.
5) curlopt_cookiefile tells curl where to store new cookies. curlopt_cookiejar tells curl where to load cookies from when it first starts up. They CAN be different files, but don't have to be. If you'd like to keep some "clean" baseline cookies, then you use cookiejar = newstuff.txt, and cookiejar=baseline.txt. Once you've got an appropriate cookie environment set up, you reset cookiejar to newstuff.txt for subsequent curl runs.
I'm using cURL to log in, like so:
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_URL,'http://www.website.com/login.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=example&password=example');
Then, once that's run, I'm using this to authenticate further requests:
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
The trouble is, I can't seem to make the code work unless I redo the login portion every time I run it; this seems impractical and I'm certain there's a better way to go about it. Perhaps there's a way to tell cURL how long to keep a cookie active?
Make sure you set CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR before you do any auth work.
Also, make sure the cookie.txt is both readable and writable.
I'm trying to login to megaupload.com using cURL and PHP. What I want to do is login so I have premium access, and then download a file. This the code for my login method:
public function login()
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.megaupload.com/?c=login");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username={$this->username}&password={$this->password}&login=1");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$store = curl_exec($ch);
curl_close ($ch);
}
And this is my index.php:
<?php
include_once("plugins/megaupload.class.php");
$megaupload = new Megaupload("username", "password");
$megaupload->login();
?>
But nothing seems to happen. When I run the script, cookie.txt isn't saved anywhere. I got the POST values from Firebug:
login=1&password=password&redir=1&username=username
This is what's being sent through the form when I log in using their site. And yeah, the username and password is correct.
Thanks for any help!
EDIT: Okay, it seems it is actually logging in as I can access my account page, which I wouldn't be able to unless I'm logged in. But that still doesn't solve where the cookie.txt file is being saved...
You need to make sure the directory where you're trying to store the cookie is world-writable.
Also, you should use an absolute path (setup a separate folder outside of your webroot, if you're able), and ensure proper permissions.
You should also set the CURLOPT_COOKIEFILE option:
url_setopt($ch, CURLOPT_COOKIEFILE, ABS_PATH_TO_COOKIE_TXT);
It might be this error:
http://www.renownedmedia.com/blog/php-curl-cookies-not-saving-on-windows/
It seems CURLOPT_COOKIEJAR option have some problems with relative urls. Try setting an absolute path:
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath("cookie.txt"));