1 Star 1 Fork 1

youngyezi / weather

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
getWeather.php 1.37 KB
一键复制 编辑 原始数据 按行查看 历史
zhangming 提交于 2014-03-15 20:56 . 对象的形式
<?php
class Weather
{
private $_dbFile;
public function __construct($dbFile=null)
{
if (!is_null($dbFile) and is_file($dbFile)) {
$this->_dbFile = $dbFile;
}
}
/**
* 获取城市信息
*
* @param str $cityCode [description]
*
* @return array
*/
private function _getWeatherInfo ($cityCode)
{
// api 链接
$url= "http://m.weather.com.cn/data/".$cityCode.".html";
$ch = curl_init();
$timeout = 10;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$weatherInfo = curl_exec($ch);
curl_close($ch);
return json_decode($weatherInfo, true);
}
public function getWeatherInfo()
{
header("Content-Type: text/html; charset=UTF-8");
$city = $_POST['city'];
$cityDb = $this->_getCityDb();
if(array_key_exists($city, $cityDb)){
echo json_encode($this->_getWeatherInfo($cityDb[$city]));
}else{
echo '{"error_response": "您输入的城市不正确"}';
}
}
private function _getCityDb()
{
return include $this->_dbFile;
}
}
$webO = new Weather('./cityDb.php');
$webO->getWeatherInfo();
PHP
1
https://gitee.com/youngyezi/weather.git
git@gitee.com:youngyezi/weather.git
youngyezi
weather
weather
master

搜索帮助