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);
?>
Related
I have been given an example piece of code from a company I'm dealing with for how to post XML data to a URL then read the response. Unfortunately for me this in VBS which I don't have a good working knowledge of:
This is the section of code that I'm interested in. This should pass over the XML file that was read in to oXML then post it and read the response:
set oHTTP = CreateObject("Microsoft.XMLHTTP")
oHTTP.open "POST", "http://www.ophub.net/opxml/response.asp", false,00092,QW 'file url - with dealers Account number, Password
oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oHTTP.setRequestHeader "Content-Length", Len(sRequest)
oHTTP.send oXML
From what I understand of this in PHP this can be done with cUrl and I have come up with the following from bits that I have read online but this doesn't work and I'm not sure why.
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form- urlencoded"));
curl_setopt($ch, CURLOPT_URL, "http://www.ophub.net/opxml/response.asp");
curl_setopt($ch, CURLOPT_USERPWD, "00092:QW");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "XML=" . $xml);
$content=curl_exec($ch);
echo $content;
I'm sure I can't be far off what I need but I can't seem to get there so any hep would be very much appreciated.
In your post fields just set the xml no need to set xml=
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
you also need return transfer true
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
It looks to me like that is precisely what you need. It seems to be an exact translation.
The only two potential issues that I can see are:
You PHP code seems to have a space in the middle of the Content-Type header value, this will most likely break things
You will, if you haven't already done so, need to URL encode the $xml data before sending it as part of a application/x-www-form-urlencoded message (clue's in the type name :-P), which you can do with urlencode().
It might be a good idea to build the body data as a string and echo it out, to ensure that the data is correct:
echo 'XML=' . urlencode($xml);
For the record wrapping XML messages in application/x-www-form-urlencoded is a horrible way to do things. But I've come across more than one API that does it, so I'm going to assume that your code is correct in this regard for the API you are trying to consume.
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.
I've been trying to perform an XML request. I've faced so many problems that I managed to solve. But this one I couldn't solve.
this is the script:
$url ="WebServiceUrl";
$xml="XmlRequest";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
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);
echo $output;
It is giving me this error:
System.InvalidOperationException: Request format is invalid: text/xml. at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
I'm still a noob at this. So go easy on me:)
thanks.
Looks like you're sending stuff as text/xml, which is not what it wants. Find the docs for this web service e.g. WSDL stuff if it's there, and find out what data formats it accepts.
Be sure e.g. that it's not really saying it will respond in XML, after receiving a request as standard HTML POST variables.
There are two main content types used with the HTTP POST method: application/x-www-form-urlencoded and multipart/form-data.
The content-type determines what the format of the CURLOPT_POSTFIELDS should be. If you are using the default, which is "application/x-www-form-urlencoded" you probably want to use build_http_query() to construct the url encoded query string.
If you are sending non-ASCII data you canpass an associative array with keys that match the field names and values that correspond to the value for the field. Using this technique will cause the request to be issued with a multipart/formdata content-type.
At this point, it sounds like your next step should be figuring out what fields the API is expecting.
application/x-www-form-urlencoded or multipart/form-data?
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 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.