Yii SOAP Web Service returning XML - php

I am trying to write a web service API using PHP and the Yii framework. This means that I will be using SOAP web services, since Yii does not directly support RESTful web services.
I would like to return results in XML format, as strings (this might be stupid, but I'm working with rather cumbersome data types that would make life more difficult for the web service clients trying to use them). The problem I'm facing is that the XML I'm returning contains tags, which leads to the strings not being read properly by the web service client.
For instance, I can return this string in one of my web services:
"hello"
But not this string:
"<hello>"
(An empty string is read by the client)
So, is there a way for a SOAP web service to return an XML document as a string (in general, or using Yii in particular)?

This should really be done by the framework (don't know about Yii though), but since it doesn't seem to work automatically for you, try escaping the data first:
$data_to_send = htmlspecialchars($data_to_send);
If Yii doesn't encode the data, your client will see the correct output (the raw XML string) when fetching the data from their SOAP library. If Yii, however, does encode the data, it will get double-encoded which is probably not what you want, but if it does encode it you shouldn't have this problem in the first place...

If your data contains characters that are also XML tags, you can use the CDATA option.
<value><![CDATA[<hello>]]></value>

Please don't use soap. It is one if the worst technologies ever made (see the numerous posts on SO) . Yii does support REST now (fully as of 1.17). Just use JSON format. Do you need to delete or insert from the client?

Related

XML to PHP for Soap call

I am trying to compile a Soap call with PHP (the params part of the call), but I am having trouble converting the XML nodes to PHP Soap to make the call.
I am trying to take this XML
https://gist.github.com/Davidlab/f40f386c65347f6d21e4
and convert it to a PHP soap (the params part of it).
If you look at the XML file you will see that there are 2 nodes and I need to translate that to this PHP sample that has only one node.
https://gist.github.com/Davidlab/e3cc11a7c80a4c273d31
I can get it to work with one but can not figure out the proper way to include another within the call.
Thanks for any help you may provide.
Use the built in SOAP functions in PHP. I just went through figuring out SOAP. After lots of Googling and emailing back and forth with tech support I figured out nobody really knows what SOAP is. :) Anyway the built in SOAP functions do all of the conversions for you. Very simple to use.
The easiest way to figure out how your data structures should look is to do a little bit of discovery like this:
$wsdl = "http://the-location-of-the-wsdl";
$soapClient = new SoapClient($wsdl);
var_dump($soapClient->__getFunctions());
var_dump($soapClient->__getTypes());
__getFunctions() will show you all the methods you can call, the data types they accept and return. __getTypes() will show you what the data structures look like. Create your arrays or classes so they follow the same structure. This is much easier than trying to figure it out with raw XML.

Is there a XML decoder for PHP objects?

I am build a application in Delphi that stores some configuration that is not going to be passive to query filters on the database. So I decided to create some blob text fields to store those configurations that will only be used as keys to configure some modules of the application.
In this Delphi side of the application, I am using NativeXML run-time components to decode the configuration class or record type of each module into/from XML and populate that field on the database.
My problem came when I realized that this application will have a web site module where people will register for clinical attending and this part will need to use some of the configuration stored on that XML on the database. So...
I am newbie on PHP and I wish to know from you if PHP has the ability to do that XML<->Object\Record DeCoding or do I have to look for a library that makes it possible?
Note: If there is only a record type capacity, I will use it, but if not, I prefer to use classes
Edit:
In response to some comments on answers, I would say that I use XML instead of JSON because of this Delphi XML library that suited me well! If someone could point me to a goo JSON DeCode library to convert JSON<->Delphi Objects will really use it instead of XML because I like to work with JSON. Would that solve the problem on the PHP parsing?
That is not that easy in PHP. However there are lots of smart folks out there, who where facing the same problem.
Paul Ferrertt has a XML-Encode Class here:
http://www.paulferrett.com/2009/encoding-an-object-in-xml-with-php/
In PHP there are multiple functions to decode a XML sheet. Start here:
http://www.php.net/manual/en/refs.xml.php
http://php.net/manual/en/function.xml-parse.php
However you won' t be able to get an object back as easy as with json_decode() and that for a reason XML is not meant to transfer objects (and the like) around. You have to write your own conversion methods.
I suggest you to read this : http://www.php.net/manual/en/refs.xml.php. Some of these libraries are easier to use that are others, some others are more powerful, etc.

Make GWT interact with REST Architecture in PHP

On my localhost,I have an already made REST Architecture. I need to make a GWT application to interact with the REST Interface.The Architecture returns JSON format data which I need to parse with GWT. Does someone have an idea how to make this?
Edit:
I have a service running at http://localhost/recess/restApp/comment, which returns a JSON like this `{"Action":"GetComments","ActionStatus":1,"CommentIds":["4","5","7"]}, I need to get this,
Normally, GWT runs a java servlet, so how do i change this??
There are two approaches:
AutoBeans
JS Overlay Types
Use JS Overlay Types to make your JS objects available to Java/GWT code, and JsonUtils to parse JSON to your overlay types.
In the event you couldn't use overlay types, have a look at JSONParser (cumbersome API unfortunately). JSONObject is also the only way to serialize an overlay type to JSON (wrap it in a JSONObject then call toString()).
The alternative is AutoBean. The main (if not only) advantage is to make your code reusable in other Java apps (such as an Android client, or in a Java servlet calling your PHP service). Otherwise, it mostly saves you from writing JSNI (on declare methods in an interface and possibly annotate them with #PropertyName)
The answer wouldn't be complete if it didn't mention JsonpRequestBuilder (there's also RequestFactory with JsonRpcService but it's about JSON-RPC –not quite RESTful– and is still experimentatl)
Have you looked at GWTs support for JSON?

Make Zend_Rest_Server return JSON instead of XML using ZF

Can Zend_Rest_Server return Json ?
If not, what are the alternatives ( other lib suggestions are ok also)
Zend_Rest_Server outputs XML, eventhough not clearly specified in the documentation.
There are some alternatives:
You could leverage the whole MVC architecture given by Zend Framework, thanks to Zend_Rest_Route and Zend_Rest_Controller.
You will find a complete example here:
http://techchorus.net/create-restful-applications-using-zend-framework
Again, the example displays plain string, but using json_encode, or Zend_Json::encode should return json.
And finally, a complete (advanced) example that use the ContextSwitch feature:
http://www.chrisdanielson.com/2009/09/02/creating-a-php-rest-api-using-the-zend-framework/
(If you need more information on ContextSwitch Action Helper, please refer to the documentation.)
Unfortunately, my limited reputation prevent me from giving you more links, but Google is your friend using some magic words such as zend rest json or zend controller rest json ...

Serving a json file for IPHONE app

I am trying to get an introduction to serving files to the iphone. I have watched tutorials on getting files from sites like Flickr and twitter. I need a tutorial to show me how to set up the site that is feeding that information. Most of those sites send you a json file. Can I just keep a dynamic file on a server using php?
PHP has some great json functions. See json_encode, which takes a PHP structure and converts it to a json string, and json_decode, which does the opposite. Make sure you set a Content-type: application/json header, echo the result of json_encode, and your iPhone app should be able to read it. Similarly, you can use json_decode to read JSON data that has been sent to your PHP script using GET or POST (preferably the latter).
All those sites use a web service to achieve the json response, depending on what you are using the ability to send back json responses might be built in to the services framework etc (.net for example), here is a question that talks about using .net to make a webservice iPhone interaction with ASP.NET WebService, im sure you can find many more out there
Found this tutorial once i got a better understanding of what i needed to do. http://davidwalsh.name/web-service-php-mysql-xml-json

Categories