I need to redirect the current user to an external page (other domain) with some parameters that will be used on that page. To do that I'm using cURL, an example code is below:
$postfields = array('key'=>'value');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://domain.com/example.aspx');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
$result = curl_exec($ch);
curl_close ($ch);
But the user stays on the same page and I don't know if any data is transmitted.
What is the problem here? Is there another way to do that?
Related
I am submitting login form data using curl in php, and from what I've observed the login POST has no response - the lander page is loaded by a subsequent GET request. What I'd like to know is how I can make a POST using curl and then a GET? For reference, here's my current code:
$ch = curl_init('https://<my url>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: text/html',
));
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'my post fields');
$result = curl_exec($ch);
// I need to make a get request to get the loggedin page here!?
curl_setopt($ch, CURLOPT_URL, 'https:<another_url>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 0);
$result = curl_exec($ch);
echo curl_errno($ch);
I'm currently getting an error 52 from my POST which is no response, which I think should be expected. The issue is, when I tried to make a GET, I also got errno 52. Also for a sanity check, I retrieved my post fields my grabbing the form data from the network data from chrome dev tools.
Thanks in advance for the help!
I am trying to login to a site using curl but for some reason when I use the print_r($login) to view the output, I always end up seeing the original login page without it being processed (it was supposed to return something like login success or fail), I think curl is not able to submit the form, any suggestion?
$user="something"; $pass="something";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username={$user}&password={$pass}");
curl_setopt($ch, CURLOPT_USERAGENT, "Something");
$login = curl_exec($ch);
print_r($login);
You need to add
curl_setopt($ch, CURLOPT_POST, TRUE);
To send POST content. Without it, your CURLOPT_POSTFIELDS will be ignored.
I am doing a remote location, and I can successfully login, and follow the location of the url to the next page. But from there I need to go to another page(it does not automatically redirect), I need to do it manually. This is the link after the login and follow location https://homeaccess.katyisd.org/HomeAccess/Classes/Classwork, and this is the link I need to manually redirect to https://homeaccess.katyisd.org/HomeAccess/Classes/Classwork
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$url = curl_setopt($ch, CURLOPT_URL, 'https://homeaccess.katyisd.org/HomeAccess/Account/LogOn? ReturnUrl=https://homeaccess.katyisd.org/HomeAccess/Classes/Classwork');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_REFERER, 'https://homeaccess.katyisd.org');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$wrapperPage = (curl_exec($ch));
curl_close($ch);
print $wrapperPage;
?>
In this instance you can not redirect much like what would happen if you were using a browser. You'll have to do another cURL session, setting your URL as:
https://homeaccess.katyisd.org/HomeAccess/Classes/Classwork
It's not the user which spawns the redirect, its the web server when it sends the HTTP response code 302.
I want to open a page from a site and save the cookies. Then open another page with those cookies.(It doesn't let me to visit the second page without the first page's cookies)
I tried this code but it didn't work.
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cook.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cook.txt');
curl_setopt($ch, CURLOPT_URL, 'url');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_exec($ch);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'nexturl');
$rs = curl_exec($ch);
echo $rs;
End of your each curl operation close the curl handle. Otherwise it will not save the cookie into the file.
$rs = curl_exec($ch);
curl_close($ch);
Now initiate second curl, and send the request. It should work now.
Im trying to get the source code of a page with CURL
http://www.deindeal.ch/zurich/?a_aid=dealanzeiger&a_bid=62e0def7
but the only thing I get is the index site (http://www.deindeal.ch), as I would be redirected automatically. I suppose they are avoiding hotlinking or something like that? And in that case, how could I get the source code, maybe some curl setopt ?
My CURL connection:
$ch = curl_init();
$timeout = 60;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch, CURLOPT_HTTPGET, false);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
Thanks!
Take out this option:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
However, if the site's redirecting, they may not output anything EXCEPT the redirect, so not following the redirect still won't get you any content, because they just flat-out don't send it.