在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
从前一阵开始,公司要和对方做web service对接。由于对方使用.net语言,而我方使用php。本来经理是要求我们也用.net写web service的服务端。而我上学时学的.net全忘了。。。 于是开始上网搜索用可否直接用php进行编程。 修改php.ini ;extension=php_soap.dll 那么首先写一个简单的服务端的测试方法 class Service { public function HelloWorld() { return "Hello"; } } 把这个方法存为一个php文件,一个web service文件就做好了。 由于web service需要一个xml格式的wsdl文件来进行对方法进行解析。所以我们也需要一个wsdl文件,.net比较方便可以自动生成,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文件,作为wsdl的制造方法。 <?php include("Service.php"); include("SoapDiscovery.class.php"); $disco = new SoapDiscovery('Service', 'Service'); //第一个参数是类名(生成的wsdl文件就是以它来命名的),即Service类,第二个参数是服务的名字(这个可以随便写)。 $disco->getWSDL(); 最后,运行上面这段代码就可以生成一个wsdl文件了。不过,new SoapDiscovery时的第二个参数要注意了,这个名字是service的服务名,php调用的时候用不到,但是如果你的web service是写给.net的同学调用的话,那么这个名字要跟对方约定好。因为.net方面会使用这个名字当做类名来调用我们的方法。 现在,wsdl文件也生成了。这时,我们需要在最开始写的服务方法里加上一段代码。 <?php class Service { public function HelloWorld() { return "Hello"; } $server = new SoapServer('Service.wsdl', array('soap_version' => SOAP_1_2)); $server->setClass("Service"); //注册Service类的所有方法 $server->handle(); //处理请求 ?> 变成如上所示。到这里,我们web service的服务端就完成了。服务端文件的地址就是http://xxxxx.Service.php?wsdl。可以把这个地址发给其他人调用了。 接下来,创建一个客户端的文件。这里就不用刚刚写的方法了,用网上一个天气预报的web servcie http://www.webxml.com.cn/WebServices/WeatherWebService.asmx <?php header("Content-Type: text/html; charset=utf-8"); $client = new SoapClient('http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl'); var_dump($client->__getFunctions());//把该web servcie的方法全部打印出来 $aaa = $client->getSupportDataSet();//调用其中一个无参方法 var_dump($aaa); //输出返回值 print_r($aaa); //输出返回值 ?> 写一个客户端文件如上。这个地方,我在调用.net方面的方法时,卡了很久,最后发现是对方参数格式问题,在这里特别提一下,要注意对方wsdl文件里的参数名。如果普通方式无法调通出现“php [soap:Server] 服务器无法处理请求。 ---> 缺少根元素”这个提示的时候,就要注意对方wsdl文件里关于参数名的问题了。我是用了$aaa = $client->__Call("ApplyStateConfirm",array($str));这种方式调用成功的,注意这里的$str也是一个数组,键名为对方wsdl文件里规定的参数name。 这样一个web service就成功调通了。 最后,如果你在调试方法的时候,发现部分方法不起作用,那是由于php的soap的wsdl缓存造成的。在php.ini文件里关闭它,然后重启nginx。 解决方法是,修改php.ini中的soap.wsdl_cache_enabled=1(改为0),soap.wsdl_cache_ttl=86400(改为0)两项。
特别注意的是,客户端和服务端的机器都要改。
|
2022-08-18
2022-07-22
2022-08-17
2022-11-06
2022-08-15
请发表评论