Home  >  Article  >  Backend Development  >  PHP uses cURL to call WebService to obtain weather information

PHP uses cURL to call WebService to obtain weather information

WBOY
WBOYOriginal
2016-07-29 09:12:071074browse
<?php
/**
 * 用cURL调用WebService获取天气信息
 * User: Ollydebug
 * Date: 2015/11/11
 * Time: 19:44
 */

//在WeatherWs的服务器上,默认大连城市的 theCityCode = 864

$data = &#39;theCityCode=864&theUserID=&#39;;
$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)));
curl_setopt($curlobj, CURLOPT_USERAGENT, &#39;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36&#39;);

$rtn = curl_exec($curlobj);
if(!curl_errno($curlobj)){
    echo $rtn;
}else{
    echo &#39;Curl error: &#39;.curl_errno($curlobj);
}
curl_close($curlobj);

?>

The above introduces how PHP uses cURL to call WebService to obtain weather information, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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