Wednesday, September 09, 2015

What is the difference between SOAP and POX on Web Services

My previous two blog talks about two aspects of Web Services. What is the differences between the two methods. To make it simple, it is basically the same except that one is easier and the other is harder. They both return exactly the same result.

Let's talk about the easier one. Using SOAP is by far the easiest way. You just need to compose an array according to the documentation available for the Web Services and send to a function of the WS object.

The harder one is to compose the XML envelop then add the nodes to make a complete XML document and then send it.

How did the Web Services work with two different methods? Actually they are both the same except that the XML is posted to the second stage. Allow me to explain the process.

When we use SOAP, we creates a connection to the Web Services (henceforth WS). We then called the particular function via the connection object and pass it an array of parameters. In PHP the script is as follows.

$client= new SoapClient("http://webservices_address", $opt);
$param=array('action'=>'theaction');
$test=$client->thefunction($param);
echo $test->functionresponse->result;

Now if the arguments is a multiple array type the $param will be something like this

$param=array('action'=>array('subaction'=>subtheaction'))

You have to know what function to call and what parameter to pass. Normally this is found in WS documentations. Now, it is very important to be specific about the object name of the array. It is often case sensitive. "subaction" is not the same as "subAction". Often WS will simply tells you that a parameter is missing because of case differences.

To a user, the PHP script is all that is needed to get it working. In actual fact PHP works in the back ground and actually posts the data in a different form. You guess it right. It actually posts a XML to WS after composing it from the WSDL that it first addressed to. This means that it actually interprets the returned value when it first posts to WS, compose the XML based on the definition then sends the XML to another address (usually a sub address of the original) according to the definition.

Our second method actually composes the XML directly and posts to the sub address. It is therefore the same thing except you need to compose the XML the hard way unless you download the WSDL of the WS and do a transpose yourself.

How does the multiple array looks in XML? It is exactly the same except in different format.

subtheaction

Obviously, the PHP array is much easier to read. Now, you would say why not just use SOAP since it is much easier? Yes it is unless the application you use does not have SOAP functionality. PHP have both SOAP and CURL (which provide POX functionality by sending the XML as post). If I use PHP I would prefer to use SOAP instead of CURL. What if you use an application for your business but that application only have CURL?

Since CURL can post a XML to the WS and still gets back the same result, it is pretty alright to use it to access WS. It will be more difficult as you will need to understand what to do when composing the entire XML. Some WS does provide the XML information and allows POX.

Filemaker is a good application for a small business. It is very easy to create a complete database application without much technical knowledge. To access to WS, there is no choice but to use CURL. It might be harder but it will still work.

Actually Filemaker can post to a web site and gets the result but then it has to call a PHP script, pass the information to it and it in turn calls WS, get the result and return it to Filemaker. It is pretty round the world way but it still works.

Post note. SOAP communication has been discontinued. The only way is to use POX now.




Sunday, August 30, 2015

How to Access Web Services using SOAP

Previously I mentioned about accessing Web Services in Communicating with Apple Web Services. It communicates directly with the Web Services by posting a XML to it.

My previous post talks about using Filemaker to access the Web Services. Actually you can use a browser to access it. In actual fact, using browser is the more common methods of accessing Web Services. Most browser has Javascript facility. Javascript has a function called XMLHttpRequest that can access web services. This method is usually called AJAX (Asynchronous Javascript And XML). Using this method, you still send the XML to the Web Services.

It is no joke composing the XML. There is an easier way to access Web Services if you have a web site that runs on PHP. PHP has a module that provides SOAP (Simple Object Access Protocol). What you need to do is to call the function and pass it an array of the parameters.

Generally you need to tell the Web Services what to do with the data. But how to know what to send? Well, it is quite simple. Web services usually uses a language called WSDL (Web Services Definition Language.) If you use a web browser and call the address directly like a web page, it actually returns you a XML telling you what functionality it has and what parameters to provide.

Below is a simple PHP program to communicate with a Web Services. It calls a function called "thefunction" and passes a parameter called $param. It echoes a node name called "result" in the returned XML.

$opt= array('encoding'=>'UTF-8');
$client= new SoapClient("http://webservices_address", $opt);
$param=array('action'=>'theaction');
$test=$client->thefunction($param);
echo $test->functionresponse->result;

Nested parameters can be done by adding arrays in an array like below

array('higherlevel'=>array('lowerlevel'=>'xxx','lowerlevel2=>'yyy'))

Note that php actually parses the returned xml automatically into an object array. You need to use the object reference method to access individual values. Converting the object to other form is beyond the scope of this blog.

There are some Web Services that require client cert. This can be easily done by adding "'local_cert=>'certname.pem'" to the variable $opt. The cert and key must be in the same file.






Saturday, July 04, 2015

A case of two cyclist again

Was cycling on Upper Thomson Road towards the East. This cyclist on a MTB was cycling on foot path. I am cycling on the road. I am travelling at 28-30KPH. Overtook him after SLE. Did not notice till I slowed down at a slope. He was trying to catch up.

He was on a MTB and on footpath. I was on a hybrid and is on road. There is no way he could catch up as it is impossible to go fast on footpaths. Unlike the previous case, I have no hindrances to slow me down. He has plenty. Anyhow, he could not play tag with me as I am simply too fast.

There is only one instance where he actually catches up. After I am aware of his efforts. I simply keep at 30KPH and he could not even be seen. He finally managed to catch up when I stopped at traffic lights at Yio Chu Kang Rd. I don't know whether he turned off or stopped after that. As it is down slopes after the junction, I go easily at 32-36KPH till Ang Mo Kio Ave 1 where I slow down and turned into it.

Cycling on footpath at over 20KPH in the early morning is dangerous as the footpath is not designed for cycling and visibility is poor. Even cycling on PCN has its many dangers already. I do not want to encourage him to speed so I make it impossible for him to catch up rather than allow him to play tag with me at my my usual slower pace.



Saturday, June 20, 2015

A Case of two Cyclists on PCN

The scene started at the junction of Hougang Ave 7 and Tampines Rd. I stopped at the road junction. This guy with a Dahon came up besides me. The first thing he looked at is my bike. Well, nothing to look at. I am having a cheapo bike.

At the start of the Serangoon PC, he starts to overtake me. Looking at the speed he is going, he probably travel at well above 25KPH. Since the PC is quite empty, I start to give chase. After a while I caught up with him and noticed that he travels at 30KPH.

It is not normal I go at the same speed when there are other users on the road so I slowed down whenever there are pedestrians along the way then speed up to catch up.

He is keenly aware of my presence and keeps looking back at me when I am very near him. Interestingly, he did not try to shake me off. I never have intention to overtake so I just keep a short distance behind him. So we keep playing tag on and off till Ponggol way via PPN PC.

I had to stop at the street crossing for safety reason. He just crossed it without even pausing. On Serangoon Way, he did the same. Since I am going onto Serangoon Way, I do not intend to keep up again. We parted way from there.

He is young and on a branded fast bike. I am old with a average hybrid bike and a bag of tools. There is no way I could overtake him and keep it that way. It is simply too dangerous to race on a PC. I just want to see what he can do with a nice bike like that since he have an "atas" look when he first encounters me.



Wednesday, June 10, 2015

Insects mimicking Preditors

Often you see pictures of insects or animals that looks like predators. They have big spots that looked like eyes, shape of snake, etc. Do they intentionally  evolved to look like one?

My hypothesis is like this. In the process of evolution, the specie have developed with features that resembles predators. Since other animals are afraid of predators, their chance of survival gets better thus they multiply.

As evolution goes, it developed further and those that by chance enhances the feature get better chance of survival and passed down the gene.

Through many generations, the selective survival of those who evolved towards predatory look makes the animal looked more like the predator and today we see the result of the survived specie.

The evolution theory of "survivor of the fittest" does not means that only the strong survived. It actually means that the specie is able to survive by their strength, ability, features or properties. It is the ability to avoid being eliminated.



Monday, May 25, 2015

Setting and resetting Default Apps

Was playing with a map app. The map application offers to take pictures. When the option is clicked, it prompts to choose which camera app to use. Being curious, I select "always use" option and did not notice the prompt after that.

I tried to go through all the menus and settings but just could not find the setting to unset the default app. Going to the internet to search for "unset default app" for the map app does not help. It did not have anything that is related to resetting the default.

Going on a hunch, I tried looking for "Android reset default app". There are a lot of people talking about it. I hit jackpot.

Unlike windows programs where all the settings are within the program itself, Android have different ways of doing things. You can set the default app in another app but to reset it, you have to go back to "settings", "application management" and select the application to reset. Just click on the app and scroll down. You will find a "reset default" button. Choose it and the work is done.

You may ask "what about other apps which I want to keep the default camera setting". Well, you are out of luck.



Sunday, May 24, 2015

Simple Restful Web Services

I was trying to use PHP CURL library to access a web services. In the process I found a useful library that makes using CURL very easy. It is called Httpful (https://github.com/nategood/httpful). The library is useful if you don't mind the many duplicate functionality.

In the process of testing, I was wondering whether CURL actually posts the XML. Since I did not want to access any real time web services, I decided to use PHP to get the posting. Naturally it failed. The standard global variables $_POST[] does not contain any content neither does $_RESPONSE[]. I search the internet and discovered that the post is more like posting a file rather than posting a form. There is a PHP function to get this kind of content.

After successfully retrieving the content, I was wondering whether I can make this functionality work like a very simple restful web services. I began to write a proper PHP page to simulate the web service. The following are the scripts for posting and the restful web services page. The XML request and response are dummy values which is stored as a simple variable, It is not in the scope of this blog to show how to create XML in PHP.

The posting page :-

include "httpful.phar";
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
$url="http://localhost/sampleresponse.php";
$authenticate = "jonathan@mail.commypassword";

$response = \Httpful\Request::Post($url)
    ->body($authenticate)
    ->mime("XML")
    ->autoParse(false)
    ->expectsType("XML")
    ->send();
$xml= new SimpleXMLElement($response);
$sessionid=$xml->xpath("//userSessionId")[0];
if ($sessionid==""){
    die("Error User does not exist or password is wrong.");
}

The restful response :-

include "httpful.phar";
$postText = file_get_contents('php://input');
$xml= new SimpleXMLElement($postText);
$auth=$xml->xpath("//AuthenticateRequest")[0];
if ($auth){
$user=$xml->xpath("//userId")[0];
.
.
.
if ($verified) {
    die("
             Sdt7tXp2XytTEVwHBeDx6lHTXI3w9s+M
       
");}};

Well, it is simple straightforward PHP turned into restful web services. Obviously CURL could post user credentials itself while posting the request. This is a simple example.

Not everyone have a web server. In the case of user just want to access the restful web services via the browser, they could simply write a Javascript AJAX function to do the posting. There are plenty of examples in the internet that shows you how to do posting in AJAX. It will not show it here.

For more security, it is best to use HTTPS for the web services.