Latest web development tutorials

Web Service instances

Any application can have a Web Service components.

Category Web Service creation and programming language-independent.

This chapter we will introduce the use of PHP's SOAP extension to create a Web Service.

SOAP have two operating modes, NO-WSDL and WSDL.

  • NO-WSDL mode: to convey information to use parameters.
  • WSDL mode: Using WSDL file name as a parameter, and extract the required information from the service WSDL.

One example: PHP Web Service

Before you start an instance, we need to determine whether to install the PHP SOAP extension. See phpinfo, the following message appears already installed the SOAP extensions:

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

Server.php file server code is as follows:

<?php 
// SiteInfo 类用于处理请求
Class SiteInfo
{
    /**
     *    返回网站名称
     *    @return string 
     *
     */
    public function getName(){
        return "本教程";
    }

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

// 创建 SoapServer 对象
$s = new SoapServer(null,array("location"=>"http://localhost/soap/Server.php","uri"=>"Server.php"));

// 导出 SiteInfo 类中的全部函数
$s->setClass("SiteInfo");
// 处理一个SOAP请求,调用必要的功能,并发送回一个响应。
$s->handle();
?>

ClientClient.php file code is as follows:

<?php
try{
  // non-wsdl方式调用web service
  // 创建 SoapClient 对象
  $soap = new SoapClient(null,array('location'=>"http://localhost/soap/Server.php",'uri'=>'Server.php'));
  // 调用函数 
  $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();
}

Then we visit http: //localhost/soap/Client.php, output results are as follows: