Help w/ Retrieving XML via cURL - php

I've run into trouble with the following php code:
<?php
$url = "http://api.ean.com/ean-services/rs/hotel/v3/list? minorRev=1&cid=55505&apiKey=58x5kuujub8xbb5tzv3a2a8q&locale=en_US&currencyCode=USD&xml= <HotelListRequest><destinationString>Seattle</destinationString> <arrivalDate>08/01/2011</arrivalDate><departureDate>08/03/2011</departureDate><RoomGroup> <Room><numberOfAdults>2</numberOfAdults></Room></RoomGroup> <numberOfResults>1</numberOfResults></HotelListRequest>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$contents = curl_exec ($ch);
echo $contents;
curl_close($ch);
?>
The problem is that $contents contains markup that's not XML at all, so I can't parse it. It's confusing b/c entering the URL in my browser's address bar will display the XML document, but I can't seem to get a valid XML doc w/ this code.
Here is a snippet of the string that gets returned:
{"HotelListResponse":{"customerSessionId":"0ABAA83D-4428-4913-0382-28FBB1901EFC","numberOfRoomsRequested":1,"moreResultsAvailable":true,"cacheKey":"-32344284:1303828fbb1:-1ef9","cacheLocation":"10.186.168.61:7305","HotelList":{"#size":"1","HotelSummary":{"#order":"0"
Could someone explain to me where I'm going wrong?
Thx.

Instead of trying to get XML, which may not be provided, you could always work with what you have, which appears to be JSON.
$response = json_decode( $contents, true );
This will give you an associative array of your data, which can be much easier to work with.

Try to remove spaces: "/v3/list? minorRev=1" -> "/v3/list?minorRev=1"
Make your URL correct, like
$url = 'http://api.ean.com/ean-services/rs/hotel/v3/list?type=xml&minorRev=1&cid=55505&apiKey=58x5kuujub8xbb5tzv3a2a8q&locale=en_US&currencyCode=USD&xml=%3CHotelListRequest%3E%3CdestinationString%3ESeattle%3C/destinationString%3E%3CarrivalDate%3E08/01/2011%3C/arrivalDate%3E%3CdepartureDate%3E08/03/2011%3C/departureDate%3E%3CRoomGroup%3E%3CRoom%3E%3CnumberOfAdults%3E2%3C/numberOfAdults%3E%3C/Room%3E%3C/RoomGroup%3E%20%3CnumberOfResults%3E1%3C/numberOfResults%3E%3C/HotelListRequest%3E';
Add option to accept xml only -- in browser we have such header -- in curl -- no:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xml'));
PROFIT!!!

Related

new line in url causing bad request - invalid url when using php curl

I have the following php:
$ch = curl_init("http://domain/blah/".rawurlencode("foo bar"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$result = curl_exec($ch);
print_r ($result);
curl_close($ch);
That works as it should, but when I change foo bar to foo\r\nbar or foo\nbar or drop the rawurlencode and just send foo%0D%0Abar or foo%0Abar, I get a Bad Request - Invalid URL error.
If I try:
$ch = curl_init("http://domain/blah/foo\r\nbar");
I get no error, but nothing happens.
I can put some other characters in the url and have them replaced with newlines, but that seems a bit of a workaround. I'm looking for the right way to do this.
I'm using php 5.6.7

php curl_setopt() returns json data with "1" on end

so I have a curl_setopt that is pulling a json file just fine with php. It does this with one exception, at the end of the json data there is a one (1) on the end after the last '}'. This "1" is not apparent in the url call by itself without using curl though. So it seems my curl_setopt is not configured properly. Can someone help with this?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $domain.$args);
curl_setopt($ch, CURLOPT_HEADER, false);
$json = curl_exec($ch);
curl_close($ch);
the $domain.$args is working fine as I can echo out this variable setup and produce the json manually via browser without the 1.
appreciate the help
/** edit after suggestions **/
I tried the suggestion below of adding:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
this just ended up changing the entire json output, not just adding a "1" on the end of the response:
"{\"data\":[{\"Name\":\"A3\",\"SeoName\":\"a3\"},{\"Name\":\"A4\",\"SeoName\":\"a4\"},{\"Name\":\"A5\",\"SeoName\":\"a5\"},{\"Name\":\"A6\",\"SeoName\":\"a6\"},{\"Name\":\"A7\",\"SeoName\":\"a7\"},{\"Name\":\"A8\",\"SeoName\":\"a8\"},{\"Name\":\"allroad\",\"SeoName\":\"allroad\"},{\"Name\":\"Q5\",\"SeoName\":\"q5\"},{\"Name\":\"Q5 hybrid\",\"SeoName\":\"q5-hybrid\"},{\"Name\":\"Q7\",\"SeoName\":\"q7\"},{\"Name\":\"R8\",\"SeoName\":\"r8\"},{\"Name\":\"RS 5\",\"SeoName\":\"rs-5\"},{\"Name\":\"RS 7\",\"SeoName\":\"rs-7\"},{\"Name\":\"S4\",\"SeoName\":\"s4\"},{\"Name\":\"S5\",\"SeoName\":\"s5\"},{\"Name\":\"S6\",\"SeoName\":\"s6\"},{\"Name\":\"S7\",\"SeoName\":\"s7\"},{\"Name\":\"S8\",\"SeoName\":\"s8\"},{\"Name\":\"SQ5\",\"SeoName\":\"sq5\"},{\"Name\":\"TT\",\"SeoName\":\"tt\"},{\"Name\":\"TTS\",\"SeoName\":\"tts\"}]}"
Use this:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
Fix but not ideal is to substr() it to strip out the 1.
substr($result, 0, strlen($result) - 1);

Parsing a JSON string (or is it XML?)

I have an XML file I need to pass.
http://mckay.canvashost.com:8080/opentripplanner-api-webapp/ws/transit/stopTimesForStop?id=27833&startTime=1361784386000&extended=true&references=true
I used PHP's CURL to load it into the page:
$url = "http://mckay.canvashost.com:8080/opentripplanner-api-webapp/ws/transit/stopTimesForStop?id=27833&startTime=1361784386000&extended=true&references=true";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$xml = curl_exec($ch);
curl_close($ch);
echo htmlentities($xml, ENT_QUOTES, "UTF-8");
The output is a JSON file, which I wasn't expecting. No matter, I continued working with the JSON but can't seem to parse it.
I had thought this would work:
$obj=json_decode($xml);
echo $obj[stopTime][0][phase];
With a result of 'departure'. I've spent a good while on this yet can't get it to output anything.
Anyone help? And anyone know at which point it switched from an XML file to JSON format?
the output is a xml-string.
try to use php's xml functionality delivered by SimpleXml:
SimpleXml
as soon as you successfully parsed the string you can iterate its nodes like an array
foreach ($xmlElement as $node) {
echo $node->attributeName;
}

sending xml string using php curl but not as post parameter

:)
I'm trying to send an XML using curl but not as post parameter. what I mean is this.
for example.
the receiving side of that XML won't be able to recieve the XML using $_POST variable.
he will need to use the following code:
$xmlStr=null;
$file=fopen('php://input','r');
$xmlStr=fgets($file);
I want to be able to send an xml string using curl via https.
so the following would be wrong:
public static function HttpsNoVerify($url,$postFields=null,$verbose=false) {
// Initialize session and set URL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($postFields !=null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
if ($verbose) {
curl_setopt($ch, CURLOPT_VERBOSE, 1);
}
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
because here i can use HttpsNoVerify($url,array('xml_file'=>'xml..')); and that
will paste it as post parameter. and i want it as post output.
so please I hope i explained myself properly and I explained exactly what I don't want to do.
how can I do what i want to do?
thanks! :)
kfir
Just directly pass the xml string as second parameter instead of an associative array item,
HttpsNoVerify($url, 'xml ..');
This will eventually call
curl_setopt($ch, CURLOPT_POSTFIELDS, "xml ...");
Which will be put in php://input for the remote server.

curl_exec() outputting destination contents to page?

I'm using the following code:
$ch = curl_init('www.google.com');
$output = curl_exec($ch);
curl_close($ch);
This is not the first time I've used cURL, and unless I'm mistaken the above code should retrieve the contents of google.com and store it in $output. Correct?
So, why then, does the above code output the contents (in this example the Google homepage) to the page? I'm not echo'ing anything out, but for some reason the curl_exec() function is outputting what it returns to the page.
Am I missing something?
you need to use
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
That will tell curl_exec not to output the results
so change it all to
$ch = curl_init('www.google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

Categories