GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Saturday, April 16, 2011

Zend - SOAP Web Service

Below is a three file:
1. mywebservie.php - defining class that used in this web service
2. webservice.php - WSDL document and Soap server creation
3. hello.php - a client that request a service from the soap server
You can find nice guide here: http://www.phpriot.com/articles/zend-soap/6



1. mywebservice.php
<?php

/**
 * Web service methods
 */
class MyWebService
{
    /**
     * Get the server date and time
     *
     * @return string
     */
     public function getDate()
     {     
        return date('c');
    }
   
    /**
     * Get a nicely formatted string of a person's age
     *
     * @param string $name The name of a person
     * @param int $age The age of a person
     * @return string
     */
     public function getAgeString($name, $age)
     {
        return sprintf('%s is %d years old', $name, $age);
     }
}

?>

2. webservice.php
<?php

require_once "mywebservice.php";

if(strcmp($_SERVER['QUERY_STRING'],'wsdl') == 0){
    // WSDL Document creation
    require_once "Zend/Soap/AutoDiscover.php";
    $auto = new Zend_Soap_AutoDiscover();
    $auto->setClass('MyWebService');
    $auto->handle();
}else{
    // SOAP Document creation
    require_once "Zend/Soap/Server.php";

    $soapServer = new Zend_Soap_Server("http://localhost/webservice.php?wsdl");
    $soapServer->setClass('MyWebService');
    $soapServer->handle();
}
?>

3. hello.php
================
<?php

require_once "Zend/Soap/Client.php";

$url = "http://localhost/hello.php?wsdl";

$client = new Zend_Soap_Client($url);

echo sprintf("Server Timestamp: %s", $client->getDate());
echo '<br/>';
echo $client->getAgeString('Lady Gaga', 24);
?>
Share/Bookmark

1 comment:

  1. Some additional information about ZF and SOAP you can find in http://plutov.by/post/web_service_soap

    ReplyDelete