麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > PHP > 正文

php中soap 的使用實例

2020-03-22 20:02:59
字體:
供稿:網(wǎng)友
  • 最近工作的內(nèi)容使用到了接口!
    對于系統(tǒng)接口:
    現(xiàn)下接觸的有兩種!

    1、URL類型的接口

    URL路由帶參數(shù)式的接口!這個很好做!只要有過Web開發(fā)經(jīng)驗的人都能完成!
    這種接口數(shù)據(jù)不夠隱蔽性,可以直接在瀏覽其中看到,

    如支付寶的交易請求URL。需要加一個MD5簽名,和本地客戶端的再次向服務(wù)器的驗證!
    雖然soap方式傳遞的數(shù)據(jù)隱蔽性很好!但為了數(shù)據(jù)安全,難免也需要進行數(shù)據(jù)簽名。

    2、SOAP類型的接口

    無關(guān)編程語言、無關(guān)平臺、擴展性很好

    要實現(xiàn)一個SOAP 型的接口,有兩種方式:一種有WSDL文件方式、一中無WSDL文件方式!


    對于熱愛研究型的人來說,使用第一種方式可以讓你清楚的了解PHP是怎么創(chuàng)建了一個Web Service!
    但第一種對于新手來說,創(chuàng)建一個XML格式的WSDL文件,是比較難的,這你的先了解熟悉什么是XML!
    學(xué)會XML語法!
    但對于一個急于解決問題的人來說!沒有這么多的時間去熟悉!所以這是件煩惱的事!
    不過不急,上面說了,還有一種無需WSDL文件的方式!

    講解前,先配置下PHP的soap環(huán)境支持:

    找到php.ini文件
    ;extension=php_soap.dll

    刪除掉";" ,重啟apache服務(wù)器

    一、有WSDL文件方式
    在這里先介紹標(biāo)準(zhǔn)的webservice。
    那么如何創(chuàng)建wsdl呢?對于PHP來說這確實是件很不容易的事情,有人說用zend studio創(chuàng)建很方便,這是一種方法。但對于那些不喜歡用zend studio的人來說,會覺得創(chuàng)建一個web service還要安裝zend studio,太強人所難了。
    在這里介紹一個簡單的方法,到網(wǎng)上下載SoapDiscovery.html' target='_blank'>class.php類,里面有個公用方法:getWSDL,這個方法末尾是用的return,那么,你修改一下這個方法:
    //return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>');
    //生成wsdl文件,將上面的return注釋
    $fso = fopen($this->class_name . ".wsdl" , "w");
    fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'));
    現(xiàn)在生成wsdl的類有了,SoapDiscovery.class.php(源碼在最末尾)。

    再準(zhǔn)備一個提供服務(wù)的Service.php類文件或者函數(shù)就可以創(chuàng)建wsdl了!

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

    創(chuàng)建wsdl文件的creat_wsdl.php

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

    運行create_wsdl.php文件,此時會生成一個Service.wsdl的文件


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

    <?phpclass 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));$server->setClass("Service"); //注冊Service類的所有方法 $server->handle(); //處理請求 ?>

    創(chuàng)建webservice客戶端程序,測試webservice是否有效,文件名是:client.php
    將以下內(nèi)容拷貝進去:

    <?php ini_set('soap.wsdl_cache_enabled', "0"); //關(guān)閉wsdl緩存  $soap = new SoapClient('http://localhost/Dragon/soap/Service.php?wsdl'); echo $soap->Add(28, 2);  echo $soap->__soapCall('Add',array(28,2))//或這樣調(diào)用 

    OK!測試通過!
     

    二、無WSDL文件方式

      服務(wù)器端

    <?phpclass 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", //資源描述符服務(wù)器和客戶端必須對應(yīng)  "style" => SOAP_RPC,  "use" => SOAP_ENCODED  ));  echo $soap->Add(12, 2);  } catch (Exction $e) {  echo print_r($e->getMessage(), true);  }

    三.出現(xiàn)的問題。

    1.在方法中對屬性的賦值在其他方法中不起作用。
    比如在客戶端調(diào)用服務(wù)端某個方法對某個屬性賦值。
    在其他方法里就不能用。但在 __construct 方法中對屬性的賦值是可以個在其他方法中使用的。

    2. 提示 Client] looks like we got no XML document錯誤。
    服務(wù)器端文件在<?php ?> 標(biāo)簽前后都不要有任何數(shù)據(jù)包括空格,空行。

    3. Warning: SoapClient::SoapClient(): I/O warning : failed to load external entity
    原因如下:PHP程序作為 SOAP客戶端 采用 WSDL 模式訪問遠端服務(wù)器的時候,PHP是通過調(diào)用 libcurl 實現(xiàn)的。至少在 PHP5.2.X 是這樣的。如果采用 non-WSDL 模式,就不需要 libcurl。除了 了ibcurl以外,至少還關(guān)聯(lián)的庫包括:libidn,ibgcc,libiconv,libintl,openssl

    現(xiàn)在,你已經(jīng)解決你了系統(tǒng)接口的問題了!剩下的空余時間,就有必要去了解什么是SOAP、http協(xié)議、XML了!

    SoapDiscovery.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 = '') {        $this->class_name = $class_name;        $this->service_name = $service_name;    }    /**     * SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.     *      * @return string     * */    public function getWSDL() {        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注釋        $fso = fopen($this->class_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>";    }}?>

    PHP編程

    鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

  • 發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發(fā)表
    主站蜘蛛池模板: 久久久久久久久久亚洲 | 亚洲一区二区中文字幕在线观看 | 久草视频手机在线观看 | 美女黄影院 | 久久欧美亚洲另类专区91大神 | 色av综合在线 | 黄色片在线免费播放 | 国产亚洲网 | 欧美视频黄色 | 欧美一级黄色录像片 | 免费在线观看毛片视频 | 精品一区在线视频 | 美女毛片在线观看 | 97干色 | 成人福利在线 | 成人免费自拍视频 | 国产精品wwww| 成人区一区二区三区 | 斗罗破苍穹在线观看免费完整观看 | 亚洲五码在线观看视频 | 久久成人免费观看 | 91成人免费在线视频 | 极品大长腿啪啪高潮露脸 | 免费欧美精品 | 久久老司机精品视频 | 久草热久草视频 | 日韩视频在线观看免费视频 | 黄色大片免费网站 | 狠狠99| 成人国产精品久久久 | av手机在线免费播放 | 国产精品久久久久久久久久了 | 成人免费网站在线观看视频 | 黄色久 | 免费看性xxx高清视频自由 | 久久精品国产精品亚洲 | 免费人成年短视频在线观看网站 | 三级xxxx | av在线高清观看 | 欧美一级毛片大片免费播放 | 久久久久电影网站 |