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
Related
I am trying to login to a page, and then go to a different page, submit a form, and then I get another form on the same link, submit that, and then get another form on the same link, and submit that.
However, from what I see, I login fine ... but after that it keeps showing me the same page (the one after I login).
$username = 'xxxxxxx';
$password = 'yyyyyyy';
$useragent = $_SERVER["HTTP_USER_AGENT"];
$fields = array(
'username='. urlencode($username),
'password=' . urlencode($password),
);
$postdata = implode('&', $fields);
$ch = curl_init();
//Main Login
curl_setopt ($ch, CURLOPT_URL,"https://xxxxxxxxxxxx.com/index.php");
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
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://xxxxxxxxxxx.com/index.php");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
echo $result;
echo "<pre>";
print_r ($info);
echo "</pre>";
print curl_error($ch);
$contractid = "12345";
$fields = array(
'contract_id='. urlencode($contractid),
'submit='. urlencode("Next"),
);
$postdata2 = implode('&', $fields);
curl_setopt ($ch, CURLOPT_URL,"https://xxxxxxxxxxxx.com/index.php?page=Recovery");
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
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://xxxxxxxxxxxxx.com/index.php?page=Recovery");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata2);
curl_setopt ($ch, CURLOPT_POST, 1);
$result_2 = curl_exec($ch);
$info = curl_getinfo($ch);
echo $result;
echo "<pre>";
print_r ($info);
echo "</pre>";
print curl_error($ch);
and twice more for each step, the same way and ending it with curl_close($ch);.
However, like I stated, it still shows only the main page.
After first curl request close curl and initialize again. Otherwise curl will not write anything to cookie file and at next call no cookies are sent!
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.
// after end of first curl call
curl_close($ch);
// before second curl call
$ch = curl_init();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
print_r($result);
$array_json = json_decode($result, true);
I tried to do a curl to a https site, it say the dns cannot be resolve. But that site I can access it by browser
https://example.com [example]
I did test on my own local installment
$ch = curl_init();
$curl_url = 'https://example.com';
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
print_r($result);
It works correct and print result. I suppose you problem is in this string
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
You could past wrong path or load wrong certificate. I comment this line in my example.
I want get data from this website: https://vrl.lta.gov.sg/lta/vrl/action/pubfunc?ID=EnquireTransferFee
This is my code using CURL:
function postPage($url, $pvars, $referer, $timeout){
if(!isset($timeout))
$timeout=30;
$curl = curl_init();
$post = http_build_query($pvars);
if(isset($referer)){
curl_setopt ($curl, CURLOPT_REFERER, $referer);
}
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt ($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/x-www-form-urlencoded"));
$html = curl_exec ($curl);
curl_close ($curl);
return $html;
}
$vars = array(
'vehicleNo' => 'SFT4228H',
'transferDate' => '10042014'
);
$result = postPage('test.php', $vars, 'https://vrl.lta.gov.sg/lta/vrl/action/pubfunc?ID=EnquireTransferFee', '30');
print "Result:".$result; //Show data in my website
=> How to get this data
As you have it here, it appears that you are using curl to request "test.php".
Try switching them around in your function call:
$result = postPage('https://vrl.lta.gov.sg/lta/vrl/action/pubfunc?ID=EnquireTransferFee', $vars, 'test.php', '30');
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;
}
?>
UPDATE: I am now able to to access the main homepage, but i am not logged in.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.xxx.com/?cmd=home');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'emailAddress=jeffanderson#tradermail.info&persist=on&pswd=qweqwe&from=/?cmd=home');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec ($ch);
curl_close ($ch);
echo $content;
curl_exec() returns false if there was a problem:
$content = curl_exec($ch);
if ($content === FALSE) {
die(curl_error($ch));
}
return($content);
You need to do two things. First, add the following two options to CURL:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
That will tell CURL to follow any redirects. Then, change return to echo. Echo will send the data to the browser. Then you are all set!
Add curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);.
after all honorable answer..u put all together in below way
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.xxx.com/?cmd=sb-login&from=/?cmd=home');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'emailAddress=xxx#tradermail.info&persist=on&pswd=xxx&from=/?cmd=home');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_MAXREDIRS, 10);
$content = curl_exec($ch);
if (FALSE===$content {
echo "Follwowing error:".curl_error();
die(curl_errorno($ch));
}
curl_close ($ch);
return($content);