phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP WDDX: Creating Web Service Server

Tag: wddx. distributed data, web service, server   Category: PHP Application
post: 30 Apr 2008 read: 445


Web Distributed Data Exchange Step By Step tutorial - Part 11: We ever talked about web services using nusoap and wsdl. We can build a simple web service using WDDX. In this post, I will show you simple way to build web service at server side that will transfer data to client.

In this practice, we will use database. So create database named "test". Then create table named "books". Here is query:


CREATE TABLE `books` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(150) NOT NULL,
  `author` varchar(150) NOT NULL,
  `description` varchar(255) NOT NULL,
  `on_sale` tinyint(1) NOT NULL,
  PRIMARY KEY  (`id`)
) 

Then, try to insert sample data:


INSERT INTO `books` (`id`, `title`, `author`, `description`, `on_sale`, `cover`) VALUES (1, 'PHP AJAX', 'Andreas', 'This is good book for learning AJAX', 1);
INSERT INTO `books` (`id`, `title`, `author`, `description`, `on_sale`, `cover`) VALUES (2, 'PHP Eclipse ', 'George', 'Nice book', 0);
INSERT INTO `books` (`id`, `title`, `author`, `description`, `on_sale`, `cover`) VALUES (3, 'PHP Pattern', 'Imanda', '-', 1);
INSERT INTO `books` (`id`, `title`, `author`, `description`, `on_sale`, `cover`) VALUES (4, 'PHP Zend Framework', 'Ozulian', 'great', 0);
INSERT INTO `books` (`id`, `title`, `author`, `description`, `on_sale`, `cover`) VALUES (5, 'PHP Web Services', 'Bobi', '', 0);
INSERT INTO `books` (`id`, `title`, `author`, `description`, `on_sale`, `cover`) VALUES (6, 'PHP API', 'Hugo', '', 1);
INSERT INTO `books` (`id`, `title`, `author`, `description`, `on_sale`, `cover`) VALUES (7, 'PHP SEO', 'Monteo', '', 1);
INSERT INTO `books` (`id`, `title`, `author`, `description`, `on_sale`, `cover`) VALUES (8, 'PHP Security', 'Jack Hijack', '', 0);
INSERT INTO `books` (`id`, `title`, `author`, `description`, `on_sale`, `cover`) VALUES (9, 'ZEND FRAMEWORK TUTORIAL', 'PHP Everyday', '', 0);

Now, create a file named "server.php" within www/test/wddx. Enter following code:


<?php
$dbhost = 'localhost';
$dbname = 'test';
$dbuser = 'root';
$dbpass = 'admin';

mysql_connect($dbhost,$dbuser,$dbpass) or die('Connection error');
mysql_select_db($dbname);

$wddxst = wddx_packet_start('Book Library');

$sql = "SELECT id, title, author, description FROM books";
$q   = mysql_query($sql) or die(mysql_error());

$i = 1;
while($r = mysql_fetch_assoc($q)){
  $varname = 'book'.$i;
  $$varname = $r;
  
  wddx_add_vars($wddxst, $varname);
  $i++;
}

$packet = wddx_packet_end($wddxst);

echo $packet;
?>

When we test, we may get:


<wddxPacket version='1.0'><header><comment>Book Library</comment></header><data><struct><var name='book1'><struct><var name='id'><string>1</string></var><var name='title'><string>PHP AJAX</string></var><var name='author'><string>Andreas</string></var><var name='description'><string>This is good book for learning AJAX</string></var></struct></var><var name='book2'><struct><var name='id'><string>2</string></var><var name='title'><string>PHP Eclipse </string></var><var name='author'><string>George</string></var><var name='description'><string>Nice book</string></var></struct></var><var name='book3'><struct><var name='id'><string>3</string></var><var name='title'><string>PHP Pattern</string></var><var name='author'><string>Imanda</string></var><var name='description'><string>-</string></var></struct></var><var name='book4'><struct><var name='id'><string>4</string></var><var name='title'><string>PHP Zend Framework</string></var><var name='author'><string>Ozulian</string></var><var name='description'><string>great</string></var></struct></var><var name='book5'><struct><var name='id'><string>5</string></var><var name='title'><string>PHP Web Services</string></var><var name='author'><string>Bobi</string></var><var name='description'><string></string></var></struct></var><var name='book6'><struct><var name='id'><string>6</string></var><var name='title'><string>PHP API</string></var><var name='author'><string>Hugo</string></var><var name='description'><string></string></var></struct></var><var name='book7'><struct><var name='id'><string>7</string></var><var name='title'><string>PHP SEO</string></var><var name='author'><string>Monteo</string></var><var name='description'><string></string></var></struct></var><var name='book8'><struct><var name='id'><string>8</string></var><var name='title'><string>PHP Security</string></var><var name='author'><string>Jack Hijack</string></var><var name='description'><string></string></var></struct></var><var name='book9'><struct><var name='id'><string>9</string></var><var name='title'><string>ZEND FRAMEWORK TUTORIAL</string></var><var name='author'><string>PHP Everyday</string></var><var name='description'><string></string></var></struct></var></struct></data></wddxPacket>


Series this article:
PHP WDDX: Introduction
PHP WDDX: Understanding Anatomy of WDDX
PHP WDDX: Understanding Data Elements
PHP WDDX: Understanding Simple Data Type
PHP WDDX: Using Array Elements
PHP WDDX: Struct Element
PHP WDDX: Recordset Element
PHP WDDX: Simple Serialization Using PHP function
PHP WDDX: Complex Serialization
PHP WDDX: Unserializing Data
PHP WDDX: Creating Web Service Server
PHP WDDX: Creating Web Service Client

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting