August 11th, 2008Make your Wordpress Blog a Web Service in Few Steps
With PHP DataServices it is just a matter of putting a little configuration php file to make your database available as a web service. I only needed few minutes to make a simple web service from my blog after figuring out my wordpress database structure, http://wpbits.wordpress.com/2007/08/08/a-look-inside-the-wordpress-database/. In this guide, I m exposing the title, date and the content of each of my blog for my service, But you can extend this more the way you prefer.
- Download and install WSF/PHP 1.3.2 and PHP Data Services Library. If WSF/PHP 2.0 released by the time you are reading this, (it is to be released in this week), you only need to install WSF/PHP, since the DataServices library is packed with WSF/PHP from 2.0.
- Drop the following file (Say WordpressService.php) in to any of your web server document directories.
<?php // Make sure you put the DataService.php in your include path require_once("wso2/DataServices/DataService.php"); // database configuraitons // you have to set your database configurations in here.. // These entries can be copy and past from the wp-config.php in your wordpress installation $config = array( "db" => "mysql", "username"=>DB_USER, "password"=> DB_PASSWORD, "dbname"=>DB_NAME, "dbhost"=>DB_HOST); // output format, plese check the API from http://wso2.org/wiki/display/wsfphp/API+for+Data+Services+Revised $outputFormat = array("resultElement" => "Posts", "rowElement" => "post", "elements" => array( "title" => "post_title", "content" => "post_content", "date" => "post_date")); // sql statment to execute, note that I assume the table prefix is wp_ (so the table name is wp_posts) // just check $table_prefix variable in the wp-config.php of your wordpress installation $sql="select post_title, post_content, post_date from wp_posts where post_status='published'"; // operations is consist of inputFormat (optional), outputFormat(required), sql(sql), input_mapping(optional) $operations = array("getPosts"=>array("outputFormat"=>$outputFormat, "sql"=>$sql)); $my_data_service = new DataService(array("config"=>$config,"operations"=>$operations)); $my_data_service->reply(); ?>
- It is all. Just access the above file from a web browser, you see your service is hosted. Here is the endpoint for my service. http://ws.dimuthu.org/blog/WordpressService.php. Since I m using WSF/PHP latest svn, I m able to retrieve the wsdl automatically from http://ws.dimuthu.org/blog/WordpressService.php?wsdl. Please wait for WSF/PHP 2.0 release for ?wsdl feature.
- To verify whether your service deployed correctly, you may need to write a simple test client. Yea I too wrote one. Since I have the wsdl generated, I just needed to generate the code for the client from the wsdl using wsdl2php tool. There is an online version of the tool in the wsf/php demo site. Here is the generated code for my client, http://labs.wso2.org/wsf/php/wsdl2phptool.php?wsdl_url=http%3A%2F%2Fws.dimuthu.org%2Fblog%2FWordpressService.php%3Fwsdl. I added the following code to the TODO section in handling response,
if(is_array($response->post)) { foreach($response->post as $post) { echo "<h2>".$post->title . " - ".$post->date."</h2>"; echo "<p>"; echo $post->content; echo "</p>"; echo "<hr/>"; } }
Check my Web Service client for the above service here, http://ws.dimuthu.org/blog/WordpressClient.php.
Now I can call for my blog from any web service enabled platform.
![[Ask]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/ask.png)
![[Bloglines]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/bloglines.png)
![[del.icio.us]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/digg.png)
![[diigo]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/diigo.png)
![[dzone]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/google.png)
![[MySpace]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/myspace.png)
![[MyWeb]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/myweb.png)
![[Newsvine]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/newsvine.png)
![[PlugIM]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/plugim.png)
![[Reddit]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/reddit.png)
![[Slashdot]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/slashdot.png)
![[Spurl]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/spurl.png)
![[StumbleUpon]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Twitter]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/twitter.png)
![[Windows Live]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/email.png)