您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php调用webservice失败怎么办

2025/11/10 15:34:28发布21次查看
php调用webservice失败的解决办法:1、去掉“;extension=php_soap.dll”前的分号并重启apache;2、创建wsdl文件并运行creatservice.php文件;3、关闭wsdl缓存。
本文操作环境:windows7系统、php7.1版、dell g3电脑
php调用webservice失败怎么办?
php编写webservice案例、webservice调用失败
作为开发者来讲,要想写webservice接口或者调用别人的webservice接口,首先需要了解什么是webservice。
简单说, webservice就是一些站点开放一些服务出来, 也可以是你自己开发的service, 也就是一些方法, 通过url,指定某一个方法名,发出请求,站点里的这个服务(方法),接到你的请求,根据传过来的参数,做一些处理,然后把处理后的结果以xml形式返回来给你,你的程序就解析这些xml数据,然后显示出来或做其它操作。
在开始编写之前需要对php环境做一些配置:
打开php安装目录下的php.ini文件
找到 ;extension=php_soap.dll ,去掉前面的分号,保存,然后重启apache。
配置好环境就可以编写php了,先创建creatservice.php文件,复制下面代码:
<?phpclass servicetest{ public function firsthandle($name){ return "你的名字是:".$name; } public function dbinsert($text){ $db=new mysqli("localhost","root","","test"); if(mysqli_connect_error()){ return '数据库连接失败。'; exit; } $result=$db->query("insert into test_soap (text) values('".$text."')"); if($result){ return $result; }else{ return "插入失败"; } }}if(file_exists('servicetest.wsdl')){ //只有当servicetest.wsdl文件存在时才进行注册类 $objsoapserver = new soapserver("servicetest.wsdl"); //注册servicetest类的所有方法 $objsoapserver->setclass("servicetest"); //处理请求 $objsoapserver->handle();}require_once "soapdiscovery.class.php";$dis=new soapdiscovery("servicetest","webservice"); //第一个参数为类名也是生成wsdl的文件名$dis->getwsdl();?>
第一次运行creatservice.php文件时要创建wsdl文件,创建wsdl文件需要用到一个辅助类soapdiscovery.class.php。在文章末尾可以得到。
创建完成之后便是测试调用,创建client.php文件,复制以下代码:
ini_set('soap.wsdl_cache_enabled','0');//关闭缓存$x = new soapclient("http://localhost/webservicetest/servicetest.wsdl"); //这里的链接换成你自己的访问链接try { echo $x->firsthandle('qweqrwergwe'); echo ('<pre>'); var_dump ( $x->__getfunctions () );//获取服务器上提供的方法 echo ('</pre>'); echo ('<pre>'); var_dump ( $x->__gettypes () );//获取服务器上数据类型 echo ('</pre>');}catch (soapfault $f){ echo "error message: {$f->getmessage()}";}
注意! ini_set('soap.wsdl_cache_enabled','0'); //关闭wsdl缓存
如果不关闭缓存,会造成测试无效,缓存数据会扰乱测试准确性。
注意!如果是在thinkphp5中使用soapclient函数应当如下使用:
$client = new \soapclient ('http://www.baidu.com/ids/terminal/terminalws.wsdl');
重要点:
使用https访问webservice的话会出现访问异常,博主就遇到这个问题的,本地创建和调用webservice都通过了,但是一放到线上就无法调用了,然后各种google、baidu。最后才发现线上的环境是http强制转了https。
我的解决办法是取消了http强制转换https的功能:
找到httpd-vhost.conf,注释掉一下代码:
## rewriteengine on## rewritecond %{https} !=on## rewriterule ^(.*) https://%{server_name}$1 [l,r]
注释掉上面的代码后我发现我的网站不是https了,呜呜呜~,真的是拆了东墙补西墙啊,赶紧想办法,因为网站必须使用https才行,还好我使用的是thinkphp框架,找到了一个方法,在入口文件index.php最下面加入下面的代码就行了:
if (empty($_server['https']) || $_server['https'] === "off") { $location = 'https://' . $_server['http_host'] . $_server['request_uri']; header('http/1.1 301 moved permanently'); header('location: ' . $location); exit;}
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>'); $fso = fopen($this->class_name . ".wsdl" , "w"); fwrite($fso, sprintf('%s%s%s%s%s%s', $headerwsdl, $porttypewsdl, $bindingwsdl, $servicewsdl, $messagewsdl, '</definitions>')); exit; } /** * 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视频教程》
以上就是php调用webservice失败怎么办的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product