Showing posts with label Best Practices. Show all posts
Showing posts with label Best Practices. Show all posts

Debugging and Troubleshooting PHP Services and Clients

Wednesday, January 9, 2008

Users sometimes would run into trouble at two stages when getting the PHP Web services extension working.

First the challenge is to get it installed. How can you check if it is working? Well, first, you can check with the phpinfo() function. If the extension is correctly installed, you will get an entry for the extension, with the name wsf. And a table following the wsf  title, you would see a table with 'wsf support' 'enabled' entries. That will be followed by the table displaying hte php.ini directives for WSO2 WSF/PHP extension. If this entry is missing form the phpinfo() page, that means the extension is not properly loaded.

If the extension does not seem to appear with phpinfo(), you can double check your PATH variable to verify that the required dlls are on the PATH as mentioned by the install guide. More often than not, the extension fails to load due to PATH problems, when it cannot find the required libraries.

When you get it installed, the second form of problems come when you cannot seem to get the samples working. Again there are some settings to double check. First the php.ini entries. Often, users happen to enter setting where the file locations, such as login path, pointed to by the php.ini entries are either non existent or do not have write access. Also check if your include_path setting in the php.ini file includes the scripts folder that comes with the PHP distribution.

When all the above are correct and still things are not working, you can turn to log files written by the Web services extension. Logs are written into the folder pointed by the wsf.log_path entry that you add to the php.ini file. If this is not set, the default location is /tmp. Note that, on Microsoft Windows, the default folder may not exist, hence you must set the wsf.log_path setting in php.ini. Logs related to the services are written to a file named wsf_php_server.log and client logs are written to a file named wsf_php_client.log. You can vary the level of information written to the logs by changing the wsf.log_level entry that you add to the php.ini file.

In case you want to verify that the messages are sent and received by the clients and services and that those messages are of the desired format, you can use the SOAP message tracing techniques that I discussed yesterday.

The Best Practices of using WSClient

Monday, January 7, 2008

If you are accessing Webservice in your application, the best practice is to delegate your Webservice accessing aspects to a separate class. This way you can hide the Webservice consumer layer from the rest of the application.

When you are using WSClient in WSO2 Web Service Framework for PHP, you can do this by defining a separate class which extend the WSClient. For an example think about a simple Webservice for ScoreBoard which gives the total score of a Cricket match. Then you will write a PHP class which completely interact with the Service.


  1 <?php
2
3 class ScoreBoardClient extends WSClient
4 {
5 // Constructor of the new Client
6 public function __construct() {
7 parent::__construct(array( "to" => "scoreboard.sports.org"));
8 }
9
10 // The service operation of getting Total Score
11 // You can provide the match number and the team name
12 public function getTotalScore($match_no, $team) {
13
14 $xml = <<< XML
15 <GetTotalScore>
16 <match>{$match_no}</match>
17 <team>{$team}</team>
18 </GetTotalScore>
19 XML;
20 try
21 {
22 $res = $this->request($xml);
23 }
24 catch(Exception $e)
25 {
26 // Handle the exception..
27 return -1;
28 }
29 // For the clarity I ignore the response xml processing part
30 return $res->str;
31
32 }

33 }
34
35 ?>


  • The PHP Class: (lines 3 -33) - The ScoreBoarClient PHP class which extends the WSClient.
  • The constructor: (lines 6-8) - This calls the WSClient constructor (the parent of the ScoreBoardClient class) with options, in this case only the service endpoint. If you are intending to use WS-Security, you may initialize the service policies here.
  • The Service operation wrapper (lines 12-32) - This is where you do all the Webservice accesing stuffs like building the request payload(lines 14-19), calling the remote service (lines22), and return the result to the caller.

So lets see how the application call the service using this class.


01 $my_scoreboard = new ScoreBoardClient();
02
03 $total_score = $my_scoreboard->getTotalScore(5, "Sri Lanka");
04
05 echo "Total Score is: $total_score \n";
06


It s simple as we are calling another local function. If you like to see more example which follow this pattern, just go to the script/wso2 directory of the WSO2 WSF/PHP distribution and observe how amazon, flickr and yahoo webservices are wrapped with PHP Classes.