XML to PHP for Soap call - php

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.

Related

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.

Yii SOAP Web Service returning XML

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?

HTTP Post command with XML request (PHP)

I'm working on a system which needs to integrate with an external (commercial) system. On my site, I have some information, and then that needs to be checked against the external system's records. This is done by posting an XML request.
I can't make sense of their documentation. They've given me some example xml, but I just don't know what to do with it. Can anyone help?! I can't find much online that tells me what to do.
I'm using PHP, and the request has to be POSTed via HTTPS. The xml I've been given is (roughly - can't give the full details due to security):
<?xml version="1.0" standalone="no" ?>
<request>
<request_header interface_version_no="0.2" dtd_version_no="0.2">
<client_reference>Test Client</client_reference>
</request_header>
<service_request id="1">
<session_service>
<logon_request domain_id="******" user_id="******" password="******"/>
</session_service>
</service_request>
</request>
The code above should log me in to their system. So, my questions are:
What do I actually do with this? How do I make it into a POST request?
What do I do with the response (which is also xml)? I have parsed xml before (using XSLT), but that was to display it... is there a standard way of doing it in PHP?
Thanks! Any help is appreciated. I can't find much online (well, I can find loads, but none of it seems to be specific to what I'm looking for, so I'm getting confused), so even links to the appropriate help pages would be great.
Sending HTTP request, in PHP, is generally done using the curl extension, which provides a lot of features.
See for example the page of curl_setopt(), which lists all the options that can be used with curl -- there are some in there that should help you, for instance with the POSTing stuff.
Another solution, if allow_url_fopen is enabled on your server, would be to use the stream functionnality of PHP.
You could the use the file_get_contents() function, providing custom context options -- see this page : HTTP context options (the example #1 is interesting, in your case ;-) )
If you get XML as a response, you'll have to parse that result.
This is generally done, in PHP, using either DOMDocument, if you need to modify or manipulate your XML a lot... or with SimpleXML if you just need to read the XML and extract some data from it.
Here, you'll most probably need to use the simplexml_load_string function ;-)

Real time RSS display on web page (best practices and source codes)

i have a php script who parser a rss and give me the data in a know pattern. Im very new with ASP, JavaScript and Jquery so i dont have any idea of how to autoupdate the script and display the new data with a smooth animation (see this example, that exactly what i want). Thanks for the support and if you know a good script to made this i will appreciate it.
Seems like you're looking for this:
http://leftlogic.com/lounge/articles/jquery_spy2/
It's PHP (not ASP), so that might be an issue, though the code is SUPER easy to implement (I've written by own implementation on three separate occasions).
The site itself has some decent documentation on getting things up and running, but if you need some extra help, comment and I'll point you in the right direction :)
Good luck!
The resources people have linked here are helpful and merely mentioning jQuery means you're probably headed in the right direction. But if you're new to this it might still be worth mentioning some of the concepts you'll be looking to play with here.
First of all, you'll probably want to stick with one language on the client side and one on the server side. This means choosing either PHP or ASP -- this isn't clear from your question but I'll assume you're dealing with PHP since that's the language I use for this kind of thing. JavaScript + jQuery is the right choice for the browser (client) side of things.
Like Luca points out, you'll have to set up some JavaScript code that goes live on page load and "polls" the server at a set interval. In JavaScript you do this using something called XMLHttpRequest (or "XHR") and it's pretty complicated. You could use combination of jQuery and a library like the one Matt points to in his answer, or just jQuery -- sample code abounds but it's basically a loop with a function call and sleep timer.
That function call is going to be one of the more difficult parts if you're trying to emulate the Twitter World Cup site. But here's the basic idea: You need to populate a list using jQuery and a data standard like JSON. Since the RSS feed you'll be parsing is written in XML, you'll have to write a server side (PHP/ASP) script that fetches, parses and converts the feed to JSON. In PHP, this is best done through cURL (file_get_contents() if you're lazy), SimpleXML and json_encode(), respectively.
Your JavaScript should load the list based on JSON. To do this, and display any new items, what you'll do is load the JSON from the client (browser) side using a jQuery method like getJSON(). Then you spin through the array object and add any new items to the list by adding new <li> elements to the "DOM." The same jQuery code that does this can easily also do the cross dissolve with something like fadeIn().
It looks like the script on that example page has an Ajax request running every TOT seconds.
You could simply have your PHP script return the RSS data (in JSON format say) and let JavaScript parse it and generate some HTML with it.
If all of this doesn't make sense to you I advice reading a little about JavaScript and PHP... there's plenty of good books.

Multiple REST Feeds to MYSQL Database - Using PHP

I've got a number of REST feeds I'd like to store in a MYSQL database, can anyone suggest a solution for this? Something PHP related appreciated....
It's not PHP related, but PERL has both a REST interface and a DBI interface (for interfacing with MYSQL).
http://metacpan.org/pod/WWW::REST
There are many other REST interfaces for Google, Twitter, etc. Just search CPAN modules at search.cpan.org
To my knowledge there is no such thing as a REST feed. There are RSS feeds and Atom feeds, so I will assume you are talking about one of those.
Both are based on XML so I suggest you find an XML parser for PHP and do an HTTP request to get the feed contents, parse the XML into a DOM and then copy the DOM data into MYSQL!
I'm not sure how to be more precise.
Are you looking for someone to write the code?
Ok, I'm assuming you are talking about "RSS" feeds. Here's a great opensource library that makes it easy -- http://simplepie.org/ . Point it at an RSS or Atom feed, it will give you back PHP arrays and objects. From there you can interpret them and save them any way you want.
Depending on what you actually want to do with the database, you could use RSS as an XML clob format. Not fast, but easy. Again, it totally depends on what you want to do with the database.

Categories