PHP写WebService

简介:

PHP中很少用到WebService,最近才研究了一下,似乎没有想象中复杂。

1、Server端

定义一个类,在其中实现方法,然后用SoapServer将此类发布出去。

data_service.php:

 
  1. <?php 
  2. class DataAction{ 
  3.     public function add($x$y){ 
  4.         return $x + $y
  5.     } 
  6.  
  7. //定义SoapServer,不指定wsdl,必须指定uri,相当于asmx中的namespace 
  8. $server = new SoapServer(null, ["uri" => "php.test"]); 
  9. //指定此SoapServer发布DataAction类中的所有方法 
  10. $server->setClass("DataAction"); 
  11. $server->handle(); 
  12. ?> 

也可以不用class,直接发布function。

 
  1. <?php 
  2. function add($x$y){ 
  3.     return $x + $y
  4.  
  5. $server = new SoapServer(null, ["uri" => "php.test"]); 
  6. $server->addFunction("add"); 
  7. $server->handle(); 
  8. ?> 

2、Client端

client.php:

 
  1. <?php 
  2. //SoapClient即WebService客户端,其uri与Server端保持一致,location是WebService服务地址 
  3. $client = new SoapClient(null, ["uri" => "php.test""location" => "http://192.168.90.81/test/data_service.php"]); 
  4. //调用add方法,传入参数 
  5. echo $client->add(100, 200); 
  6. unset($client); 
  7. ?> 


抓包看看:

 
  1. 请求包: 
  2.  
  3. POST /test/data_service.php HTTP/1.1 
  4. Host: 192.168.90.81 
  5. Connection: Keep-Alive 
  6. User-Agent: PHP-SOAP/5.4.3 
  7. Content-Type: text/xml; charset=utf-8 
  8. SOAPAction: "php.test#add" 
  9. Content-Length: 512 
  10.  
  11. <?xml version="1.0" encoding="UTF-8"?> 
  12. <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="php.test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:add><param0 xsi:type="xsd:int">100</param0><param1 xsi:type="xsd:int">800</param1></ns1:add></SOAP-ENV:Body></SOAP-ENV:Envelope> 
  13.  
  14. -------------------------------- 
  15. 响应包: 
  16.  
  17. HTTP/1.1 200 OK 
  18. Server: nginx/1.2.0 
  19. Date: Thu, 07 Jun 2012 06:51:33 GMT 
  20. Content-Type: text/xml; charset=utf-8 
  21. Content-Length: 488 
  22. Connection: keep-alive 
  23. X-Powered-By: PHP/5.4.3 
  24.  
  25. <?xml version="1.0" encoding="UTF-8"?> 
  26. <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="php.net" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:addResponse><return xsi:type="xsd:int">900</return></ns1:addResponse></SOAP-ENV:Body></SOAP-ENV:Envelope> 


需要注意,
如果PHP是以FastCGI方式运行,且只启动了一个进程 ,由于php-cgi.exe的单线程模型,访问client.php时其又会向data_service.php发送请求,但当前请求未处理完毕,data_service.php无法响应,会造成卡死得不到结果。

比如nginx如果没有使用负载均衡,所有php请求都转发到同一个php-cgi进程,就肯定会卡死。请负载均衡到不同进程上,或者换用isapi模式。


使用这种方式没有定义wsdl,没想明白双方是如何约定通讯的。另外显然,用这种方式肯定无法跨语言访问服务,因为我们访问data_service.php?wsdl拿不到wsdl,返回如下:

 
  1. <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
  2.   <SOAP-ENV:Body> 
  3.     <SOAP-ENV:Fault> 
  4.       <faultcode>SOAP-ENV:Server</faultcode> 
  5.       <faultstring>WSDL generation is not supported yet</faultstring> 
  6.     </SOAP-ENV:Fault> 
  7.   </SOAP-ENV:Body> 
  8. </SOAP-ENV:Envelope> 








本文转自 BoyTNT 51CTO博客,原文链接:http://blog.51cto.com/boytnt/891283 ,如需转载请自行联系原作者

相关文章
|
XML 安全 PHP
php调用webservice的几种方法
原文:php调用webservice的几种方法 1.WSDL模式: extension = php_soap.dll extension = php_curl.dll extension = php_openssl.
1266 0
|
PHP C# 网络架构
php 如何利用 soap调用.Net的WebService asmx文件
原文:php 如何利用 soap调用.Net的WebService asmx文件 最近,帮一个同行测试用.net写的WebService接口,C#调用通过,现在需要测试一下php版本对它的调用,经过各种探索, 相关的PHP调用webservice的过程如下: 1.
1374 0
|
PHP C# 网络架构
PHP使用SOAP调用.net的WebService数据
需要和一个.net系统进行数据交换,对方提供了一个WebService接口,使用PHP如何调用这个数据呢,下面就看看使用SOAP调用的方法吧 这个与一般的PHP POST或GET传值再查库拿数据的思路有点不一样,需要用到SOAP模块,处理方法也很简单,就是有一些需要注意的事情。
1360 0
|
PHP 网络架构
利用PHP SOAP实现WEB SERVICE
php有两个扩展可以实现web service,一个是NuSoap,一个是php 官方的soap扩展,由于soap是官方的,所以我们这里以soap来实现web service.由于默认是没有打开soap扩展的,所以自己先看一下soap扩展有没有打开。
819 0
|
PHP 网络架构 数据格式
PHP调用WebService接口
WebService是一个提供外部使用的一个服务,使用PHP去调用它其实是很简单的,写一个demo如下: 1、首先你的PHP要支持SOAP 检测PHP是否支持SOAP打印phpinfo(),...
1514 0