php调用webservice(续)
在上一文中提到了如何调用一个webservice并获取其返回值。
http://www.yibin001.com/Archives/soapclient.aspx
昨天在调用服务器端为asp.net的webservice时遇到一些问题,在这里提出来,希望能给遇到同样问题的朋友们一点帮助。
具体表现为:
客户端用SoapClient发起一个请求,正常地传递参数,而webservice却无法正确获取参数值,每个值均为null!
通常在传递参数时我们是这样完成的:
$client = new SoapClient(‘http://xxxxxxxxxxx.asmx?wsdl’);
$res = $client->__Call(‘getWeatherbyCityName’,array(‘参数1′=>‘值’,’参数2′=>‘值’));
但调用对于用.net编写的ws,上面的代码有点无能为力了,参数传递失败。
昨天遇到这个问题时我头大了许久,后来才找到解决方案
If your service is a .NET doc/lit, which means the input message has a single part named ‘parameters’ that is a structure that wraps the parameters.
Your call should look like this:
<?php
$params = array(‘param_name_1′=>$val_1,’param_name_2′=>$val_2);
$client->call(‘MethodName’, array(‘parameters’ => $params));
?>
原文:http://cn.php.net/manual/en/function.soap-soapclient-soapcall.php
知道原因,也就好解决了。
回头看看下面的例子,也就相当容易了
<?php
header('Content-type:text/html;charset=utf-8');
require_once 'city.php'; //这里为城市与城市编码的缓存文件,也是从webxml上获取的。
if($_POST['submit']==='OK')
{
$client = new SoapClient('http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl');
$code = $_POST['city'];
$para = array('theCityName'=>$code); //getWeatherbyCityName只需要一个参数,参数名theCityName
//每调用一个方法,都会有一个对应的返回结果,结果名称为:方法名+Result,如下面的getWeatherbyCityNameResult,该返回结果为object
$res = $client->__Call('getWeatherbyCityName',array('paramters'=>$para))->getWeatherbyCityNameResult->string;
echo "<pre>";
echo "城市:".$res[1];
echo "<br/>气温:".$res[5];
echo "<br/>天气:".$res[6];
echo "<br/>风力:".$res[7];
echo "</pre>";
}
?>
在线demo http://demo.yibin001.com/weather/index.php
可惜我的服务器不支持SoapClient扩展,无法放demo。
soap.rar (4.91 kb)
另外发现一个提供巨多免费webservice的网站:
http://www.webxml.com.cn/zh_cn/web_services.aspx
http://www.programmableweb.com/
这个更强大…