The code below sends XML via CURL using POST with a http header of text/xml. The server responds by echoing $ch_result. I am making a call to this file via AJAX as the content of the returned XML file will need to be displayed on the client-side. Is there anyway I can get this XML file into an array so I can use json_encode and have it return like that? I need this content to be easy to manipulate using JavaScript as the XML file returned is large... so an array might be best?
Thanks!
<?php
$xml_builder = '
MY XML POSTED TO SERVER GOES HERE';
$ch = curl_init('http://username:password#myserver.blabla/api');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.mydomain.co.uk');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ch_result = curl_exec($ch);
curl_close($ch);
echo $ch_result;
?>
You can parse the XML on server side, but also on client side.
On server side:
Simple and dirty way:
<?php
$a = json_decode(json_encode((array) simplexml_load_string($s)),1);
?>
however, please read this article:
http://gaarf.info/2009/08/13/xml-string-to-php-array/
On client side:
If you are using jQuery you can set dataType to xml, and can parse the xml on client side with jQuery:
How to parse XML using jQuery?
To parse xml using jquery you must echo: htmlentities($ch_result) so that the XML is echoed with the tags etc.
Related
I'm new to XML, and usually use JSON to pass data. I am working with a new system, and this was part of their instructions to me about passing data:
The XML content then can be sent as either PAYLOAD on the stream or as an additional parameter. If the latter is done, the parameter name is RequestXML
I'm not sure what this means? I'm afraid if I pass it as a parameter, but I have a lot of text, it will make the URL too long, so I'd like to do the PAYLOAD option. I'm using PHP and Jquery to generate the array. I can create an XML file using PHP and have it properly formatted as XML, but sending it across is confusing me.
What do I need to do to get it sent as a PAYLOAD?
You'll likely just want to send a POST HTTP request. Here's an example using the curl library:
<?php
$url = "https://example.com/service";
$xml = "<foo />";
$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, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
?>
I'm trying to fetch this url via a script: http://api.alarabiya.net/sections/2/
But JSON response received is much smaller than when I open it directly in a browser,
please notice that I tried this url through CURL and set the same USER-AGENT of the browser and all request header used in the browser and I still get a smaller response.
Here's an exmaple using just file_get_contents
<?php
echo file_get_contents("http://api.alarabiya.net/sections/2/");
?>
My question is if there's a request size limit when using file_get_contents or if the PHP's memory can't handle it or what's the problem exactly?
When I CURLed this in shell it gave me the same o/p as in php (the trimmed output).
I finally found a solution for this:
$url = "http://api.alarabiya.net/sections/2/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
// This is what solved the issue (Accepting gzip encoding)
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
$response = curl_exec($ch);
curl_close($ch);
echo $response;
When I load the URL used below in curl function directly in a browser, I get correct data in XML format.
But when I call it through curl, sometimes I get data in JSON format or sometimes no dat at all.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://api.ean.com/ean-services/rs/hotel/v3/list?minorRev=4&cid=55505&apiKey=5q4gzx43g6ukcrq798z2hz75&customerSessionId=&locale=en_US¤cyCode=USD&xml=<HotelListRequest><city>new%20delhi</city><RoomGroup><Room><numberOfAdults>2</numberOfAdults><numberOfResults></numberOfResults></Room></RoomGroup></HotelListRequest>");
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$retValue = curl_exec($ch);
curl_close($ch);
echo $retValue;
The above API is working so you can test it yourself.
You need to specify XML content type explicitly in your HTTP request by setting Accept header field, so REST server knows what do you want e.g.:
Accept: text/xml,application/xml;q=1
In your case (q=1 is default value):
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Accept: text/xml,application/xml'));
How do I receive an xml response using php curl? I don't need to manipulate the xml or store it or anything like that. I just need to receive it as plain xml. Does anyone know how to do this?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/path/to/service');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec($ch);
$store contains response.
I have one problem regarding CURL.
I am trying send CURL request to http://whatismyipaddress.com
so..
is there any way to get curl response in array ?
because right now it display the HTML Page but i want the response in array.
here is my code...
$ipAdd = '121.101.152.170';
$ch = curl_init("http://whatismyipaddress.com/ip/".$ipAdd);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
So.. currently i am getting the detail HTML page but i want the out put as array or XML.
The "easiest" path is going to be to find the surrounding text & extract based on that.
If you're willing to step your dedication to this up, you can use something like http://simplehtmldom.sourceforge.net/ & go from there.
edit: actually you can use this (it is built into php5) - http://php.net/manual/en/book.dom.php
more specifically, this - http://www.php.net/manual/en/domdocument.loadhtml.php