I've been banging my head on this for a while now and I'm sorta fed up. I'm not a PHP programmer so I might be missing something that is not immediately obvious to my Python infused brain.
So here's the context. I need to write a script to login automatically into a web interface and run a search, and well, absolutely everything I've tried to do seems to fail miserably.
Here's my code:
<?php
echo 'start';
// INIT CURL
$ch = curl_init();
// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'https://W0110DcIpsOcsRpt02/si_ocs_gui/login.php');
// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);
// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=*****&password=*****&btnSubmit=Login');
// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, '/var/tmp/cookie.txt');
# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_FRESH_CONNECT, 1);
// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);
if($store == False){
echo " store false ";
echo curl_error($ch);
} else {
echo " store true ";
echo $store;
}
// SET FILE TO DOWNLOAD
echo ' second_request ';
curl_setopt($ch, CURLOPT_URL, 'https://W0110DcIpsOcsRpt02/si_ocs_gui/FWOCS1_BL_SUBSCRIBERS_list.php');
curl_setopt ($ch, CURLOPT_POST, 1);
#curl_setopt ($ch, CURLOPT_POSTFIELDS, 'value_ACCESS_NO_4=15142351150');
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'q=\(SUBSCRIBER_ID~equals~1545303\)');
// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
if(!$store == False){
$content = curl_exec ($ch);
echo $content;
}
// CLOSE CURL
curl_close($ch);
echo ' done';
?>
The behaviour of this is as follows. I run the script, and it fails at the first cURL request (the login) and returns no errors. I have modified this line here:
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=*****&password=*****&btnSubmit=Login');
by removing the btnSubmit=Login part and I get the login page displayed with the filled in username and password fields. I can press the login button and it will work. However, it seems that my lack of web development is biting me here and I have no idea how that button works. So, here's the code inspected with firebug:
<a class="rnr-button main" href="#" onclick="document.forms[0].submit();return false;">Submit</a>
I also went directly in the PHP code on the web server and found the button corresponding to it:
if ((#$_POST["btnSubmit"] == "Login" || $adSubmit) && $logacc)
Hence why I was trying the btnSubmit=Login.
So my question is pretty simple: what am I doing wrong and how can I get the results I need?
I have got the same problem and find the solution for it.
Code
CURLOPT_RETURNTRANSFER => false
Use this to solve your problem.
Related
I'm making a web and one of its functions need to get content from itself. Using curl with the following function:
function get_page_code($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
if (curl_errno($ch)) {
echo curl_error($ch);
echo "\n<br />";
$contents = '';
} else {
curl_close($ch);
}
if (!is_string($data) || !strlen($data)) {
$data = '';
}
return $data;
}
The issue appears when I noticed I need to have a cookie so the content I want to get actually appears, so my question is how can I send cookies with this curl function? Of course I've those cookies on my computer but once sent the request to get the html code of the introduced url those cookies are not there.
Cookies I want to send with curl:
session: ab1b298aslc
gender: m
I'd grateful with any help :-)
EDIT:
Tried adding this lines to the function and nothing happened:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: gender=m"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: session=ab1b298aslc"));
Please excuse the probable simplicity of the question but I am new to curl and therefore finding my feet. I have the following which is in a php page and when called meant to post dept=xxx to mysite
$ch = curl_init( );
curl_setopt($ch, CURLOPT_URL,"http://mysite/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "dept=".$departmentName);
curl_exec ($ch);
curl_close ($ch);
I have 2 queries:
1) When called the screen goes blank and the user is not redirected.
2) If redirection is correct, am I right in thinking that the following will correctly receive the data posted:
if (isset($_POST["dept"]))
{
$deptName=$_POST["dept"];
}
Include this cURL paramter to your existing set.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
I'm trying to figure out why this won't work for me. I'm a complete noob when it comes to cURL, today is my first day using it. I followed a tutorial for this but obviously failed.
It should check the page and if it sees "Skill Stats" on there, then return "Success", and return "Failure" if it spots "Member Rankings".
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://services.runescape.com/m=hiscore/compare.ws?user1=Mercon185");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
if (stristr($output,"Skill Stats")) {
echo 'Success';
}
if (stristr($output,"Member Rankings")) {
echo 'Failure';
}
curl_close($ch);
?>
`
You need to enable follow redirects. As I see currently, your URL redirects to http://services.runescape.com/m=hiscore/overall.ws?errorcode=1. Without follow redirects enabled, it only fetches the first page, which indeed is empty.
The final landing page though, contains the data you want, so if you add this line to your cURL options, it should work:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
I have written a php script to login to a site from curl. Below is my code:
<?php
// INIT CURL
$ch = curl_init();
// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'http://wordpress.dev/wp-login.php');
// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);
// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'log=admin&pwd=admin');
// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);
var_dump($store);exit;
// CLOSE CURL
curl_close ($ch);
?>
If I give right password it gives string() " and if give wrong password it redirects me to the login page
How can check that login is successful?
You need to check response code as well. This might help you.
$contents = curl_exec($ch);
$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
var_dump($httpcode);
var_dump($contents);
In most cases HTTP code 200 is a valid authentication.
curl_exec() Returns TRUE on success or FALSE on failure.
<?php
//execute the request (the login)
$store = curl_exec($ch);
if($store){ //check for true/false
//Logged in
}else{
//Login failed
}
?>
UPDATE:
if you are on HTTPS, add this:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
UPDATE 2:
add this:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
you can check it with $store it will return true on success otherwise false.
check Storing the response of login using curl? . think it will solve your problem.
You should pay attention to status codes.
curl_exec would return true even if your login is not validated.
It simply means that the curl request is executed without error..
Make sure that status code is "200"..
Hopefully, someone can spot the error, what I need to do is to first fetch the webpage for a token, then curl the new url with the token attached;
here is my code
$text = $siteName;
if (preg_match('/;t=([a-zA-Z0-9_-]{43})%3D/',$text,$matches)) {
// Match... vjVQa1PpcFMYuRsz10_H-1z41mWWe8d6ENEnBLE7gug
echo 'TOKEN: '.$matches[1];
$curltube = curl_init ();
curl_setopt ($curltube, CURLOPT_URL, "http://www.veoh.com/watch?v=opQ9GzRe5qs".$matches[1]);
curl_setopt ($curltube, CURLOPT_RETURNTRANSFER, 0 );
curl_setopt ($curltube, CURLOPT_COOKIEFILE, "cookie8.txt");
$curltubeplay = curl_exec ($curltube);
curl_close ($curltube);
echo $curltubeplay;
} else {
// No match
}
and the previous code before that fetches the web-page
curl_setopt ($ch, CURLOPT_URL, "http://www.veoh.com/watch?v=opQ9GzRe5qs");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0 );
curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookie8.txt");
so hopefully, someone can shed some light
My guess (please expand the question to be clear) is that you expect to build a URL like this:
http://www.veoh.com/watch?v=opQ9GzRe5qs;t=abc
But you are building one like this:
http://www.veoh.com/watch?v=opQ9GzRe5qsabc
There are two simple fixes. This one takes the whole matched string, not just the token:
curl_setopt ($curltube, CURLOPT_URL, "http://www.veoh.com/watch?v=opQ9GzRe5qs".$matches[0])
And this one adds back in the missing parts to the URL:
curl_setopt ($curltube, CURLOPT_URL, "http://www.veoh.com/watch?v=opQ9GzRe5qs;t=".$matches[1])