How to implement a webservice_php tutorial using PHP

Source: Internet
Author: User
Tags php class wsdl

Wsdl

WSDL (Network Service Description Language, Web Services Description Language) is an XML-based language that describes Web services and how to access them. This document can describe a WEB service. It can specify the location of the service and the operation (or method) provided by the service.

The main structure of a WSDL document is similar to this:

 
  
  
   
    
      definition of types
  
   
  ..... 
   
    
      definition of a message ...
  .
   
  
   
    
      definition of a port
  
   
  .... 
   
    
      definition of a binding ....
  
   
 
  

A WSDL document can contain other elements, such as the extension element, and a service element that can combine the definitions of several Web services into a single WSDL document.

PHP generates WSDL

Class Code (SoapDiscovery.class.php):

 Class_name = $class _name;    $this->service_name = $service _name;     }/** * SOAPDISCOVERY::GETWSDL () Returns The WSDL of a class if the class is instantiable. * * @return String **/public Function getwsdl () {if (Empty ($this->service_name)) {thro        W New Exception (' No service name. '); } $headerWSDL = "
 n "; $headerWSDL. = "
 
  n "; $headerWSDL. = "
  
   
  n ";        if (Empty ($this->class_name)) {throw new Exception (' No class name. ');                } $class = new Reflectionclass ($this->class_name);        if (! $class->isinstantiable ()) {throw new Exception (' class is not instantiable. ');                } $methods = $class->getmethods (); $portTypeWSDL = '
  
   '; $bindingWSDL = '
   
    N
    
     
    n "; $serviceWSDL = '
    
     
      n 
       
      n 
      
       service_name. ' Port ' binding= "TNS: '. $this->service_name." Binding "> 
        
       n 
      N
     
    n ";        $messageWSDL = ";                foreach ($methods as $method) {if ($method->ispublic () &&! $method->isconstructor ()) { $portTypeWSDL. = '
    
     
      n ". '
      n 
      getName (). " Response "/>n 

     
    n "; $bindingWSDL. = '
    
     
      n ". ' 
       N
      
 NN 
      n 
        
       n n 
       
     
    n "; $messageWSDL. = '
    
     
      n ";                $parameters = $method->getparameters ();                foreach ($parameters as $parameter) {$messageWSDL. = ' 
       
      n '; } $messageWSDL. = "
     
    n "; $messageWSDL. = '
    
     
      n ";                $messageWSDL. = ' 
       
      n '; $messageWSDL. = "
     
    n "; }} $portTypeWSDL. = "
   
  n "; $bindingWSDL. = "n "; return sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '
 ');     }/** * Soapdiscovery::getdiscovery () Returns discovery of WSDL. * * @return String **/public Function Getdiscovery () {return '
 N
 
  
   n 
    
   N
  
 "; }}?>

How to use (Server-side server.php):

 
  GETWSDL ();    File_put_contents (Wsdl_url, $str);} Soap opens and receives the client's incoming parameter response $server = new SoapServer (wsdl_url), $server->setclass (' HelloWorld '); $server->handle ( );  The test definition exposes classes class HelloWorld {    private $nombre = ';    Public function __construct ($name = ' world ') {$this->name = $name;}    Public function greet ($name = ") {$name = $name? $name: $this->name;return ' Hello ' $name. '. ';}    Public Function Servertimestamp () {return time ();}}? >

Client client.php:

 
  Greet (' IELIWB ');        Var_dump ($result);        echo "the answer Isresult";} catch (SoapFault $f) {        echo "Error Message: {$f->getmessage ()}";}? >

Create Webservice

1. Create WSDL

    • Non-standard WebService, which may only be accessible by PHP
    • Standard WebService, you have to use WSDL (WebService Description Language, which is the XML syntax standard to describe your service content, as I understand it)

Here I only introduce the standard webservice. So how do you create WSDL? This is a really hard thing for PHP, and some people say it's easy to create it with Zend Studio, which is a method. But for those who do not like to use Zend Studio, will feel to create a webservice also install Zend Studio, too much, I just, hey.

Here I introduce a simple method, to download the SoapDiscovery.class.php class online, there is a common method: Getwsdl, this method at the end of the return, then, you modify this method, I do this:

return sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ');//Generate WSDL file , put the return comment above $fso = fopen ($this->class_name. ". wsdl", "w"); Fwrite ($fso, sprintf ('%s%s%s%s%s%s ', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $ MESSAGEWSDL, "));

Now the class that generates the WSDL has, SoapDiscovery.class.php.

I just need to prepare a service class or function to create the WSDL. For example, I have a class: person, file name: person.class.php★, there are two methods, one is say, one is run. Very simple.

 
  

There are two classes here: SoapDiscovery.class.php and person.class.php.

Begin formally generating WSDL: Create file server.php. Copy the following to run to generate a person.wsdl file

 
  GETWSDL ();? >

2. Create a WebService service-side program

Empty the contents of the server.php file and copy the following code in:

 
  SetClass ("person");//Register all methods of the person class    $objSoapServer->handle ();//Processing Request?>

3. Create the WebService client program to test whether the webservice is valid and the file name is: client.php

 
  Say ());    echo "
"; Echo ($client->run ()); echo "
";? >

OK, end. NET if you want to use, you just have to provide a URL to him on the line.

How to get the URL: You can find it in the person.wsdl file first, here the URL (the specific URL is determined by your directory) is what you want to provide. NET developers to use. But do not be happy too early, the following to add: "? wsdl", http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl this is the right, do not believe you can copy the URL to the browser's address bar to see it.

. NET developer gets the URL you gave him, you can add a service reference or Web reference to your project, and then you can follow the prompts to complete the operation, for use. NET developers are simple.

http://www.bkjia.com/PHPjc/752376.html www.bkjia.com true http://www.bkjia.com/PHPjc/752376.html techarticle WSDL WSDL (Network Service Description Language, Web Services Description Language) is an XML-based language that describes Web services and how to access them. This document can ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.