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

访问:

 

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

 

结果:

 

apache:

1
2
3
4
5
6
7
8
9
10
11
12
<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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();

  

posted @   徐锅  阅读(615)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南

点击右上角即可分享
微信分享提示