Web Service instance


Release date:2023-12-29 Update date:2023-12-29 Editor:admin View counts:96

Label:

Web Service instance

Any application can have Web Service components.

The creation of Web Service is independent of the type of programming language.

In this section, we will introduce you to using PHP’s SOAP extension to create Web Service.

SOAP has two modes of operation, NO-WSDL and WSDL.

  • NO-WSDL mode: use parameters to pass the information to be used.

  • WSDL mode: use the WSDL file name as a parameter and extract the informationneeded for the service from the WSDL.

An example: PHP Web Service

Before starting the instance, we need to determine whether PHP has the SOAP extension installed. Looking at phpinfo, the following message appears to indicate that the SOAP extension has been installed:

Image0

In this example, we will use PHP SOAP to create a simple Web Service.

Server side the Server.php file code is as follows:

<?php
// The SiteInfo class is used to process requests
Class SiteInfo
{
    /**
     *    Return website name
     *    @return string
     *
     */
    public function getName(){
        return "Rookie Tutorial";
    }

    public function getUrl(){
        return "www.runoob.com";
    }
}

// Create SoapServer object
$s = new SoapServer(null,array("location"=>"http://localhost/soap/Server.php","uri"=>"Server.php"));

// Export all functions in the SiteInfo class
$s->setClass("SiteInfo");
// Process a SOAP request, call necessary functions, and send back a response.
$s->handle();
?>

Client the Client.php file code is as follows:

<?php
try{
  // non-wsdl method call web service
  // create SoapClient object
  $soap = new SoapClient(null,array('location'=>"http://localhost/soap/Server.php",'uri'=>'Server.php'));
  // Call function
  $result1 = $soap->getName();
  $result2 = $soap->__soapCall("getUrl",array());
  echo $result1."<br/>";
  echo $result2;
} catch(SoapFault $e){
  echo $e->getMessage();
}catch(Exception $e){
  echo $e->getMessage();
}

At this point, we visit http://localhost/soap/Client.php and the output is as follows:

Image1

Powered by TorCMS (https://github.com/bukun/TorCMS).