header("Content-type:text/html;charset=utf-8");
/**
*获取地理位置
*/
function getCity($ip = '')
{
$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
$ip=json_decode(file_get_contents($url),true);
$data = $ip;
return $data;
}
$address = getCity();
/*
获取天气信息接口
http://www.weather.com.cn/data/sk/101010100.html
http://www.weather.com.cn/data/cityinfo/101010100.html
*/
$conn = mysqli_connect("127.0.0.1","root","root","tag","3306") or die('打开失败');//连接数据库
mysqli_set_charset($conn,'utf-8');//设置编码
$sql = 'select * from tag_weather_code where city like "%'.$address['city'].'%" limit 1';//构建查询语句,查询地理位置代码
$res = mysqli_query($conn,$sql);
$res = mysqli_fetch_array($res);
$url = 'http://www.weather.com.cn/data/sk/'.$res['code'].'.html';
$data = file_get_contents($url);//获取接口数据
$result = json_decode($data);//处理json数据
$weatherinfo = $result->weatherinfo;
echo "<pre>";
print_r($weatherinfo);
转载于:https://my.oschina.net/nebulamo/blog/1550612