php中soap 的利用实例无需手写WSDL文件,提供自动生成WSDL文件类(2)

<?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文件方法

   处事器端

<?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); }

  

三.呈现的问题。

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 = '<portTypecolor:rgb(128,0,128);line-height:1.5 !important;">$this->service_name . 'Port">'; $bindingWSDL = '<bindingcolor:rgb(128,0,128);line-height:1.5 !important;">$this->service_name . 'Binding" type="tns:' . $this->service_name . "Port\">\n<soap:binding style=\"rpc\" transport=\"http://schemas.xmlsoap.org/soap/http\" />\n"; $serviceWSDL = '<servicecolor:rgb(128,0,128);line-height:1.5 !important;">$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.= '<operationcolor:rgb(128,0,128);line-height:1.5 !important;">$method->getName() . "\">\n" . '<input message="tns:' . $method->getName() . "Request\" />\n<output message=\"tns:" . $method->getName() . "Response\" />\n</operation>\n"; $bindingWSDL.= '<operationcolor:rgb(128,0,128);line-height:1.5 !important;">$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.= '<messagecolor:rgb(128,0,128);line-height:1.5 !important;">$method->getName() . "Request\">\n"; $parameters = $method->getParameters(); foreach ($parameters as $parameter) { $messageWSDL.= '<partcolor:rgb(128,0,128);line-height:1.5 !important;">$parameter->getName() . "\" type=\"xsd:string\" />\n"; } $messageWSDL.= "</message>\n"; $messageWSDL.= '<messagecolor:rgb(128,0,128);line-height:1.5 !important;">$method->getName() . "Response\">\n"; $messageWSDL.= '<partcolor:rgb(128,0,128);line-height:1.5 !important;">$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>"; } } ?>

 

=================================================================================

 

批改版:
按照YII 的CWsdlGenerator改变而来,

增加了接口参数的范例识别;

利用前需利用IDE对要领名举办注释,方可自动生成有效的WSDL文件。(详细原因可查察代码processMethod要领中的115行)

 

挪用要领:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8277.html