How do I receive an xml response using php curl? - php

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.

Related

How do I pass XML data as a PAYLOAD on the stream?

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);
?>

CURL result into PHP array?

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.

Problem fetching XML data using Expedia API and curl

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&currencyCode=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'));

Authentication using JSON and PHP

Can anyone kindly tell me how to use JSON base Authentication using/in PHP?
I got some helping code (as given below), but can not understand that how to use it.
url = http://testerws.heroku.com/
POST url + 'user_session.json',{:username => "admin",:password => "admin"}, :accept => 'application/json'
I am not sure whether this code is correct (with respect to syntax) or not. And I can not understand how to use it in combination with PHP.
So kindly help me to solve this, I am in great need of it.
You can use curl of php to post the json data.
$json = json_encode($data);
$http_post_data = array("data" => $json);
$url = 'http://testerws.heroku.com/test.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $http_post_data);
$result = curl_exec($ch);
curl_close($ch);
Then test.php will get the data which you post. You can do authentication your self and return to the calling script.
But the authentication is not that easy for a security system.
FYR.
http://blog.evernote.com/tech/2011/05/17/architectural-digest/
and read the comment.
What is the context of the authentification? If it's a webapplication you could probably fill in a form, post the data as JSON using JQuery and decoding the JSON on the PHP side and check the validation. Be sure to encrypt the information you send from the browser though. Username and password as clear text is not secure at all.

How to get CURLs reponse in Array

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

Categories