日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php中soap的使用以及wsdl的生成

發布時間:2024/9/30 php 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php中soap的使用以及wsdl的生成 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

webservice

webservice是一個部署在web服務器上的,向外界暴露出一個能過通過web進行調用的API。顧名思義,就是基于WEB的服務。它使用WEB(HTTP)方式,接收和響應外部系統的某種請求;從而實現遠程調用。

wsdl

  • ( web service descroption language) web 服務描述語言 ;

  • 提供能辦事的文檔說明,通常由 xml 構成 ;

  • soap

  • 簡單對象訪問協議
  • 提供請求的規范
  • wsdl和soap雖然是web service的兩大標準,但是兩者并沒有必然的聯系,都可以獨立使用。

    實現soap接口的兩種方式
    • 有wsdl文件的方式

      ? 在這里先介紹標準的webservice。 那么如何創建wsdl呢?對于PHP來說這確實是件很不容易的事情,有人說用zend studio創建很方便,這是一種方法。但對于那些不喜歡用zend studio的人來說,會覺得創建一個web service還要安裝zend studio,太強人所難了。wsdl文件是可以 使用腳本生成的,網友寫的SoapDiscovert.class.php

    <?php /*** Copyright (c) 2005, Braulio Jos?Solano Rojas* All rights reserved.* * Redistribution and use in source and binary forms, with or without modification, are* permitted provided that the following conditions are met:* * Redistributions of source code must retain the above copyright notice, this list of* conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of* conditions and the following disclaimer in the documentation and/or other materials* provided with the distribution. * Neither the name of the Solsoft de Costa Rica S.A. nor the names of its contributors may* be used to endorse or promote products derived from this software without specific* prior written permission.* * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.* ** @version $Id$* @copyright 2005 *//*** SoapDiscovery Class that provides Web Service Definition Language (WSDL).* * @package SoapDiscovery* @author Braulio Jos?Solano Rojas* @copyright Copyright (c) 2005 Braulio Jos?Solano Rojas* @version $Id$* @access public* */ class SoapDiscovery {private $class_name = '';private $service_name = '';/*** SoapDiscovery::__construct() SoapDiscovery class Constructor.* * @param string $class_name* @param string $service_name* */public function __construct($class_name = '', $service_name = '',$path = './') {$this->class_name = $class_name;$this->service_name = $service_name;$this->path = $path;}/*** SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.* * @return string* */public function getWSDL() {header("Content-type: text/html; charset=utf-8");if (empty($this->service_name)) {throw new Exception('No service name.');}$headerWSDL = "<?xml version=\"1.0\" ?>\n";$headerWSDL.= "<definitions name=\"$this->service_name\" targetNamespace=\"urn:$this->service_name\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"urn:$this->service_name\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\">\n";$headerWSDL.= "<types xmlns=\"http://schemas.xmlsoap.org/wsdl/\" />\n";if (empty($this->class_name)) {throw new Exception('No class name.');}$class = new ReflectionClass($this->class_name);if (!$class->isInstantiable()) {throw new Exception('Class is not instantiable.');}$methods = $class->getMethods();$portTypeWSDL = '<portType name="' . $this->service_name . 'Port">';$bindingWSDL = '<binding name="' . $this->service_name . 'Binding" type="tns:' . $this->service_name . "Port\">\n<soap:binding style=\"rpc\" transport=\"http://schemas.xmlsoap.org/soap/http\" />\n";$serviceWSDL = '<service name="' . $this->service_name . "\">\n<documentation />\n<port name=\"" . $this->service_name . 'Port" binding="tns:' . $this->service_name . "Binding\"><soap:address location=\"http://" . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'] . "\" />\n</port>\n</service>\n";$messageWSDL = '';foreach ($methods as $method) {if ($method->isPublic() && !$method->isConstructor()) {$portTypeWSDL.= '<operation name="' . $method->getName() . "\">\n" . '<input message="tns:' . $method->getName() . "Request\" />\n<output message=\"tns:" . $method->getName() . "Response\" />\n</operation>\n";$bindingWSDL.= '<operation name="' . $method->getName() . "\">\n" . '<soap:operation soapAction="urn:' . $this->service_name . '#' . $this->class_name . '#' . $method->getName() . "\" />\n<input><soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</input>\n<output>\n<soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</output>\n</operation>\n";$messageWSDL.= '<message name="' . $method->getName() . "Request\">\n";$parameters = $method->getParameters();foreach ($parameters as $parameter) {$messageWSDL.= '<part name="' . $parameter->getName() . "\" type=\"xsd:string\" />\n";}$messageWSDL.= "</message>\n";$messageWSDL.= '<message name="' . $method->getName() . "Response\">\n";$messageWSDL.= '<part name="' . $method->getName() . "\" type=\"xsd:string\" />\n";$messageWSDL.= "</message>\n";}}$portTypeWSDL.= "</portType>\n";$bindingWSDL.= "</binding>\n";//return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>');//生成wsdl文件,將上面的return注釋$this->wsdl_name = preg_replace('/(\\\.*\\\)/','',$this->class_name);$fso = fopen($this->path.$this->wsdl_name . ".wsdl", "w");fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'));}/*** SoapDiscovery::getDiscovery() Returns discovery of WSDL.* * @return string* */public function getDiscovery() {return "<?xml version=\"1.0\" ?>\n<disco:discovery xmlns:disco=\"http://schemas.xmlsoap.org/disco/\" xmlns:scl=\"http://schemas.xmlsoap.org/disco/scl/\">\n<scl:contractRef ref=\"http://" . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'] . "?wsdl\" />\n</disco:discovery>";}}?>

    ? 再準備一個提供服務的Service.php類文件或者函數就可以創建wsdl了!

    <?php class Service {public function HelloWorld() { return "Hello"; }public function Add($a, $b) { return $a + $b; }} ?>

    下面創建wsdl文件的creat_wsdl.php

    <?php include("Service.php"); include("SoapDiscovery.class.php");$disc = new SoapDiscovery('Service', 'soap'); //第一個參數是類名(生成的wsdl文件就是以它來命名的,例如:下面的Service.wsdl),即Service類,第二個參數是服務的名字(這個可以隨便寫) $disc->getWSDL();

    ##說明:通過查看"SoapDiscovery.class.php"文件的源碼發現,此成員函數如果開啟return語句,會返回一個xml格式的字符串,如果用 $strXML = $disc->getWSDL();echo $strXML;那么在瀏覽器中將不能顯示xml的wsdl文件。因為瀏覽器默認用的是"html/text"來返回stdout的內容,需要在php腳本的頭部用:
    header(“Content-type: text/html; charset=utf-8”); 才可以讓瀏覽器不把輸出作為html而是作為xml來解析!
      用web訪問的方式運行create_wsdl.php文件,此時會生成一個Service.wsdl的文件,下面將會用到這個文件!
    注意:上面方法生成的wsdl文件在windows下面沒問題,但是在Linux下面的wsdl文件中的"location=XXX" 段,確實沒有服務器IP地址。所以要修改該代碼中的: $_SERVER[‘SERVER_NAME’] 為 $_SERVER[‘SERVER_HOST’] ,因為在linux(nginx)下面,前者變量為空,這樣才能生成正確的wsdl文檔!

    再在Service.php文件中添加一些代碼

    <?php class Service {public function HelloWorld() { return "Hello"; }public function Add($a, $b) { return $a + $b; }}$server = new SoapServer('Service.wsdl', array('soap_version' => SOAP_1_2)); ##此處的Service.wsdl文件是上面生成的 $server->setClass("Service"); //注冊Service類的所有方法 $server->handle(); //處理請求 ?>創建**webservice客戶端**程序,測試webservice是否有效,文件名是:client.php 將以下內容拷貝進去: <?phpini_set('soap.wsdl_cache_enabled', "0"); //關閉wsdl緩存 $soap = new SoapClient('http://localhost/Dragon/soap/Service.php?wsdl');echo $soap->Add(28, 2); echo $soap->__soapCall('Add',array(28,2))//或這樣調用 \#echo $soap->__Call('Add',array(28,2));?>

    OK!測試通過!

    需要更改生成的wsdl文件中的location的值

    • 無WSDL文件方式
        服務器端
    <?php class Service { public function HelloWorld() { return "Hello"; } public function Add($a,$b) { return $a+$b; } } $server=new SoapServer(null,array('uri' => "abcd")); $server->setClass("Service"); $server->handle(); ?>

    客戶端

    <?php try { $soap = new SoapClient(null, array( "location" => "http://localhost/Dragon/soap/Service.php", "uri" => "abcd", //資源描述符服務器和客戶端必須對應 "style" => SOAP_RPC, "use" => SOAP_ENCODED ));echo $soap->Add(12, 2); } catch (Exction $e) { echo print_r($e->getMessage(), true); } ?>

    總結

    以上是生活随笔為你收集整理的php中soap的使用以及wsdl的生成的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。