Showing posts with label webservices. Show all posts
Showing posts with label webservices. Show all posts
Convert your PHP script to a Web Service
Thursday, January 10, 2008
Web Services are not only for Java and .net people. WSO2 WSF/PHP allows you to easily explore your PHP script as a web service. You may have written a nice PHP script may be to access a MYSQL database. Then why not published it as a web service ? The advantage is using WSO2 WSF/PHP you can explore the script functionality as Web service operations to non PHP clients may be written in .NET, Java or in any other language. Don't worry WSO2 WSF/PHP guarantees the interoperability.
Labels:
Interoperable,
webservices,
WSO WSF/PHP
Make your PHP Webservice available for both REST and SOAP consumers
Tuesday, January 8, 2008
You may have noticed many public web services have deployed their services both as REST services and SOAP services, (e.g. Flickr). You may like to have this feature in your services as well. If you are working with WSO2 Web Service Framework for PHP you don't need to worry about this. Because its engine itself take care of publishing your service both in REST and SOAP form.
In fact one of the indirect advantage, I experienced from this feature is, it allows us to test our Webservice logic in much easier path.
For an example think you are a teacher in a high school and you want to publish examination results through a Webservice.
"http://mysubject.myhighschool.edu/exam.php/getResults?firstName=Hiro (That is add the the "exam.php/getResults?firstName=Hiro" to the URL where you have hosted your service)
If you see something like
<result>A+</result>
you are done. You can continue testing your service by typing several names and observing whether it returns the correct one.
Actually what you have done here is sending a 'REST' request to the service. If this is working fine your logic is correct. And it will work for the 'SOAP' request as well.
So availability of the services both in REST and SOAP form is a cool feature in WSO2 Webservice framework for PHP which is really helpful not only for consumers but also for service developers.
In fact one of the indirect advantage, I experienced from this feature is, it allows us to test our Webservice logic in much easier path.
For an example think you are a teacher in a high school and you want to publish examination results through a Webservice.
1 <?phpSo you are done writing and deploying the service by copying it to the Apache Root directory. But you may feel little lazy to write a simple client to test this service. Although it is very simple to write a client (either SOAP or REST) with WSF/PHP, much easier way is to simply open a browser and type following text in the address bar.
2 /* exam.php: Simple PHP Webservice for publish students marks */
3
4 /* getResults: This is the operation you going to expose as the Webservice operaion */
5 function getResults($message) {
6
7 /* you hard code the results in an array */
8
9 $results_map = array("Hiro" => "A+",
10 "Clair" => "A+",
11 "Peter" => "A+",
12 "Mohinder" => "A+",
13 "Ando" => "A",
14 "Niki" => "B",
15 "Sylar" => "F",
16 /* you are not worrying to put others results, since they all have 'F's */);
17
18 /* extract the firstName using SimpleXML techniques */
19
20 $xml = new SimpleXMLElement($message->str);
21 $firstName = trim($xml->firstName);
22
23
24 /* you look for the result for the firstName */
25 $result = $results_map[firstName];
26 if($result === NULL)
27 {
28 /* sorry I dont have time to give you a good result.. :( */
29 $result = "F";
30 }
31
32 /* build the response xml and return */
33 return "<result>$result</result>";
34
35 }
36
37 /* create a WSService and specify the service operation */
38 $service = new WSService(array("operations" => array("getResults")));
39
40 $service->reply();
41
42 ?>
"http://mysubject.myhighschool.edu/exam.php/getResults?firstName=Hiro (That is add the the "exam.php/getResults?firstName=Hiro" to the URL where you have hosted your service)
If you see something like
<result>A+</result>
you are done. You can continue testing your service by typing several names and observing whether it returns the correct one.
Actually what you have done here is sending a 'REST' request to the service. If this is working fine your logic is correct. And it will work for the 'SOAP' request as well.
So availability of the services both in REST and SOAP form is a cool feature in WSO2 Webservice framework for PHP which is really helpful not only for consumers but also for service developers.
Labels:
REST,
SOAP,
webservices,
WSO2 WSF/PHP
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.
So lets see how the application call the service using this class.
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.
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.
Labels:
Best Practices,
PHP,
webservices,
WSClient,
WSF/PHP
Secure Web services in PHP
Do you want to do secure web services in PHP ? Then no choice other than WSF/PHP . You can taste all the security features in a very cool way.
Labels:
webservices,
WSF/PHP
Encrypting SOAP messages using PHP
Wednesday, January 2, 2008
In this blog I've described how to write a PHP script that uses WSO2 WSF/PHP to do secured web services...
Labels:
Client,
Encryption,
PHP,
Security,
webservices,
WSF/PHP
Subscribe to:
Posts (Atom)