I am trying to pass my userid and password to the site www.licindia.in using curl, but I am getting trouble with cookies perhaps, I am unable to continue my session and the site response 302 error, the document has been moved temporarily, now I am getting no response with this code :-
<?php
$username="myusername";
$password="password";
$url="http://onlinelic.in/LICEPS/Login/webLogin.do";
//echo "praveenpuglai";
$postdata = "portlet_5_6{actionForm.userName}=".$username."&portlet_5_6{actionForm.password}=".$password;
$cookie = "JSESSIONID";
ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0");
curl_setopt($ch,CURLOPT_COOKIESESSION,true);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, $url);
//var_dump($ch);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
// $result = curl_exec ($ch);
//echo $result;
/*if($result != null)
{
header('Location: http://onlinelic.in/LICEPS/appmanager/Customer/CustomerHome');
} */
// curl_close($ch);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if($info['http_code'] == 301 || $info['http_code'] == 302) {
preg_match_all('|Location: (.*)\n|U', $response, $results);
$location = implode(';', $results[1]);
header("Location: $location");
exit;
} else {
$response = substr_replace($response, '', 0, strpos($response, '<', 0));
echo $response;
}
?>
You have specified that you're not willing to follow any redirects with this statement:
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
Setting the value higher or not specifying it will make cURL follow redirects.
<?php
$username="XXXXXX";
$password="YYYYYY";
$url="url";
$capt="ZZZZZ";
$postdata = "?{actionForm_userName}=".$username."&{actionForm_password}=".$password."&{actionForm_qreply}=".$capt;
$cookie = "JSESSIONID";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0");
curl_setopt($ch,CURLOPT_COOKIESESSION,true);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
//echo $cookie;
//echo $postdata;
echo curl_error($ch);
$result = curl_exec ($ch);
print($result);
curl_close($ch);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if($info['http_code'] == 301 || $info['http_code'] == 302) {
preg_match_all('|Location: (.*)\n|U', $response, $results);
$location = implode(';', $results[1]);
header("Location: $location");
exit;
} else {
$response = substr_replace($response, '', 0, strpos($response, '<', 0));
echo $response;
}
?>
Related
Hey am I missing a curl_opt or something? This code was working yesterday and now it doesnt work. It is suppose to return a json string but instead returns empty string.
<?php
session_start();
$UCID=$_POST['UCID'];
$Pass=$_POST['Pass'];
$credentials=array("Username"=>$UCID, "Password"=>$Pass);
$post_field_string = http_build_query($credentials, '', '&');
$url1="https://web.njit.edu/~vm276/connect.php";
//$url1="https://www.njit.edu/cp/login.php";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_REFERER, $url1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post_field_string);
curl_setopt ($ch, CURLOPT_POST, true);
$result = curl_exec($ch);
curl_close($ch);
$json=json_decode($result);
echo $json->result;
?>
You can get information about errors by adding to your code:
$result= curl_exec($ch);
$error = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if(200 == $httpCode) {
//...
}
else {
echo "httpCode: ".$httpCode."\n";
var_dump($error);
exit();
}
http://php.net/manual/en/function.curl-error.php
http://php.net/manual/en/function.curl-getinfo.php
I am trying to check login details via php curl for https://www.mercadolivre.com/jms/mlb/lgz/login?platform_id=mp&go=https://www.mercadopago.com/mlb/accountSummary.
Here is my code
$username="****#outlook.com";
$password="******";
$callbackerror= "https://www.mercadolivre.com/jms/mlb/lgz/login";
$go = "https://www.mercadopago.com/mlb/accountSummary";
$platform_id = "mp";
$site_id = "mlb";
$loginType = "DEFAULT";
$dps = "554738efe4b0aed09e93f37d";
$parent_url = "";
$arfRedirUrl = "";
$confirm = "Entrar";
$url="https://www.mercadolivre.com/jms/mlb/lgz/login/authenticate";
$postdata = "user_id=$username&password=$password&callback_error=$callbackerror&go=$go&platform_id=$platform_id&site_id=$site_id&loginType=$loginType&dps=$dps&parent_url=$parent_url&arfRedirUrl=$arfRedirUrl&confirm=$confirm";
//$postdata = "user_id=".$username."&password=".$password."&dps=".$dps;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
echo $result = curl_exec ($ch);
curl_close($ch);
It returns empty. What I am doing wrong?
Thanks
I have the following code to login to a remote website.
After executing the script i get the following error from the website : Technical Error
This is the result that i get from executing curl.
$username = $_POST['search']; $password = $_POST['pasword']; $ch = curl_init(); $postdata="&search=".$username."&password=".$password; curl_setopt ($ch, CURLOPT_URL,"https://payment.schibsted.no/login?client_id=5087dc1b421c7a0b79000000&response_type=code&redirect_uri=https%3A%2F%2Fwww.finn.no%2Ffinn%2FloginCallback%3FredirectKey%3D977170356717"); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); curl_setopt ($ch, CURLOPT_HEADER, true); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); curl_setopt ($ch, CURLOPT_REFERER, "https://payment.schibsted.no/login?client_id=5087dc1b421c7a0b79000000&response_type=code&redirect_uri=https%3A%2F%2Fwww.finn.no%2Ffinn%2FloginCallback%3FredirectKey%3D977170356717"); curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); curl_setopt ($ch, CURLOPT_POST, 1); $result = curl_exec($ch); curl_setopt($ch, CURLOPT_URL, "menu link") ; $result2 = curl_exec($ch) ; echo $result2 ; curl_close($ch);
I cleaned up your code what had no line breaks and made it more understandable to some level.
//Variables
$username = $_POST['search'];
$password = $_POST['pasword'];
$cookie = $username.'.txt';
$postdata = "search=${username}&password=${password}";
$agent = 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)';
//*not sure if all this is needed and I don't know why you set it as the referer*
$url = 'https://payment.schibsted.no/login?client_id=5087dc1b421c7a0b79000000&response_type=code&redirect_uri=https%3A%2F%2Fwww.finn.no%2Ffinn%2FloginCallback%3FredirectKey%3D977170356717';
// ---
//Curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
curl_close($ch);
// ---
echo $result;
//*Not sure what this is doing I found it at the bottom of your code*
//curl_setopt($ch, CURLOPT_URL, "menu link");
//$result2 = curl_exec($ch) ;
//echo $result2;
I don't think all this is necessary, also could you provide a little more information?
My PHP script does the following
It logins to a remote server with login and password using CURL, which works Great. echo $result; shows the logged in page contents.
Secondly what it does is to access a inner page ( which needs a person to be logged in ) and check if the page is remotely changed( updated) or not using CURL.
The error i am getting is "Access denied" in Part 2 of the code below, i think the Cookies is not persisting throughout the session? Is that the problem what changes should i do in the code.
<?
//Part 1
$username="username;
$password="pwd";
$url="http://abc.com/home?q=login&destination=filmmaker%2Fhome";
$cookie="cookie.txt";
$postdata = "name=".$username."&pass=".$password."&form_id=user_login&edit-name=".$username."&remember_me=1";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo $result;
//Part 2
$curl = curl_init("http://abc.com/filmmaker/home");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FILETIME, true);
$result = curl_exec($curl);
if ($result === false) {
die (curl_error($curl));
}
$timestamp = curl_getinfo($curl, CURLINFO_FILETIME);
if ($timestamp != -1) { //otherwise unknown
echo date("Y-m-d H:i:s", $timestamp); //etc
}
curl_close($ch);
?>
Part 2 needs:
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
This will send the cookies that were saved by the CURLOPT_COOKIEJAR option in Part 1.
How to create error reporting for this script below to make sure its working
and is there anything wrong with it
<?php
$username="username here";
$password="password here";
$url="url here";
$cookie="cookie.txt";
$postdata = $username . $password . $url;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, true);
$result = curl_exec ($ch);
curl_close($ch);
echo $result;
exit;
?>
curl_exec() returns FALSE on failure. Check for it's success before echoing your $result. On failure, call curl_error() to see the most recent error message returned.
$result = curl_exec ($ch);
if ($result) {
// Success.
echo $result;
}
else {
// something went wrong.
echo curl_error($ch);
}
curl_close($ch);
exit();