国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

php中soap 的使用實例

2020-03-22 20:02:59
字體:
來源:轉載
供稿:網友
  • 最近工作的內容使用到了接口!
    對于系統接口:
    現下接觸的有兩種!

    1、URL類型的接口

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

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

    2、SOAP類型的接口

    無關編程語言、無關平臺、擴展性很好

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


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

    講解前,先配置下PHP的soap環境支持:

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

    刪除掉";" ,重啟apache服務器

    一、有WSDL文件方式
    在這里先介紹標準的webservice。
    那么如何創建wsdl呢?對于PHP來說這確實是件很不容易的事情,有人說用zend studio創建很方便,這是一種方法。但對于那些不喜歡用zend studio的人來說,會覺得創建一個web service還要安裝zend studio,太強人所難了。
    在這里介紹一個簡單的方法,到網上下載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>'));
    現在生成wsdl的類有了,SoapDiscovery.class.php(源碼在最末尾)。

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

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

    創建wsdl文件的creat_wsdl.php

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

    創建webservice客戶端程序,測試webservice是否有效,文件名是:client.php
    將以下內容拷貝進去:

    <?php ini_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))//或這樣調用 

    OK!測試通過!
     

    二、無WSDL文件方式

      服務器端

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

    三.出現的問題。

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

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

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

    現在,你已經解決你了系統接口的問題了!剩下的空余時間,就有必要去了解什么是SOAP、http協議、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編程

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

  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 应用必备| 贵港市| 四川省| 托里县| 韩城市| 英超| 镇坪县| 习水县| 麻江县| 抚松县| 屏山县| 闽侯县| 麻江县| 新泰市| 延津县| 滦平县| 鄂伦春自治旗| 大足县| 淳化县| 武穴市| 霍林郭勒市| 汾阳市| 玛多县| 富宁县| 桐城市| 华蓥市| 永年县| 南溪县| 沙河市| 武宁县| 麻江县| 花莲县| 高青县| 庆阳市| 连云港市| 冷水江市| 明光市| 阜康市| 云安县| 枣强县| 渝中区|