Home >Backend Development >PHP Tutorial >PHP使用cURL调用WebService的问题
我想用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>
求解??
自己看下吧[超时什么的错误判断自己处理],真不知道怎么说你,不看文档还要玩高难度的。
网上大把简单的接口不用。
截图:
<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>