Get response URL using PHP / cURL very slow - php

Am trying to figure out what method to use here.
Am trying to write some php to query a servlet and get the redirect URL / response ONLY.
For example - going to this URL / servlet request http://www.argos.ie/webapp/wcs/stores/servlet/CheckPriceAndStockCmd?partNum=5240793&storeSelection=4101 will redirect to the following URL / servlet response instantly: http://www.argos.ie/webapp/wcs/stores/servlet/CheckPriceAndStockResultView?query-result=13&pickupStoreNumber2=826&pickupStore=Arklow&storeStockStatus=0&availableQuantityInStore2=1&storeStockStatus2=1&outOfStockEmail=&pickupStore2=Wexford&failureView=CheckPriceAndStockResultView&partNum=5240793&isExtraStoreRequest=0&promisedAvailTime2=2011-09-19&pickupStoreType=3&availableQuantityInStore=0&storeSelection=4101&pickupStoreNumber=4101&stockChecked=true&pickupStoreType2=3&promisedAvailTime=&estAvailTime=&catentryid=95934&estAvailTime2=&ddkey=http:CheckPriceAndStockCmd
I want to basically get that response and some of the parameters in it (I can do the latter).
Have tried using various cURL methods to output headers and / or get final redirect URL but they're either not useful or very slow:
$url = "http://www.argos.ie/webapp/wcs/stores/servlet/CheckPriceAndStockCmd?partNum=" . $catNum . "&storeSelection=" . $storeNum;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$content = curl_exec ($ch);
curl_close ($ch);
Anyone got any suggestions?

Related

Post body not received

My curl post doesn't return a body on the server side. How can I rectify this?
curl_setopt($ch, CURLOPT_URL,$cpUrl);// "ex:www.lalala/aa/"
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $varpost); // has string parameter
curl_setopt($ch,CURLOPT_HTTPHEADER,array('VariableParams:'.$varpost ));
info ("variables passed:",$varpost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_NOBODY, false);
$output = curl_exec ($ch); // Execute
It can happen that for the server the request is redirected into another url. For example based on location some website redirects it to a particular location. That's why you are not getting any body as you are disabling the follow location option.
Try by enabling it.
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);

curl getting header and cookie from one URL and posting to another URL to retrieve data

I'm trying to retrieve json data from a URL, however it requires http authorization (no password) just cookie and header data which I would need to get from the parent URL.
I've managed to retrieve header information successfully however integrating it within the post part of the script I am having difficulty with.
function check_link($link) {
$main = array();
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $link);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_NOBODY, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_NETRC, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 300);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
ob_start();
curl_exec ($ch);
$stuff = ob_get_contents();
ob_end_clean();
curl_close ($ch);
return $stuff;
}
$link = 'http://www.avis.fr/default.aspx'; //parent URL
$returnData = check_link($link);
print_r($returnData); //HEADER INFORMATION
curl_setopt($ch2, CURLOPT_HTTPHEADER, array ($returnData));
$url2 = "http://www.avis.fr/WebServices/LocationSearch.asmx/GetStations"; // where I want to post data
curl_setopt($ch2, CURLOPT_URL, $url2);
curl_setopt($ch2, CURLOPT_POST, true); // tell curl you want to post something
curl_setopt($ch2, CURLOPT_POSTFIELDS, "searchTerm=ares&countryId=56&pickUpDate=2014-04- 15 09:00&dropOffDate=2014-04-17 09:00&isPickup=true&returnToSameLocation=true"); // define what you want to post
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); // return the output in string format
$output = curl_exec ($ch2); // execute
var_dump($output); // show output

AS3 do post through PHP without changing doted parameters

I am trying to communicate with Google Forms using AS3 and PHP. The problem is that when I pass parameters such 'entry.283123610', PHP changes these into 'entry_283123610'. How can I prevent PHP from changing the post variables? This is the code I am currently using:
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_POSTFIELDS, $params);
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();

Having Difficulty Using cURL and PHP to Login and Download

Here is the code that I am using to try and accomplish this:
<?php
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'linkToLoginPage.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'email=email#example.com,password=password');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
curl_setopt($ch, CURLOPT_URL, 'linkToDownloadFile.pdf');
$content = curl_exec ($ch);
curl_close ($ch);
?>
I set it up with my information, but it just produces a blank page. I think my main issue is with the CURLOPT_POSTFIELDS. Here is the link to the page I am trying to login.
I looked at the source code but I cannot find a clear definition of the form values. Any ideas?
Try
curl_setopt($ch,
CURLOPT_POSTFIELDS,
'email=email#example.com&password=password' );
(& not ,)

Logging into a PHP forum using lib-cURL and getting a page contents

This is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.membersite.com/login.php");
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=deleted&password=deleted');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'C:\xampp\htdocs\scrape\cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
curl_setopt($ch, CURLOPT_URL, "http://www.membersite.com/memberlist.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec ($ch);
echo $page;
curl_close($ch);
But I don't think it's successfully logging in as the site (my own, by the way) doesn't show a log of me signing in. I know the username and password are correct, as are the URLs. I get a cookie.txt file back with what looks like good data but I'm not sure.
If I try some basic debugging, like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.membersite.com/login.php");
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=deleted&password=deleted');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'C:\xampp\htdocs\scrape\cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(!$store = curl_exec($ch))
{
echo "login fail";
}
curl_setopt($ch, CURLOPT_URL, "http://www.membersite.com/memberlist.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(!$page = curl_exec($ch))
{
echo "page fail";
}
echo $page;
curl_close($ch);
I get a "page fail" being printed to the page, so I guess the logging in isn't working.
Any help? thanks.
Nevermind, downloaded a phpBB cURL library and sorted it.
Add the CURLOPT_COOKIEFILE option with same value as the CURLOPT_COOKIEJAR option.
You also need to use those two options for each subsequent request (ie your request for memberlist.php).
You can see a way of doing it here.

Categories