微信php翻译和天气预报整合,微信公众平台天气预报功能开发
本來是想自己直接從中國天氣網獲取信息并處理,后來發現處理起來太麻煩,而且要獲取所有城市的城市編碼,再有就是!不支持國外天氣!!(我們學校有很多畢業生在國外上學,所以我考慮還是做出支持國外天氣的版本)
因此考慮直接調用別人的API,一開始選用了方倍工作室已經做好的接口(無奈也沒有國外)。直到有一天返回北京天氣溫度是零下的時候(當時天熱到不敢出門)。。。換!換!換!
后來終于發現最靠譜的接口----百度的天氣API。
廢話少說,首先大家要上 百度地圖API申請一個專用key(大概一分鐘就搞定了,很方便)
代碼如下:
//圖文信息2 for 天氣【這是微信的圖文信息模板】
$tqTpl = "
%s
5
";//最后發現最多只能看到五個item= =無語。。。
PHP代碼
if(substr_count($keyword,'天氣')!=0 && $keyword!='天氣')
{
$geshu = substr_count($keyword,'天氣');
$t = explode("天氣",$keyword);
for($i=0;$i<=$geshu;$i++)
{
if($t[$i]!='')
{
$city = $t[$i];
break;
}
}
$mykey = "【這里換成你的API key】";
$url = "http://api.map.baidu.com/telematics/v3/weather?location=".$city."&output=json&ak=".$mykey;
$output = file_get_contents($url);
$contentStr = json_decode($output, true);
if($contentStr['status']=='success')
{
$T[0]['Title']=$contentStr['date']." ".$contentStr['results'][0]['currentCity']."天氣";
//$T[0]['PicUrl']="http://zhengwairen-try2.stor.sinaapp.com/xytq.jpg";
//$T[0]['Url']="http://zhengwairen-try2.stor.sinaapp.com/xytq.jpg";
//上面兩行可以設成你想要的頂部圖片
if(is_array($contentStr['results'][0]['index']))
{
$T[2]['Title']="【pm2.5】".$contentStr['results'][0]['pm25']."n"."【".$contentStr['results'][0]['index'][0]['title']."】"."(".$contentStr['results'][0]['index'][0]['zs'].") ".$contentStr['results'][0]['index'][0]['des'];
//下一行是洗車指數,感覺不對主題還是不要的好。。
//$T[2]['Title']=$T[2]['Title']."n"."【".$contentStr['results'][0]['index'][1]['title']."】(".$contentStr['results'][0]['index'][1]['zs'].") ".$contentStr['results'][0]['index'][1]['des'];
$T[2]['Title']=$T[2]['Title']."n"."【".$contentStr['results'][0]['index'][2]['title']."】(".$contentStr['results'][0]['index'][2]['zs'].") ".$contentStr['results'][0]['index'][2]['des'];
}
else
$guowai=1;
for($i=1,$aaa=0;$i<=5;$i++)
{
if($i==2 && $guowai!=1)
continue;
if($guowai==1 && $i==5)
continue;
$T[$i]['Title']=$contentStr['results'][0]['weather_data'][$aaa]['date']." ".$contentStr['results'][0]['weather_data'][$aaa]['temperature']." ".$contentStr['results'][0]['weather_data'][$aaa]['weather']." ".$contentStr['results'][0]['weather_data'][$aaa]['wind'];
$T[$i]['PicUrl']=$contentStr['results'][0]['weather_data'][$aaa]['dayPictureUrl'];
$T[$i]['Url']=$contentStr['results'][0]['weather_data'][$aaa]['dayPictureUrl'];
$aaa++;
}
$tianqi = sprintf($tqTpl,$fromUsername,$toUsername,time(),"news",$T[0]['Title'],$T[0]['Description'],$T[0]['PicUrl'],$T[0]['Url'], $T[1]['Title'],$T[1]['Description'],$T[1]['PicUrl'],$T[1]['Url'], $T[2]['Title'],$T[2]['Description'],$T[2]['PicUrl'],$T[2]['Url'], $T[3]['Title'],$T[3]['Description'],$T[3]['PicUrl'],$T[3]['Url'], $T[4]['Title'],$T[4]['Description'],$T[4]['PicUrl'],$T[4]['Url'],$T[5]['Title'],$T[5]['Description'],$T[5]['PicUrl'],$T[5]['Url']);
echo $tianqi;
}
}
效果圖如下:
將其中的$mykey變量改成自己的APIkey就行了。另外有一點讓我不解的是,我微信用的圖文模板明明是6個item,為什么回復實際效果只有5個呢= = 求廣大網友幫忙解釋
最后附代碼說明。用戶發送“北京天氣”和“天氣北京”都是可以的,所以首先做了字符串處理,得到正確的城市名。利用百度地圖API給的網址,發出GET請求(其實就是直接訪問),獲取一個json類型的數據包。將數據包信息轉成數組格式,對應的回復微信用戶即可。國外的天氣沒有當天天氣的具體說明,所以最后做了一點小修改。
PHP中對各種加密算法、Hash算法的速度測試對比代碼
PHP的Hash算法是比較常用的,現在的MD5有時候不太安全,就得用到Hash_algos()中的其它算法,下面進行了一個性能的比較。php代碼:define('testtime',50000);$algos
php發送get、post請求的6種方法簡明總結
方法1:用file_get_contents以get方式獲取內容:php$url='http://www.gimoo.net/';$html=file_get_contents($url);echo$html;方法2:用fopen打開url,以get方式獲取內容:php$fp=fopen($url,
PHP大批量插入數據庫的3種方法和速度對比
第一種方法:使用insertinto插入,代碼如下:$params=array(‘value'='50′);set_time_limit(0);echodate(H:i:s);for($i=0;$i2000000;$i++){$connect_mysql-insert($params);};echodate(H:i:s);
總結
以上是生活随笔為你收集整理的微信php翻译和天气预报整合,微信公众平台天气预报功能开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos 下载文件很慢_CentOS
- 下一篇: java文件服务器_JavaWeb项目架