a complete nusoap and flex example. part 1: the nusoap server

May 30th, 2010 § 3

This is a complete example of how to create a web service for an address book in PHP using NuSOAP and then connect to it from a FLEX application in order to perform basic CRUD operations.

The post got long so I split it in two. This first part here describes the soap server and part two the flex application.

Check out the NuSOAP web service output, grab the files and take a look at the final application.

Note: PHP5 has built in soap which is better in many ways but there are valid reasons to use NuSOAP, automatic WSDL generation and pretty browsable documentation being two of them. Remember that if you wish to use NuSOAPs soap client in a PHP5 environment you should rename the class and function to something else, or get the PHP5 compatible version of NuSOAP.

In the code below, we first create the server instance, then define two new data structures (contact and addressbook) and finally register our methods with the server. The last two lines try to invoke the service and server the request.

$server = new soap_server();
$server->configureWSDL('addressbook_wsdl', 'urn:addressbook_wsdl');
$server->soap_defencoding = 'UTF-8';

$server->wsdl->addComplexType('Contact',
    'complexType',
    'struct',
    'all',
    '',
    array(
            'id' => array('name' => 'id', 'type' => 'xsd:int'),
            'first_name' => array('name' => 'first_name', 'type' => 'xsd:string'),
            'last_name' => array('name' => 'last_name', 'type' => 'xsd:string'),
            'email' => array('name' => 'email', 'type' => 'xsd:string'),
            'phone_number' => array('name' => 'phone_number', 'type' => 'xsd:string')
    )
);
$server->wsdl->addComplexType('AddressBook',
    'complexType',
    'array',
    '',
    'SOAP-ENC:Array',
    array(),
    array(
            array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:Contact[]')
    ),
    'tns:Contact'
);
$server->register('searchAddressBook',
    array('query' => 'xsd:string'),
    array('return' => 'tns:AddressBook'),
    'addressbook_wsdl',
    'addressbook_wsdl#search',
    'rpc',
    'encoded',
    'Returns matching contacts'
);
$server->register('updateContact',
    array('new_contact' => 'tns:Contact'),
    array('return' => 'xsd:boolean'),
    'addressbook_wsdl',
    'addressbook_wsdl#updateContact',
    'rpc',
    'encoded',
    'Update contact'
);
$server->register('deleteContact',
    array('new_contact' => 'tns:Contact'),
    array('return' => 'xsd:boolean'),
    'addressbook_wsdl',
    'addressbook_wsdl#deleteContact',
    'rpc',
    'encoded',
    'Delete contact'
);
$server->register('insertContact',
    array('new_contact' => 'tns:Contact'),
    array('return' => 'xsd:boolean'),
    'addressbook_wsdl',
    'addressbook_wsdl#insertContact',
    'rpc',
    'encoded',
    'Insert contact'
);
$request = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($request);

The most important thing here are the complex types. NuSOAPĀ  uses associative arrays to represent SOAP structs. The Contact type is defined as being of type “complexType” with php type “struct”. The struct (associative array) is then defined with the fields that we require. We want the AddressBook type to contain an array of Contacts. To do that we define the attributes and the array type.

Check out this and this for a more detailed explanation of what is happening.

Next we define the Contact class in PHP:

class Contact {
    private $id;
    public $first_name;
    public $last_name;
    public $email;
    public $phone_number;

    function __construct($id) {
        $this->id = $id;
    }

    public function get() {
        return $this->id;
    }

    public static function search($query="") {
        if ($query!="") $where = "  WHERE first_name LIKE '%".mysql_real_escape_string($query)."%'
                                    OR last_name LIKE '%".mysql_real_escape_string($query)."%'";
        else $where ="";
        $res = mysql_query("select * from contacts ".$where);
        $contacts = array();
        while ($row=mysql_fetch_assoc($res)) {
            $contact = new Contact($row['id']);
            $contact->first_name=$row['first_name'];
            $contact->last_name=$row['last_name'];
            $contact->email=$row['email'];
            $contact->phone_number=$row['phone_number'];

            array_push($contacts,$contact);
        }
        return $contacts;
    }

    public function update() {
        $query = "  UPDATE contacts
                    SET first_name='".$this->first_name."', last_name='".$this->last_name."', email='".$this->email."', phone_number='".$this->phone_number."'
                    WHERE id='".$this->id."'";

        if ($res = mysql_query($query)) return true;
        else return false;
    }

    public function delete() {
        $query = "  DELETE FROM contacts
                    WHERE id='".$this->id."'";
        if ($res = mysql_query($query)) return true;
        else return false;
    }

    public function insert() {
        $query = "  INSERT INTO contacts VALUES
                    (null,
                     '".$this->first_name."',
                     '".$this->last_name."',
                     '".$this->email."',
                     '".$this->phone_number."')";
        if ($res = mysql_query($query)) return true;
        else return false;
    }
}

Finally we define the service methods as PHP functions. The search function outputs an array of Contacts as defined in the soap server. The rest of functions have a Contact as input.

function searchAddressBook($query) {
    $contacts = Contact::search($query);
    //return $contacts;
    $results = array();
    foreach ($contacts as $contact) {
            $tempArray = array( 'id' => $contact->get(),
                                'first_name' => $contact->first_name,
                                'last_name' => $contact->last_name,
                                'email' => $contact->email,
                                'phone_number' => $contact->phone_number
                                            );
            array_push($results, $tempArray);
    }

    return $results;
}

function deleteContact($in_contact) {
    $contact = new Contact($in_contact['id']);
    return $contact->delete();
}

function insertContact($in_contact) {
    $contact = new Contact(0);
    $contact->first_name=mysql_real_escape_string($in_contact['first_name']);
    $contact->last_name=mysql_real_escape_string($in_contact['last_name']);
    $contact->email=mysql_real_escape_string($in_contact['email']);
    $contact->phone_number=mysql_real_escape_string($in_contact['phone_number']);
    if ($contact->insert()) return true;
}
function updateContact($in_contact) {
    $contact = new Contact($in_contact['id']);
    $contact->first_name=mysql_real_escape_string($in_contact['first_name']);
    $contact->last_name=mysql_real_escape_string($in_contact['last_name']);
    $contact->email=mysql_real_escape_string($in_contact['email']);
    $contact->phone_number=mysql_real_escape_string($in_contact['phone_number']);
    if ($contact->update()) return true;
}

Check out the created WSDL and the service documentation

Continue to Part 2 for the FLEX application

Tagged: , , ,

§ 3 Responses to “a complete nusoap and flex example. part 1: the nusoap server”

What's this?

You are currently reading a complete nusoap and flex example. part 1: the nusoap server at Discorganized.

meta

Partly powered by CleverPlugins.com