使用 PHP SOAP 来创建一个简单的 Web Service。

访问:

http://www.debug.com/php-soap-demo.php?client=22

结果:

apache:

<VirtualHost _default_:80>
DocumentRoot "E:\www\test\debug"
ServerName www.debug.com
ServerAlias debug.com
  <Directory "E:\www\test\debug">
    Options -Indexes +FollowSymLinks +ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>
</VirtualHost>

  

code:: php-soap-demo.php

<?php

/*-------------*/
if(isset($_GET['client'])){//fixme client index -  客户端入口
    try{
        // non-wsdl方式调用web service
        // 创建 SoapClient 对象
        $soap = new SoapClient(null,array('location'=>"http://www.debug.com/php-soap-demo.php",'uri'=>'php-soap-demo.php'));
        // 调用函数
        $result1 = $soap->getName();
        $result2 = $soap->__soapCall("getHost",array());
        echo $result1."<br/>";
        echo $result2;
    } catch(SoapFault $e){
        echo $e->getMessage();
    }catch(Exception $e){
        echo $e->getMessage();
    }
}
/*-------------*/

//fixme server index

//request Class
Class Request
{
    //base config
    protected $config = [
        'app'=> '徐锅博客!',
        'host'=>'localhost:3038'
    ];
    //construct
    public function __construct($config= [])
    {
        $this->config = array_merge($this->config,$config);
    }
    //get attr config
    public function __get($name){
        return $this->config[$name];
    }
    //soap method
    public function getName()
    {
        return $this->app;
    }
    //soap method
    public function getHost()
    {
        return $this->host;
    }
}

// Create SoapServer OBJECT
$server = new SoapServer(null,array("location"=>"http://www.debug.com/php-soap-demo.php","uri"=>"php-soap-demo.php"));

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

  

猜你喜欢

转载自www.cnblogs.com/q1104460935/p/10103919.html