Services_Webservice

Avatar von manfred

Services_Webservice finally made it into PEAR. Phillipe Jausions and Matthew Fonda are now contributing to the project.

We already released the first alpha version and ask everyone to download and test it.

The package will/should do all the annoying stuff that comes to you when creating webservices, like:

  • wsdl (webservice description language) creation
  • disco (discovery) creation
  • instantiating a php SoapServer
  • infopage of the webservice

To see what the package does for you, have a quick look here.

If you are interested have a look at the examples I prepared.

1) Simple example creates wsdl, disco, infopage and soapserver.

include_once('Services/Webservice.php');

class myService extends Services_Webservice
{
    /**
    * Says "Hello!"
    *
    * @param int
    * @return string
    */
    public function hello($i )
    {
        //create some logic here
        return 'myString';
    }
}

$myService = new myService(
        'myService',
        'example webservice description',
        array('uri' => 'myService', 'encoding' => SOAP_ENCODED,'soap_version' => SOAP_1_2)
        );

$myService->handle();

 

2) A more advanced service with support for complex types

include_once('Services/Webservice.php');

class classB
{
    /**
    * @var string
    */
    public $c;
    public function __construct($c)
    {
        $this->c=$c;
    }
}

class myService extends Services_Webservice
{
    /**
    * Says "Hello!"
    *
    * @param int
    * @param string
    * @return classB
    */
    public function hello($i, $j )
    {
        //create some logic here
        return new SoapVar(new ClassB('myString'),
            SOAP_ENC_OBJECT,
            'classB',
            'urn:myService');
    }
}

$myService = new myService(
        'myService',
        'example webservice description',
        array('uri' => 'myService', 'encoding' => SOAP_ENCODED,'soap_version' => SOAP_1_2)
        );

$myService->handle();

 

3) Using arrays

include_once('Services/Webservice.php');

class myService extends Services_Webservice
{
    /**
    * Says "Hello!"
    *
    * @param int[]
    * @return string[]
    */
    public function hello($i)
    {
        $strArray = array();
        $strArray[] = $i[0].'a';
        $strArray[] = $i[1].'b';
        $strArray[] = $i[2].'c';
        $strArray[] = $i[3].'d';
        $strArray[] = $i[4].'e';
        return $strArray;
    }

}

$myService = new myService(
        'myService',
        'example webservice description',
        array('uri' => 'myService', 'encoding' => SOAP_ENCODED,'soap_version' => SOAP_1_2)
        );

$myService->handle();

 

4) Using arrays, complex types

include_once('Services/Webservice.php');

class classB
{
    /**
    * @var int[]
    */
    public $i;

    public function __construct()
    {
        $this->i = array(1,2,3,4,5);
    }
}

class myService extends Services_Webservice
{
    /**
    * Says "Hello!"
    *
    * @return classB[]
    */
    public function hello()
    {
        //create some business logic here
        $classB[] = new SoapVar(new ClassB(),SOAP_ENC_OBJECT,'classB','urn:myService');
        return $classB;
    }
}

$myService = new myService(
        'myService',
        'example webservice description',
        array('uri' => 'myService', 'encoding' => SOAP_ENCODED,'soap_version' => SOAP_1_2)
        );

$myService->handle();

 

Please do not expect from the package to work properly yet. The wsdl-creating is quite stable.
While doing some extensive test with .net-clients I discovered the wsdl is also used for typemapping the
soap-messages. So the whole wsdl had to be recreated.

I am waiting for your feedback!

cvs source tree

Manfred <crafics@php.net>

Avatar von manfred

Kommentare

7 Antworten zu „Services_Webservice“

  1. Hi,

    Been playing with it today. I just noticed that when you use a complex type, like classB in your example, you will end up with a fatal error. The problem is that you call ReflectionProperty::getDocComment() which was only added in PHP 5.1.0. I’m on 5.0.4 :/.

    Might wanna add a notice on that.

    Besides that, working nicely so far.

    Thanks,

    Mathieu

  2. Avatar von Manfred

    Yes, this extension works with PHP 5.1 only. :-) But you`re right, we should redefine all those errors, warnings,…

  3. Hi, I’m new to web services so please bare with me ;). Why would one not use PHP5’s built-in soap server instead of this PEAR version you are using?

    1. Hi Dean,
      you can use the php5 soap server extension of course. but it won`t create wsdl, disco and infopage for you. the pear version is a kinda layer above that c-extension which offers you all the comfort you`d expect when creating webservice. a good way to learn more about webservice with php is to start with the ‚built-in‘ extension and then go further with the pear-version.
      – manfred

  4. Something to look out for: If you’re working with C# (mono) and a PHP webservice be sure to cast your data to the exact type you’re defining it to be.

    I had an issue where mono threw a hissy fit whenever I tried returning an array of objects containing only and ID (defined in the doc comment as an integer) and a Name. When I assigned the ID from a database record, the ID became a string and the webservice sent it along as being a string. The problem went away as soon as I casted it to an int.

    Perhaps it might be usefull to raise a warning whenever the type of a variable you’re trying to send doesn’t match the type you’ve defined in your doc comment?

  5. I’m trying to communicate with C#
    as my client. When I try to return an array I get this error:

    The specified type was not recognized: name=’Struct‘, namespace=’http://schemas.xmlsoap.org/soap/encoding/‘, at .

    But communocate with PHP SOAP client work good.

  6. Avatar von ZhongWei

    It’s so good a WebServices framework for PHP 5. I’d do some tests on your code. An question, for the example 2, it can get returned value from an class instance via webservice. Is it any possible to execute an method in a class instance which get via webservice?

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert


Für das Handling unseres Newsletters nutzen wir den Dienst HubSpot. Mehr Informationen, insbesondere auch zu Deinem Widerrufsrecht, kannst Du jederzeit unserer Datenschutzerklärung entnehmen.