您好,欢迎访问一九零五行业门户网

laravel框架下soapServer支持wsdl的代码示例

本篇文章给大家带来的内容是关于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
<?phpclass soapcallbackcontroller { public function soapfun() { try { $procclass = 'app\services\soapservice'; $classnamefull = explode('\\', $procclass); $classname = array_pop($classnamefull); $storagepath = storage_path(); if (! file_exists($storagepath . '/wsdl/' . $classname . '.wsdl')) { if (! file_exists($storagepath . '/wsdl/')) { mkdir($storagepath . '/wsdl/', 0777, true); } require_once app_path() . '/libs/soapdiscovery.php'; $soapdiscovery = new \soapdiscovery($procclass, 'soap'); $file = fopen($storagepath . '/wsdl/' . $classname . '.wsdl', 'w'); 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 = ''; 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>;    }}?>
webservice测试
<?php// 关闭wsdl缓存ini_set('soap.wsdl_cache_enabled', "0");$soap = new soapclient('https:xxx/soapurl?wsdl');// 以下两种调用方式echo $soap->getsum(10, 24);echo $soap->__soapcall('getsum',array(10,24));
以上就是laravel框架下soapserver支持wsdl的代码示例的详细内容。
其它类似信息

推荐信息