Now you can view the article I wrote titling “Introduction to PHP Data Services“. There I explain how you can design and implement Data Services in PHP using WSF/PHP Data Services Library.
This article covers,
Designing your Data Service API.
Writing the Data Service.
Deploying and Testing Data Service.
Make the Data Service available in both SOAP and RESTful form.
Use of WS-* features in your Data Service.
If you are thinking of adapting SOA in to your database backed PHP applications, this article will be a good starting point.
LAMP (Linux + Apache + Mysql + PHP) stack powers many servers in the Internet today. For a LAMP server, PostgreSQL could be the first alternative to Mysql. Similar to PHP + MySQL, PHP + PostgreSQL too can be easily used in to host data services. Here are the steps to do it.
You have to enable the PHP pdo_pgsql, pdo and pgsql plugins. Read here for the instructions to setup these libraries. (For an example: if you are windows you have to set the system ‘PATH’ variable to the <postgresql_installed_dir>/bin directory.
If you already don’t have WSF/PHP, download and install it according to the guidelines provided in wsf/php installation guide.NOTE: You can check pdo_pgsql and wsf/php has properly installed with the help of phpinfo() function.
Now lets start with creating a sample Database table. For this example I created a database called ‘workshop’, schema called ‘workshop’ and inside there the table ‘Employee’ with the following schema.
Then you can write a small php script to expose the data in the above table as a web service.
<?php//Including the Data Services libraryrequire_once("wso2/DataServices/DataService.php");
// Including the connection information (i.e. PGSQUL USERNAME// and PGSQL_PASSWORD) for my PGSQL Connectionrequire_once("constants.php");
// database configurations$config = array("db" => "pgsql",
"username" => PGSQL_USERNAME,
"password" => PGSQL_PASSWORD,
"dbname" => "workshop",
"dbhost" => "localhost");
$output_format = array("resultElement" => "employees",
"rowElement" => "employee",
"elements" => array("id" => "employeeId",
"name" => "name",
"email" => "email",
"jobTitle" => "jobTitle",
"project" => "project"));
$sql = "SELECT * FROM workshop.Employees";
$get_employees_op = array("outputFormat" => $output_format, "sql" => $sql);
$get_employees_url = array("HTTPMethod" => "GET", "RESTLocation" => "employees");
// list of operations$operations = array("getEmployees" => $get_employees_op,
);
// list of rest url mappping (operation => url)$restmap = array("getEmployees" => $get_employees_url,
);
// creating DSService and reply$service = new DataService(array("config" => $config,
"operations" => $operations, "RESTMapping"=>$restmap));
$service->reply();
?>
We just wrote a PostgreSQL Data Services that provides its service as both REST and SOAP form. To deploy this service, We just need to copy this in to the web root directory. And the web URL for the script will be the endpoint to the web service.
We can test the service either by calling its SOAP interface, which we may need to write a small SOAP client or by calling its REST interface, which only need a GET request from the browser. Say my script name is “my_dataservice.php” and I’ve put it in the web root directory, then the URL to call the REST interface of the service is
http://localhost/my_dataservice.php/employees
WSDL Generation for PostgreSQL Data Service
You can get the WSDL for the service from the URL formed adding the suffix “?wsdl” (or “?wsdl2″ to wsdl v2.0) to the service URL,
http://localhost/my_dataservice.php?wsdl
Here all the schema data types are shown as xsd:anyType, which may not be the behavior that you want. In fact you can provide the schema data types to the fields from the code itself. Lets change the $outputFormat variable to provide the schema information as well using the following code snip.
Note that you provide the xsd type for each field explicitly. In fact this change is not needed for mysql pdo extension since it allows identifying field types programatically. Since this feature is not available in all the other pdo drivers, we have to explicitly give xsd type information for them.
WS-Addressing Action is used by web services to dispatch the operation for an incoming request SOAP message. It is one way of dispatching operations in WSF/PHP and it base Apach Axis2/C, other ways are SOAP action based dispatching which covers in my early blog “The Use of SOAP Action with WSF/PHP“, Body based dispatching and URI based dispatching.
In WSF/PHP client, we can enable the addressing by setting the “action” field in the request WSMessage instance and setting “useWSA”=> TRUE in the WSClient instance. Here is an example.