I am getting a Premature end of file response to my XML request for following code.
Cant figure out where is the error.
$xml = '<?xml version="1.0" ?>';
$xml .= '<PickUpCityListRQ>';
$xml .= '<Credentials username="'.$api->username.'" password="'.$api->password.'" remoteIp="'.$api->remoteIp.'" />';
$xml .= '<Country>UK</Country>';
$xml .= '</PickUpCityListRQ>';
$url = 'https://secure.rentalcars.com/service/ServiceRequest.do?serverName=www.rentalcars.com&xml='.utf8_decode(trim($xml));
$port = 443;
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, true); // Fail on errors
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off'))
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return into a variable
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_PORT, $port); //Set the port number
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // times out after 15s
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, '&xml='.$xml); // add POST fields
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
if ($port==443) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/xml; charset=UTF-8', 'Accept: application/xml; charset=UTF-8'));
$data = curl_exec($ch);
curl_close($ch);
Have tried sending data in post fields as well but did not work.
Response:
<!DOCTYPE DefaultRS SYSTEM 'https://xml.rentalcars.com:443//tj.dtd'><DefaultRS>
<Error id="2">
<Message>Premature end of file.</Message>
</Error>
</DefaultRS>
Any help appreciated.
Thanks
You need to url encode the xml data before you can use it as a query parameter. So the url assignment becomes:
$url = 'https://secure.rentalcars.com/service/ServiceRequest.do?
serverName=www.rentalcars.com&xml='.urlencode(utf8_decode(trim($xml)));
Note, I've just wrapped that for readability - that should obviously be on one line.
After trying a lot of variation I finally managed to make it work.
There were two things that were casuing problem.
utf8_decode should be utf8_encode
POSTFIELDS data is supposed to be an array
otherwise parser will throw Premature end of file or not send back anything.
Related
hoping someone out there can help. I have a small script using Curl to download a file from an rss feed.
The RSS feed validates as XML yet when I try to run my script I get the following:
‹íYsã6¶Çßó)x=5yá%Œ•=n§¸¦“^’j;=wª+´DKLK¤Š¤Ýíùôà"Ñk#™ÞªÔÕ•¸eŠàøƒƒ…‡?}_.Œ‹¤(Ó<{³‡Ü3’l’OÓlöfï“È{?ýpX”åæ*¬®’ßËʃ*/Š$«ÞìíïË*®Ò ˜äËýú·û ý=ùíÉ<βdqôƒ!ÿViµHŽª£ýri|:>6Î’djXÆ»t"oTž´¿:Üo®n¾¹H³¯GóªZ(s˜û‡ûõ‡Íï§I9)ÒU%Kyëý÷ûW4ßI«dÙüØ+ܯñ2) ?/¦If`ˆl2aãCšM/w±´93Þ'Ÿïئ–ÿé·ßÞIªêâÏu‰7÷œÄU2Ë‹Ë£“χûë´¿?ŒÏ«y^\«Öy™ûê~²‚íëÛÝòþRåµ&uy-U^2ak©Êk}ËkͧՅwRäù×ÒR÷·*„™C¨#À¼Z.ú³66;O§g¬¾ûÆØêü4ç(*Òÿ51>Ä—ÍÓGΘ&”÷»{º5ZLò¬’ÿŸd³j~D)ÇCˆIUn½àæ Òì,—ó#¢ àÄЇԣØó˜ís98Œ
8Èu< Ø»õú«7ﺌgYRýñé—£Ãÿùâî‰û¥ùèà§ïÕ›ó";8Òù®Í§Ù›Z³‘ÁTOÉ„ÌDجe0L%ƒ©d0L%ÃUñæ|ºú'qÿ‰#ù·*âÉפò©.ÒÉi¥z°üXöÙü<›$×/Ÿ-ò®gÕ7ù±íØÎ=×w·ÏWIvšVíwk#ÄPó›õÏòÇb&á½ïýùçÑF…Í£¾)C)!PÁÍÅÍ7
/\%vý›n^(¡˜ž¥Éôm®]vóò³t‘|”ÂÕêF= Ô„AhÔJ= Ôz#©Ú{mìoº±'¾ÈËó"1΋ś=ÕKÙƒå
7&ñdžù„º¯ïë6ºÎðO5àÞ|ipðçö5Ù3u/|³·î§{Fu¹JÞìÅ«•lŠ±âôþwkÓjöŒý–ÛûpßÎp?.Ê<3‚xqÙG¸›e
I've tested it with other XML links and it seems to work. The url is 'HTTPS' if that might make any difference and here is my code:
$url = "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
Any help is greatly appreciated
The output seems to be gzip encoded, try using:
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
Note:
This option sets the Accept-Encoding: gzip header on the request and uncompresses the response.
I make a curl call which seems to go fine.
[http_code] => 200, and curl_errno is 0.
Yet despite the fact there should be an output ([download_content_length] => 102), the curl_exec call doesn't return anything.
define('_WSURL', 'https://mobistar.msgsend.com/mmp/cp3'); // Feel free to try it yourself
$stderr = fopen("err.log", 'w+');
if ($stderr !== false) {
print "Opened the log file without errors";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, _WSURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
// curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $stderr);
// curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).DIRECTORY_SEPERATOR.'c00kie.txt');
$data = curl_exec($ch);
fclose($stderr);
print_r(curl_getinfo($ch));
print_r($data);
if ($data === false)
{ // Process curl error codes here
echo('ERROR');
echo(curl_errno($ch));
} else {
echo(sprintf('DATA: [%s]', $data));
}
curl_close($ch);
How comes?
Returns a response for me.
<?xml version="1.0" encoding="UTF-8"?>
<response code="499" description="The XML Content is Invalid"/>
I suspect you're seeing a blank page in your browser. If it's so, take a look at the page source in the browser, the XML should be there.
Arg, I'm so stupid sometimes...
The response is a XML and I tried to output it directly in the HTML, but of course it's just considered a set of invalid tags thus display as blank.
I should have taken a closer look to the source code XD
I have a Affiliate URL Like http://track.abc.com/?affid=1234
open this link will go to http://www.abc.com
now i want to execute the http://track.abc.com/?affid=1234 Using CURL
and now how i can Get http://www.abc.com
with Curl ?
If you want cURL to follow redirect headers from the responses it receives, you need to set that option with:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
You may also want to limit the number of redirects it follows using:
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
So you'd using something similar to this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://track.abc.com/?affid=1234");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
Edit: Question wasn't exactly clear but from the comment below, if you want to get the redirect location, you need to get the headers from cURL and parse them for the Location header:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://track.abc.com/?affid=1234");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
This will give you the headers returned by the server in $data, simply parse through them to get the location header and you'll get your result. This question shows you how to do that.
I wrote a function that will extract any header from a cURL header response.
function getHeader($headerString, $key) {
preg_match('#\s\b' . $key . '\b:\s.*\s#', $headerString, $header);
return substr($header[0], strlen($key) + 3, -2);
}
In this case, you're looking for the value of the header Location. I tested the function by retrieving headers from a TinyURL, that redirects to http://google.se, using cURL.
$url = "http://tinyurl.com/dtrkv";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$location = getHeader($data, 'Location');
var_dump($location);
Output from the var_dump.
string(16) "http://google.se"
How to run url from php script in the same way (exactly the same behaviour) as in browser when i run url from address bar. I mean with the same header data, cookies and additional data which browser send. How to add this data in php.
I need this cause when I logged in, answers from this 2 cases are not the same:
in browser I still logged in and this is correct
from php run I am logged OUT - not correct
I've tried file_get_contents nad curl (from here) but it doesn't work properly - response is still different.
I'm calling http://127.0.0.1/check.html and here is function check:
public function check(){
echo 'begin';
// $total_rows = file_get_contents('https://127.0.0.1:8443/example.html?shopId=121');
$total_rows = $this->getUrl('https://127.0.0.1:8443/example.html', '121');
print_r($total_rows);
echo 'end';
}
function getUrl($url, $shopId ='') {
$post = 'shopId=' . $shopId;
$ch = curl_init();
$cookie_string="";
foreach( $_COOKIE as $key => $value ) {
$cookie_string .= "$key=$value;";
};
$cookie_string .= "JSESSIONIDSSO=66025D1CC9EF39ED7F5DB024B6026C61";
// echo $cookie_string;;
$ch = curl_init();
curl_setopt($ch,CURLOPT_COOKIE, $cookie_string);
// curl_setopt($ch, CURLOPT_PORT, 8443);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/../../files/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Secure Content-Type: text/html; charset=UTF-8"));
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: 127.0.0.1:8443'));
$ret = curl_exec($ch);
curl_error ($ch );
curl_close($ch);
return $ret;
}
Try it:
http://www.lastcraft.com/browser_documentation.php
Or that:
http://sourceforge.net/projects/snoopy/
Or that:
php curl: how can i emulate a get request exactly like a web browser?
Hope help
You can execute the cron job using your PHP script to execute the other script.
Because the received string has a lot of gibberish in it and I can't use it as an xml. Any thoughts?
$login_xml ='<xml>'.
'<action>log_in</action>'.
'<parameters>'.
'<username>'.$var.'</username>'.
'<pass>abdef01</pass>'.
'</parameters>'.
'</xml>';
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $login_xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
$output = curl_exec($ch);
Maybe some options in the CURLOPT_HTTPHEEADER? Thanks for your time!
edit:
var_dump($output) : string '<xml><action>log_in</action><parameters><username>Ionel P</username><pass>abdef01</pass></parameters></xml>107' (length=110)
edit 2:
<?php
$xml = readfile('php://input');//file_get_contents('php://input');
var_dump($xml);
print_R($xml);
?>
var_dump($xml) = pre class='xdebug-var-dump' dir='ltr'><small>int</small> <font color='#4e9a06'>107</font>
107' (length=207)