Services_Webservice

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
  • nstantiating a php SoapServer
  • infopage of the webservice

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

    PHP:
    1. include_once('Services/Webservice.php');
    2.     class myService extends Services_Webservice
    3.     {
    4.         /**
    5.         * Says "Hello!"
    6.         *
    7.         * @param int
    8.         * @return string
    9.         */
    10.         public function hello($i )
    11.         {
    12.             //create some logic here
    13.             return 'myString';
    14.         }
    15.     }
    16.  
    17.     $myService = new myService(
    18.             'myService',
    19.             'example webservice description',
    20.             array('uri' => 'myService', 'encoding' => SOAP_ENCODED,'soap_version' => SOAP_1_2)
    21.             );
    22.     $myService->handle();

    2) A more advanced service with support for complex types. Watch live !

    PHP:
    1. include_once('Services/Webservice.php');
    2.     class classB
    3.     {
    4.         /**
    5.         * @var string
    6.         */
    7.         public $c;
    8.  
    9.         public function __construct($c)
    10.         {
    11.             $this->c=$c;
    12.         }
    13.     }
    14.  
    15.     class myService extends Services_Webservice
    16.     {
    17.         /**
    18.         * Says "Hello!"
    19.         *
    20.         * @param int
    21.         * @param string
    22.         * @return classB
    23.         */
    24.         public function hello($i, $j )
    25.         {
    26.             //create some logic here
    27.             return new SoapVar(new ClassB('myString'),
    28.                 SOAP_ENC_OBJECT,
    29.                 'classB',
    30.                 'urn:myService');
    31.         }
    32.     }
    33.  
    34.     $myService = new myService(
    35.             'myService',
    36.             'example webservice description',
    37.             array('uri' => 'myService', 'encoding' => SOAP_ENCODED,'soap_version' => SOAP_1_2)
    38.             );
    39.     $myService->handle();

    3) Using arrays. Watch live !

    PHP:
    1. include_once('Services/Webservice.php');
    2.     class myService extends Services_Webservice
    3.     {
    4.         /**
    5.         * Says "Hello!"
    6.         *
    7.         * @param int[]
    8.         * @return string[]
    9.         */
    10.         public function hello($i)
    11.         {
    12.             $strArray = array();
    13.             $strArray[] = $i[0].'a';
    14.             $strArray[] = $i[1].'b';
    15.             $strArray[] = $i[2].'c';
    16.             $strArray[] = $i[3].'d';
    17.             $strArray[] = $i[4].'e';
    18.             return $strArray;
    19.         }
    20.     }
    21.  
    22.     $myService = new myService(
    23.             'myService',
    24.             'example webservice description',
    25.             array('uri' => 'myService', 'encoding' => SOAP_ENCODED,'soap_version' => SOAP_1_2)
    26.             );
    27.     $myService->handle();

    4) Using arrays, complex types. Watch live !

    PHP:
    1. include_once('Services/Webservice.php');
    2.     class classB
    3.     {
    4.         /**
    5.         * @var int[]
    6.         */
    7.         public $i;
    8.  
    9.         public function __construct()
    10.         {
    11.             $this->i = array(1,2,3,4,5);
    12.         }
    13.     }
    14.     class myService extends Services_Webservice
    15.     {
    16.  
    17.         /**
    18.         * Says "Hello!"
    19.         *
    20.         * @return classB[]
    21.         */
    22.         public function hello()
    23.         {
    24.             //create some business logic here
    25.             $classB[] = new SoapVar(new ClassB(),SOAP_ENC_OBJECT,'classB','urn:myService');
    26.  
    27.             return $classB;
    28.         }
    29.     }
    30.     $myService = new myService(
    31.             'myService',
    32.             'example webservice description',
    33.             array('uri' => 'myService', 'encoding' => SOAP_ENCODED,'soap_version' => SOAP_1_2)
    34.             );
    35.  
    36.     $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.

  • 18 Comments

    1. markus.zierhut.name » Blog Archiv » Web Services mit PHP 2006-07-25, 5:57 pm

      Gravatar

      [...] Unser Portal wurde in PHP entwickelt, weshalb ich auch den Web Service damit entwickeln wollte. Um die Arbeit etwas zu erleichtern, machte ich mich auf die Suche nach einem bestehenden Framework für die Entwicklung von Web Services. Fündig wurde ich im PEAR (PHP Extension and Application Repository), nämlich mit dem Paket Services_Webservice. Damit läßt sich tatsächlich relativ leicht und bequem ein Web Service mit allem Drumherum (u.a. WSDL, DISCO) automatisch erzeugen. Hilfreich waren hierzu auch die Informationen aus “Manfred’s Diary“. [...]

    2. Services_Webservice Beta! » Manfred`s Diary 2006-12-28, 12:03 pm

      Gravatar

      [...] I found some time to reactivate Services_Webservice and release the beta version. This package has been in alpha state since more then a year. You can download the version on the pear website. Additional information about the package are available here. Thanks also go to David. [...]

    3. dsp 2006-12-30, 4:35 pm

      Gravatar

      Finally it’s beta :). I hope I find the time to have a closer look, it’s definatly worth. Thanks for the great work. Maybe somedays there will be a stable release.

    4. Sharing complex objects between PHP and Flash - Part II » Manfred`s Diary 2007-02-23, 9:23 pm

      Gravatar

      [...] More example about Services_Webservice [...]

    5. Lawrence R. Steeger 2007-02-27, 7:53 pm

      Gravatar

      Service_Webservice was exactly what I was looking for to create webservices easily. Thanks!

      I found that the Webservice constructor was generating several Apache error log waring/error messages because is was invoked without parameters whenever a webservice method was being invoked. The following constructor eliminated all of the Apache error log warning/error messages:

      /**
      * constructor
      *
      * @var string
      * @var string
      * @var array
      * @access public
      */
      public function __construct()
      {
      $num_args = func_num_args();

      if ($num_args > 0) {
      //
      // arguments: namespace, description, options
      //
      $namespace = func_get_arg(0);

      if (isset($namespace) && ($namespace != ”)) {
      $this->warningNamespace = false;
      $this->namespace = $namespace;
      }
      else {
      $this->warningNamespace = true;
      $this->namespace = ‘http://example.org/’;
      }

      if ($num_args > 1) {
      $description = func_get_arg(1);

      if (isset($description) && ($description != ”)) {
      $this->errorDescription = false;
      $this->description = $description;
      }
      else {
      $this->errorDescription = true;
      $this->description = ‘my example service description’;
      }

      if ($num_args > 2) {
      $options = func_get_arg(2);

      if (isset($options) && is_array($options) && !empty($options)) {
      $this->soapServerOptions = $options;
      }
      else {
      $this->soapServerOptions = array(’uri’ => $this->namespace, ‘encoding’ => SOAP_ENCODED);
      }
      }
      }
      }

      $this->wsdlStruct = array();
      $this->preventMethods = array(’__construct’, ‘__destruct’, ‘handle’);
      $this->protocol = ‘http’;
      }

      Feel free to use this code.
      Again, thanks!

    6. Manfred Weber 2007-03-2, 1:21 pm

      Gravatar

      Lawrence, thanks so much. I am going to implement your fix in the package as soon as possible. I did not see there were errors generated in the Apache logs.

    7. Michael Kunze 2007-03-9, 6:43 pm

      Gravatar

      Got warnings and a fatal error using this class with php 5.2.1:

      [Fri Mar 09 18:36:20 2007] [error] [client 192.168.100.21] PHP Warning: strpos(): Offset not contained in string. in /usr/share/php
      5/PEAR/Services/Webservice.php on line 580
      [Fri Mar 09 18:36:20 2007] [error] [client 192.168.100.21] PHP Notice: Undefined offset: 0 in /usr/share/php5/PEAR/Services/Webser
      vice.php on line 587
      [Fri Mar 09 18:36:20 2007] [error] [client 192.168.100.21] PHP Fatal error: Uncaught exception ‘ReflectionException’ with message ‘
      Class does not exist’ in /usr/share/php5/PEAR/Services/Webservice.php:604\nStack trace:\n#0 /usr/share/php5/PEAR/Services/Webservic
      e.php(604): ReflectionClass->__construct(”)\n#1 /usr/share/php5/PEAR/Services/Webservice.php(482): Services_Webservice->classMethod
      sIntoStruct()\n#2 /usr/share/php5/PEAR/Services/Webservice.php(282): Services_Webservice->intoStruct()\n#3 /web/pegasus/ws/test_01.p
      hp(24): Services_Webservice->handle()\n#4 {main}\n thrown in /usr/share/php5/PEAR/Services/Webservice.php on line 604

      Any idea what’s going on here?

    8. Manfred Weber 2007-03-9, 8:04 pm

      Gravatar

      Michael, it seems the ReflectionAPI cannot find a class that got found in the doc-comments. Either the class is not included or does not exists. Can you check it again? Maybe I should add some more information errors!

    9. Michael Kunze 2007-03-16, 4:40 pm

      Gravatar

      I’ll try it again here because it seems that you don’t check your gmail account…;)..

      With PHP 5.1.6 (Windows), everything works fine. I’m quite sure that the configuration of the platforms is identical. so I reckon the error has something to do with PHP 5.2.1

    10. Manfred Weber 2007-03-18, 10:46 am

      Gravatar

      Micha, hmmm … I don´t know. I remember there was a bug in 5.1.2 but this is new to me. Still it looks like some problem in Userland. Can you send me the sources please?

    11. Ronan 2007-03-23, 2:21 pm

      Gravatar

      Is there a new version available? I’m having some problems using a string that’s 10M (in an object). Should __construct not be SoapServer for PHP5 ?

    12. Manfred 2007-03-23, 2:32 pm

      Gravatar

      Ronan,
      no there is no new version availbale. Simply I think everything works like it should. I did not test it yet with 10M files. But still that should work as long there is no memory_limit or apache timeout.
      __construct(?): yes SoapServer::__construct() instantiates a SoapServer !?! What exact problem are you talking about? Best Regards. Manfred

    13. Donald 2007-06-27, 7:34 pm

      Gravatar

      I have a webservice setup and when going to the page with the webservice everything works fine and invoking one method works while trying to in voke another doesnt, and even if i just have the method return a string, and do nothing else, i still get “Function (”processUpdate”) is not a valid method for this service” If you could please email me back so I can futher discuss this with you that would be great.

    14. Aristos 2007-06-28, 11:37 pm

      Gravatar

      Very good article, well done mate..

    15. Nico 2007-08-28, 11:36 am

      Gravatar

      Hi!

      Thanks, your wsdl creation saved my day :)
      One thing I noticed:

      you wrap the SoapServer as follows:
      $server = new SoapServer(null, $this->soapServerOptions);

      Since you are using non-wsdl mode here, it seems that a php-client (using SoapClient) cannot use the classmap feature anymore, because the returned xml data does not contain the right types. As a result anything gets mapped to stdClass.

      greetings,

      Nico

    16. Donald 2007-09-26, 3:53 pm

      Gravatar

      Hello Menfred how are you, you helped me a little while back and was wondering if you could help me one more time.

      I have the following in my webservice

      I have two objects a Project object and a Plate object, the plate object is defined below as there is where I am believe everything is stemming from.

      The Project object has a property defined in the following way:
      /**
      * @var Plate[]
      */
      var $plates;

      I am trying to have $plates be an array, filled with multiple Plate objects, and I am doing the following to push more Objects onto the array: $plate = new plate();
      $p->plates[] = new SoapVar( $plate, SOAP_ENC_OBJECT, ‘Plate’, ‘urn:ArtWebService’ );

      It seems that when I try to the this in .NET i am getting an error saying that “The specified type was not recognized: name=’ur-type’, namespace=’http://www.w3.org/2001/XMLSchema’, at “

      I am sure that I am doing something wrong when encoding this objects in the array, I am just not sure what.

      Any help would be greatly appreciated.

      Class Plate {
      /**
      * @var string
      */
      var $platenum;
      /**
      * @var string
      */
      var $itemnmbr;
      /**
      * @var string
      */
      var $descr;
      /**
      * @var string
      */
      var $comments;
      function Plate() {
      }
      function setPlatenum( $platenum ) {
      $this->platenum = $platenum;
      }
      function setItemnmbr( $itemnmbr ) {
      $this->itemnmbr = $itemnmbr;
      }
      function setDescr( $descr ) {
      $this->descr = $descr;
      }
      function setComments( $comments ) {
      $this->comments = $comments;
      }
      }

    17. PHP and Flex - Part III > Manfred Weber`s Weblog 2007-10-9, 8:54 pm

      Gravatar

      [...] Basic Examples [...]

    18. Services_Webservice in the books | Manfred Weber`s Weblog 2008-02-7, 3:33 pm

      Gravatar

      [...] just found my pear package Services_Webservice in 2 books: Pro PHP XML and Web Services and the other one is Foundations of PEAR: Rapid PHP [...]

    Add a Comment