I am trying to write a Cookie after login to cookies.txt.
My source code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
'loginMail='.$username.'&password='.$password.'&targetUrl=');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$store = curl_exec($ch);
curl_close($ch);
The output of the store variable shows that login was successful. But there is no change in the cookies.txt. Rights are set to 777. After login there is a redirect on the page.
What might be the reason for it?
If you are running the code from your apache, the use the full path for the cookie file instead of the cookies.txt. For example
/var/some-dir/tmp/cookies.txt
Related
I want to auto login on other website. I am using Curl php for this. I have print the output its working fine . it returns the array properly but i need to set cookies with token id. cookies like "user_XXXX" and token in it.
my code is :-
$uname='';
$pwd='FFFFFFFF';
$url_post='https://api.test.com/v3/public/loginGrant?site_address=example.test.com&username=xxxx.com&password=BdXXXX';
$ckfile = "cookies.txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://lab.example.com/example2-crm/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL,$url_post);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "$uname:$pwd");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_COOKIE, $ckfile);
$result = curl_exec($ch);
$arr=json_decode($result);
$arr= (array) $arr;
print_r($arr);
how can i set cookies in it and login the website.
I have a simple PHP script that make a login to desired webpage. It is working OK on my localhost (wamp server), but when I run it on Heroku I get response like this (link: image link:)
Script is simple, just like that:
<?php
$username = "username";
$password = "password";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'user[username]'=>$username, 'user[password]'=>$password)));
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$content = curl_exec($ch);
echo $content;
?>
I don't know what am I doing wrong. Is it something wrong with Heroku or is just luck that code is working OK on my wamp server? I also tried running this script on other free hosting pages but they don't support CURLOPT_FOLLOWLOCATION parameter and so script is not working.
The text you're seeing at the top of the page is the header that you requested with :
curl_setopt ($ch, CURLOPT_HEADER, true);
Also, CURLOPT_POST is implicit when you use CURLOPT_POSTFIELDS, not need for it.
You're code should look something like:
<?php
$username = "username";
$password = "password";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.yoursite.com");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'user[username]'=>$username, 'user[password]'=>$password)));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$content = curl_exec($ch);
echo $content;
Looks like that page that I was trying to post to has country restricted IP.
I'm facing with error which is not resolved from 3 days . let me know where i'm going wrong . Like wise i need to login into portal and redirect it to next webpage .
url="http://gis.lntecc.com/BWSSBLnT/login.aspx?ReturnUrl=%2fbwssblnt%2fScada.aspx%3ffield1%3dKathriguppe%2cSW2DM0402%2c235505H073%2c450&field1=Kathriguppe,SW2DM0402,235505H073,450";
$useragent="xyz";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'abc:123');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'Login1_Username=abc&Login1_Password=123');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
echo $store;
curl_close ($ch);
The post field names on the website are not the same in your code.
The proper names are Login1$UserName and Login1$Password, you need to look at the name attribute, not the ID.
That being the case, your code should be:
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'Login1$UserName=abc&Login1$Password=123');
hey guys easy way to login into the webpages using php script is just removing url and following with the thing
header("Location: http://gis.lntecc.com/bwssblnt/Scada.aspx? field1=Kathriguppe%2cSW2DM0402%2c235505H073%2c450")
i have a file cron.php in it is a CURL function.
The CURL function writes content from a diferent website in my database.
If i call it in the browser it works fine, but in the cron jobs it write the wronge content from the site into my database.
Is there a problem between crontab and CURL in php files?
UPDATE
the curl function:
function CurlPost($sURL,$sMessage = "")
{
$cookie_file_path = "cookies/cookies.txt";
print_r($sMessage);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $sURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sMessage);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$sResult = curl_exec($ch);
curl_close($ch);
return $sResult;
}
You must have to provide full/absolute path for file name when running it from cron. You must be using cookie with your curl, and that is lacking of absolute path. Hence the cookie is not working and you are getting wrong content.
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
So cookie.txt should be /home/tmp/cookie.txt
In the below code i am trying to post the username and password but not able to create the cookie using post, if i do the same with get method it creates the cookie.
$BASEURL="http://localhost/openx/www/api/json/index.php/main/authenticate/";
$POSTFIELDS='username="'.$username.'"&password="'.$password.'"';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
curl_setopt($ch, CURLOPT_URL, $BASEURL);
$returned = curl_exec($ch);
The curl_getinfo($ch) returns 200 OK http code, can anybody please advice what is wrong with the above code.
Thanks in advance.
The cookiejar is only saved when you close the curl handle using curl_close($ch).
From the manual:
CURLOPT_COOKIEFILE
The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file. If the name is an empty string, no cookies are loaded, but cookie handling is still enabled.
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.
http://www.php.net/manual/en/function.curl-setopt.php
I have changed my code to below and now working for me.
$ckfile = tempnam ("/tmp/", "CURLCOOKIE");
$BASEURL='http://localhost/openx/www/api/json/index.php/main/authenticate/';
$POSTFIELDS='username='.$username.'&password='.$password.'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,'http://localhost/openx/www/api/json/index.php/main/authenticate/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
ob_start(); // prevent any output
$result=curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
$result = curl_exec($ch);
curl_close($ch);