Below is my code but it's not showing output. Now I temporarily shown yahoo.com as the curl output but my actual output url is different. Can anyone tell me what's the problem in the code?
<?php
$myurl = "www.yahoo.com";
curl_setopt ($ch, CURLOPT_URL, $myurl);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result= curl_exec ($ch); //execute and get the results
echo $result; //display the reuslt
curl_close($ch);
?>
Try This:
//Add $ch = curl_init();
$ch = curl_init();
$myurl = "www.yahoo.com";
curl_setopt ($ch, CURLOPT_URL, $myurl);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result= curl_exec ($ch); //execute and get the results
echo $result; //display the reuslt
curl_close($ch);
Your are not initializing the curl use this:
<?php
$myurl = "www.yahoo.com";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $myurl);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result= curl_exec ($ch); //execute and get the results
echo $result; //display the reuslt
curl_close($ch);
?>
Related
I'm going crazy with this. I read some other question about it, but never found an answer.
My problem is:
I'm trying to create a PHP Curl request (GET), and the request return false if I set the url from a variable.
Here below my code:
Working code:
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, "www.google.com");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_PORT, 80);
$output = curl_exec ($ch);
echo json_encode($output);
Not working code:
$url = "www.google.com";
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_PORT, 80);
$output = curl_exec ($ch);
echo json_encode($output);
Someone could help me with this?
Thank you.
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();
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
Script is running without implementing for loop, but when I place a for loop it fails.
Working script:
$dob=new DateTime('28-12-83');
$regno='1984';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://domain.com/login.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'op=&sub=3&txtregno='.$regno.'&txtrollno=&txtpass=&txtdob='. $dob->format('d-m-y'));
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
curl_setopt($ch, CURLOPT_URL, 'http://domain.com/results.php');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$fp = fopen('mm.htm', 'w');
curl_setopt ($ch, CURLOPT_FILE, $fp);
$store = curl_exec ($ch);
fclose($fp);
curl_close ($ch);
Above script is running good and generate a mm.htm file with output.
Incorrect script after for loop
$dob=new DateTime('27-12-83');
$regno='1984';
for($i=1; $i<=3; $i++)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'domain.com/login.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'op=&sub=3&txtregno='.$regno.'&txtrollno=&txtpass=&txtdob='. $dob->format('d-m-y'));
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
curl_setopt($ch, CURLOPT_URL, 'domain.com/results.php');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$fp = fopen($regno."_".$i.'.htm', 'w');
curl_setopt ($ch, CURLOPT_FILE, $fp);
$store = curl_exec ($ch);
fclose($fp);
$dob->modify('+1 day');
set_time_limit(0);
curl_close ($ch);
}
Above script generates 3 htm files but without any output. It means this script fails somewhere, but I failed to trace the problem.
If you want to store the received contents in the file you specify with CURLOPT_FILE, surely you shouldn't also use CURLOPT_RETURNTRANSFER to ask for it to get returned? Or perhaps I should ask you why you do that?
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);