Home > Backend Development > PHP Tutorial > PHP使用cURL调用WebService的问题

PHP使用cURL调用WebService的问题

WBOY
Release: 2016-06-06 20:27:11
Original
1398 people have browsed it

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

<code>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</code>
Copy after login
Copy after login

我写的代码:

<code>$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);</code>
Copy after login
Copy after login

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

<code>发现错误:用户验证失败。http://www.webxml.com.cn/</code>
Copy after login
Copy after login

如果不加入theUserID又会提示:

<code>缺少参数: theUserID。</code>
Copy after login
Copy after login

求解??

回复内容:

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

<code>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</code>
Copy after login
Copy after login

我写的代码:

<code>$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);</code>
Copy after login
Copy after login

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

<code>发现错误:用户验证失败。http://www.webxml.com.cn/</code>
Copy after login
Copy after login

如果不加入theUserID又会提示:

<code>缺少参数: theUserID。</code>
Copy after login
Copy after login

求解??

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

网上大把简单的接口不用。
截图:
PHP使用cURL调用WebService的问题

<code><?php $post='theCityCode='.urlencode('广州').'&theUserID=';
// 初始化
$curl = curl_init('http://www.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather');
$header = array();
$header[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$header[] = 'Accept-Encoding: gzip, deflate';
$header[] = 'Accept-Language: zh-CN,zh;q=0.8';
$header[] = 'Cache-Control: max-age=0';
$header[] = 'Content-Length: '.strlen($post);
$header[] = 'Content-Type: application/x-www-form-urlencoded';
$header[] = 'Host: www.webxml.com.cn';
$header[] = 'Origin: http://www.webxml.com.cn';
$header[] = 'Proxy-Connection: keep-alive';
$header[] = 'Referer: http://www.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather';
$header[] = 'Upgrade-Insecure-Requests: 1';
$header[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36';
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
// 不输出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);</code></code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template