当前位置:Gxlcms > PHP教程 > PHP使用cURL调用WebService的问题

PHP使用cURL调用WebService的问题

时间:2021-07-01 10:21:17 帮助过:2人阅读

我想用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。

求解??

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

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

输出header头信息
curl_setopt($curl, CURLOPT_HEADER, 0);
// 保存到字符串而不是
输出 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // post数据 curl_setopt($curl, CURLOPT_POST, 1); // 请求数据 curl_setopt($curl, CURLOPT_POSTFIELDS, $post); // 是否抓取跳转后的页面 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); $response = curl_exec($curl); curl_close($curl); $xml = simplexml_load_string($response); print_r($xml);

人气教程排行