本篇文章给大家带来的内容是关于php如何使用cURL调用WebService来获取天气信息(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
相关代码如下:
<?php
$data = 'theCityName=杭州';
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName");
curl_setopt($curl,CURLOPT_HEADER,0);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
curl_setopt($curl,CURLOPT_HTTPHEADER,array(
"application/x-www-form-urlencoded;
charset=utf-8",
"Content-length: ".strlen($data)
));
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36');
$result = curl_exec($curl);
if (!curl_errno($curl)) {
echo $result;
} else {
echo 'Curl error:' . curl_error($curl);
}
curl_close($curl);相关推荐:
以上就是php如何使用cURL调用WebService来获取天气信息(代码)的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号