今天在看某娱乐网时候,发现一个源码,是"全国城市,天气查询html源码",啥?html?
于是我把源码下载下来,还真是html,还有js加载接口,于是我自己有了一个写天气预报查询接口,然后我找到他那个js文件里面的接口,是https://sapi.k780.com/?app=weather.future&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&jsoncallback=getWeather&weaid=
然后就是传一个地点过去,然后就返回最近7日的天气,ok,完美于是我开写
这次难度有点大,要用到(正则表达式、CURL、json)
中途....
完成...
ok,完美,写好了
我的接口地址:http://api.weilaicloudy.com/cgi-bin/roots/weater.php?location=地点
请求方式:get
请求数据:location 地点
代码如下:
<?php
header("Content-type:application:json;charset=utf-8");
function get_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$httpheader[] = "Accept:*/*";
$httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
$httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
$httpheader[] = "Connection:close";
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if($post){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if($header){
curl_setopt($ch, CURLOPT_HEADER, TRUE);
}
if($cookie){
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if($referer){
if($referer==1){
curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
}else{
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
}
if($ua){
curl_setopt($ch, CURLOPT_USERAGENT,$ua);
}else{
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.4.1; zh-cn; R815T Build/JOP40D) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/4.5 Mobile Safari/533.1');
}
if($nobaody){
curl_setopt($ch, CURLOPT_NOBODY,1);
}
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
$l = $_GET['location'];
if(!$l)
{
exit('{"code":-1,"msg":"地点不能为空"}');
}
//第一步,取出json,去掉无用数据
$Data = get_curl('https://sapi.k780.com/?app=weather.future&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&jsoncallback=getWeather&weaid='.$_GET["location"]);
preg_match_all('/getWeather(\(.*?)\)/is',$Data,$v);
//第二步,去掉第一个字符(
$Data = substr($v[1][0],1);
//第3步,解析json
$Data = json_decode($Data,true);
if(!$Data["success"])
{
exit('{"code":-1,"msg":"地点有误"}');
}
//第4步,遍历数据流 完成
echo '{"code":200,"msg":"ok","data":[';
foreach($Data["result"] as $key=>$v)
{
echo '{"days":"'.$v["days"].'","week":"'.$v["week"].'","temperature":"'.$v["temperature"].'","weather":"'.$v["weather"].'","wind":"'.$v["wind"].'"},';
}
echo '{"status":true,"location":"'.$l.'","time":"'.time().'"}]}';
exit;
?>

