>백엔드 개발 >PHP 튜토리얼 >PHP使用cURL调用WebService的问题

PHP使用cURL调用WebService的问题

WBOY
WBOY원래의
2016-06-06 20:27:111393검색

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

我写的代码:

<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>

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

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

如果不加入theUserID又会提示:

<code>缺少参数: theUserID。</code>

求解??

回复内容:

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

我写的代码:

<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>

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

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

如果不加入theUserID又会提示:

<code>缺少参数: theUserID。</code>

求解??

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

网上大把简单的接口不用。
截图:
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>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.