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> 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM