搜尋
首頁php框架Laravellaravel框架下soapServer支援wsdl的程式碼範例

laravel框架下soapServer支援wsdl的程式碼範例

Oct 26, 2018 pm 04:33 PM
laravelphpsoapwebservice

本篇文章给大家带来的内容是关于laravel中soapServer支持wsdl的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

$server = new \SoapServer(null, ['uri' => 'noganluonguri']);
$server->setObject(new NganluongServer());
ob_start();
$server->handle();
return ob_get_clean();

上边这段代码是无wsdl模式下的,但是这次是对接第三方的服务,需要我们这边去定义soap webservice,第三方来调用,第三方定义的是wsdl模式的,所以今天研究了下。

laravel代码示例(其它框架类似思考方式):

  • 主要逻辑代码 - SoapService.php

<?php /**
 * soap服务端
 */

namespace App\Services;

Class SoapService
{
    public function getSum($param1, $param2)
    { 
        return $param1 + $param2;
    }
}
  • 创建路由

$api->any('soapUrl', 'SoapCallbackController@soapFun');
  • 路由主要实现方法-wsdl不存在则创建,不需要手动创建,url:https:xxx/soapurl?wsdl

<?php Class SoapCallbackController {
    public function soapFun()
        {
            try {
                $procClass     = &#39;App\Services\SoapService&#39;;
                $classNameFull = explode(&#39;\\&#39;, $procClass);
                $className     = array_pop($classNameFull);
                $storagePath   = storage_path();
                if (! file_exists($storagePath . &#39;/wsdl/&#39; . $className . &#39;.wsdl&#39;)) {
                    if (! file_exists($storagePath . &#39;/wsdl/&#39;)) {
                        mkdir($storagePath . &#39;/wsdl/&#39;, 0777, true);
                    }
                    require_once app_path() . &#39;/Libs/SoapDiscovery.php&#39;;
       
                    $soapDiscovery = new \SoapDiscovery($procClass, &#39;soap&#39;);
                    $file          = fopen($storagePath . &#39;/wsdl/&#39; . $className . &#39;.wsdl&#39;, &#39;w&#39;);
                    fwrite($file, $soapDiscovery->getWSDL());
                    fclose($file);
                }
                $server = new \SoapServer($storagePath . '/wsdl/' . $className . '.wsdl', array('soap_version' => SOAP_1_2));
                $server->setClass($procClass);
                $server->handle();
            } catch (\Exception $e) {
                Log::error('wsdl服务创建异常');
            }
        }
    }
  • 生成wsdl类 - SoapDiscovery.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 = &#39;&#39;;
    private $service_name = &#39;&#39;;

    /**
     * SoapDiscovery::__construct() SoapDiscovery class Constructor.
     * 
     * @param string $class_name
     * @param string $service_name
     * */
    public function __construct($class_name = &#39;&#39;, $service_name = &#39;&#39;) {
        $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>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></types>\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>service_name . 'Port">';
        $bindingWSDL = '<binding>service_name . 'Binding" type="tns:' . $this->service_name . "Port\">\n<binding></binding>\n";
        $serviceWSDL = '<service>service_name . "\">\n<documentation></documentation>\n<port>service_name . 'Port" binding="tns:' . $this->service_name . "Binding\"><address></address>\n</port>\n</service>\n";
        $messageWSDL = '';
        foreach ($methods as $method) {
            if ($method->isPublic() && !$method->isConstructor()) {
                $portTypeWSDL.= '<operation>getName() . "\">\n" . '<input>getName() . "Request\" />\n<output>getName() . "Response\" />\n</output></operation>\n";
                $bindingWSDL.= '<operation>getName() . "\">\n" . '<operation>service_name . '#' . $this->class_name . '#' . $method->getName() . "\" />\n<input><body>service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n\n<output>\n<body>service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</body></output>\n</body></operation></operation>\n";
                $messageWSDL.= '<message>getName() . "Request\">\n";
                $parameters = $method->getParameters();
                foreach ($parameters as $parameter) {
                    $messageWSDL.= '<part>getName() . "\" type=\"xsd:string\" />\n";
                }
                $messageWSDL.= "</part></message>\n";
                $messageWSDL.= '<message>getName() . "Response\">\n";
                $messageWSDL.= '<part>getName() . "\" type=\"xsd:string\" />\n";
                $messageWSDL.= "</part></message>\n";
            }
        }
        $portTypeWSDL.= "</binding></porttype>\n";
        $bindingWSDL.= "\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, ''));
    }

    /**
     * SoapDiscovery::getDiscovery() Returns discovery of WSDL.
     * 
     * @return string
     * */
    public function getDiscovery() {
        return "<?xml  version=\"1.0\" ?>\n<discovery>\n<contractref></contractref>\n</discovery>";
    }

}

?>
  • webservice测试

<?php // 关闭wsdl缓存
ini_set(&#39;soap.wsdl_cache_enabled&#39;, "0");

$soap = new SoapClient(&#39;https:xxx/soapurl?wsdl&#39;);
// 以下两种调用方式
echo $soap->getSum(10, 24);
echo $soap->__soapCall('getSum',array(10,24));

以上是laravel框架下soapServer支援wsdl的程式碼範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:segmentfault。如有侵權,請聯絡admin@php.cn刪除
Laravel的後端功能:數據庫,邏輯等等Laravel的後端功能:數據庫,邏輯等等Apr 14, 2025 am 12:04 AM

Laravel在後端開發中表現強大,通過EloquentORM簡化數據庫操作,控制器和服務類處理業務邏輯,並提供隊列、事件等功能。 1)EloquentORM通過模型映射數據庫表,簡化查詢。 2)業務邏輯在控制器和服務類中處理,提高模塊化和可維護性。 3)其他功能如隊列系統幫助處理複雜需求。

Laravel的多功能性:從簡單站點到復雜系統Laravel的多功能性:從簡單站點到復雜系統Apr 13, 2025 am 12:13 AM

選擇Laravel開發項目是因為其靈活性和強大功能適應不同規模和復雜度的需求。 Laravel提供路由系統、EloquentORM、Artisan命令行等功能,支持從簡單博客到復雜企業級系統的開發。

Laravel(PHP)與Python:開發環境和生態系統Laravel(PHP)與Python:開發環境和生態系統Apr 12, 2025 am 12:10 AM

Laravel和Python在開發環境和生態系統上的對比如下:1.Laravel的開發環境簡單,僅需PHP和Composer,提供了豐富的擴展包如LaravelForge,但擴展包維護可能不及時。 2.Python的開發環境也簡單,僅需Python和pip,生態系統龐大,涵蓋多個領域,但版本和依賴管理可能複雜。

Laravel和後端:為Web應用程序提供動力邏輯Laravel和後端:為Web應用程序提供動力邏輯Apr 11, 2025 am 11:29 AM

Laravel是如何在後端邏輯中發揮作用的?它通過路由系統、EloquentORM、認證與授權、事件與監聽器以及性能優化來簡化和增強後端開發。 1.路由系統允許定義URL結構和請求處理邏輯。 2.EloquentORM簡化數據庫交互。 3.認證與授權系統便於用戶管理。 4.事件與監聽器實現松耦合代碼結構。 5.性能優化通過緩存和隊列提高應用效率。

為什麼Laravel如此受歡迎?為什麼Laravel如此受歡迎?Apr 02, 2025 pm 02:16 PM

Laravel受歡迎的原因包括其簡化開發過程、提供愉快的開發環境和豐富的功能。 1)它吸收了RubyonRails的設計理念,結合PHP的靈活性。 2)提供瞭如EloquentORM、Blade模板引擎等工具,提高開發效率。 3)其MVC架構和依賴注入機制使代碼更加模塊化和可測試。 4)提供了強大的調試工具和性能優化方法,如緩存系統和最佳實踐。

django或laravel哪個更好?django或laravel哪個更好?Mar 28, 2025 am 10:41 AM

Django和Laravel都是全棧框架,Django適合Python開發者和復雜業務邏輯,Laravel適合PHP開發者和優雅語法。 1.Django基於Python,遵循“電池齊全”哲學,適合快速開發和高並發。 2.Laravel基於PHP,強調開發者體驗,適合小型到中型項目。

哪個是更好的PHP或Laravel?哪個是更好的PHP或Laravel?Mar 27, 2025 pm 05:31 PM

PHP和Laravel不是直接可比的,因為Laravel是基於PHP的框架。 1.PHP適合小型項目或快速原型開發,因其簡單直接。 2.Laravel適合大型項目或高效開發,因其提供豐富功能和工具,但學習曲線較陡,性能可能不如純PHP。

Laravel是前端還是後端?Laravel是前端還是後端?Mar 27, 2025 pm 05:31 PM

laravelisabackendframeworkbuiltonphp,設計ForweBapplicationDevelopment.itfocusessonserver-sideLogic,databasemagemention和Applicationstructure和CanBeintegratedWithFrontendTechnologiesLikeLikeVue.jsorreActeReacterVue.jsorreActforforfull-stackDevefloct。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器