好久没更新了,最近在写《悦帮到家》小程序,预计下周写完了,挺不错的小程序,社区服务类的
项目中获取城市天气预报,可以用百度的,这里还涉及了模拟get,学习到了
直接展示示例
<?php
//注意$weatherURL: ***为com
$resdata = json_decode(getWeather('珠海'),true);
print_r($resdata);
/**
* 根据城市名称/ID获取详细天气预报
* @param string $city [城市名称/ID]
* @return array
*/
function getWeather($city){
//百度天气接口API
$location = $city; //地区
$ak = "8lIbBbCrSBQ9If60RSoIOsjt"; //秘钥,需要申请,百度为了防止频繁请求
//注意: ***为com
$weatherURL = "http://api.map.baidu***/telematics/v3/weather?location=$location&output=json&ak=$ak";
$res = httpGet($weatherURL);
return $res;
}
/**
* 模拟get进行url请求
* @param string $url
* @return json
*/
function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
?>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号