// 查询天气详情 需要传递一个城市名称与对应的天气编码
private function getWeatherInfo($cityName, $code)
{
$ch = curl_init();
$url = 'http://apis.baidu.com/apistore/weatherservice/recentweathers?cityname=' . urlencode($cityName) . '&cityid=' . $code . '';
$header = array(
'apikey:9b3bc8af61328f514eff3190ef5d5134',
);
// 添加apikey到header
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行HTTP请求
curl_setopt($ch, CURLOPT_URL, $url);
$res = curl_exec($ch);
return json_decode($res, true);
}
// 按照城市的拼音名称查询城市天气的code编码
private function getCityPinyin($cityInfo)
{
$ch = curl_init();
$url = 'http://apis.baidu.com/apistore/weatherservice/weather?citypinyin=' . $cityInfo . '';
$header = array(
'apikey:9b3bc8af61328f514eff3190ef5d5134',
);
// 添加apikey到header
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行HTTP请求
curl_setopt($ch, CURLOPT_URL, $url);
$res = curl_exec($ch);
return json_decode($res, true);
}
// 按照中文名称获取城市天气code编码信息
private function getCityInfo($cityInfo)
{
$ch = curl_init();
$url = 'http://apis.baidu.com/apistore/weatherservice/cityinfo?cityname=' . $cityInfo;
$header = array(
'apikey: 9b3bc8af61328f514eff3190ef5d5134',
);
// 添加apikey到header
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行HTTP请求
curl_setopt($ch, CURLOPT_URL, $url);
$res = curl_exec($ch);
return json_decode($res, true);
}