I use php curl, it works fine on localhost but no result on server (after 60 sec trying)
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_PROXY, 'x.x.x.x:808');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
echo $output = curl_exec ($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 scrape the contents of the a site with login secured
but unable to do it
The site's login has three options username,password,passcode
here is the code I am using
<?php
// HTTP authentication
$url = "http://aftabcurrency.com/login_script.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$cookie = 'cookies.txt';
$timeout = 30;
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch,CURLOPT_POSTFIELDS,"user_name=user&user_password=pass&passcode=code");
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
you need to do a POST to http://aftabcurrency.com/login_script.php
your curl needs also to accept cookies.
After the authentification the script will redirect you, so you need also to add CURLOPT_FOLLOWACTION.
here is a edited version of your script, I can't test it on http://aftabcurrency.com/ hope it works:
$url = "http://aftabcurrency.com/login_script.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$cookie = 'cookies.txt';
$timeout = 30;
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch,CURLOPT_POSTFIELDS,"user_name=user&user_password=pass&passcode=code");
$result = curl_exec($ch);
/* //OPTIONAL - Redirect to another page after login
$url = "http://aftabcurrency.com/some_other_page";
curl_setopt ($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
*/ //end OPTIONAL
curl_close($ch);
echo $result;
You need to POST your username/password/passcode to that page. What you are trying to do right now is http authentication.
So instead of this
curl_setopt($ch, CURLOPT_USERPWD, "demo:demopass:demopasscode");
you need this
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "user_name=xxxxx&user_password=xxxxxx&passcode=xxxxx");
I'm new to cURL and I'm trying to access my ebay account using php cURL, I have an existing code but ebay doesn't seem to let me log in using cURL. It just displays the login page when I echo the result.
$username="testtest";
$password="testtest";
$url="https://signin.ebay.co.uk/ws/eBayISAPI.dll?co_partnerId=2&siteid=3&UsingSSL=1";
$gacookie="/var/www/barcode/cookie/cookie.txt";
$postdata = 'userid='. $username .':pass='. $password;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $gacookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $gacookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$AskApache_result = curl_exec ($ch);
curl_close($ch);
echo $AskApache_result;
Is it possible to login to ebay using php cURL by username and password or tokens?
Post data should look like this:
foo=bar&field=value
In your case:
$postdata = 'userid='. $username .'&pass='. $password;
try adding the following code
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
and troubleshoot further with
var_dump($AskApache_result);
echo curl_error($ch);
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);