PHP使用cURL调用WebService的问题

原创
2016-06-06 20:27:11 1077浏览

我想用PHP的cURL来访问WeatherWS
官网给出的请求示例:

POST /WebServices/WeatherWS.asmx/getWeather HTTP/1.1
Host: www.webxml.com.cn
Content-Type: application/x-www-form-urlencoded
Content-Length: length

theCityCode=string&theUserID=string

我写的代码:

$data = 'theCityCode=广州&theUserID=""';

$curlObj = curl_init();
curl_setopt($curlObj,CURLOPT_URL,'http://www.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather');
curl_setopt($curlObj,CURLOPT_HEADER,0);
curl_setopt($curlObj,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curlObj,CURLOPT_POST,1);
curl_setopt($curlObj,CURLOPT_POSTFIELDS,$data);

curl_setopt($curlObj,CURLOPT_HTTPHEADER,array("application/x-www-form-urlencoded;charset=utf-8","Content-length:".strlen($data)));
$rtn = curl_exec($curlObj);

if(!curl_errno($curlObj)){
    echo $rtn;
}else{
    echo 'Curl error:'.curl_error($curlObj);
}
curl_close($curlObj);

说是免费用户的话theUserID留空,可是执行的结构就是

发现错误:用户验证失败。http://www.webxml.com.cn/

如果不加入theUserID又会提示:

缺少参数: theUserID。

求解??

回复内容:

我想用PHP的cURL来访问WeatherWS
官网给出的请求示例:

POST /WebServices/WeatherWS.asmx/getWeather HTTP/1.1
Host: www.webxml.com.cn
Content-Type: application/x-www-form-urlencoded
Content-Length: length

theCityCode=string&theUserID=string

我写的代码:

$data = 'theCityCode=广州&theUserID=""';

$curlObj = curl_init();
curl_setopt($curlObj,CURLOPT_URL,'http://www.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather');
curl_setopt($curlObj,CURLOPT_HEADER,0);
curl_setopt($curlObj,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curlObj,CURLOPT_POST,1);
curl_setopt($curlObj,CURLOPT_POSTFIELDS,$data);

curl_setopt($curlObj,CURLOPT_HTTPHEADER,array("application/x-www-form-urlencoded;charset=utf-8","Content-length:".strlen($data)));
$rtn = curl_exec($curlObj);

if(!curl_errno($curlObj)){
    echo $rtn;
}else{
    echo 'Curl error:'.curl_error($curlObj);
}
curl_close($curlObj);

说是免费用户的话theUserID留空,可是执行的结构就是

发现错误:用户验证失败。http://www.webxml.com.cn/

如果不加入theUserID又会提示:

缺少参数: theUserID。

求解??

自己看下吧[超时什么的错误判断自己处理],真不知道怎么说你,不看文档还要玩高难度的。

网上大把简单的接口不用。
截图:

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。