最近一直研究公众微信号码,目前已经实现查询天气预报、PM2.5、点歌等相关功能,为了便于有共同爱好的伙伴们少走些弯路,我共享下天气预报查询的源码;
基础类:
class Weather
{
public function get_Weather($value)
{
$qurl="http://api2.sinaapp.com/search/weather/?appkey=0020130430&appsecert=fa6095e113cd28fd&reqtype=text&keyword=".$value;
$content=file_get_contents($qurl);
$result=json_decode($content,true);//json数据的解析
if($result['text']['content']== NULL)
{
return array('weather'=>"对不起,查询天气失败,请稍后再试!");
}
else
{
return array('weather'=>$result['text']['content']);//返回数组的形式
}
}
}
调用方法:
$stock=new Weather();
$results=$stock->get_Weather($cityname);
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $results['weather']);
echo $resultStr;
exit;
经过我试验没问题了,可以关注我的微信,看看效果