<?phperror_reporting(0);set_time_limit(0);include_once('feed.php');// 这是用户数据
include_once('database.php');// 这是城市数据
$mobile='13800138000';// 用来发送飞信的手机号
$mobilePwd='123456';// 飞信密码
$to=$FeedUsers;// Import user data.
$cityList=$cityDatabase;// Import city data.
$serviceIntro='(此服务由Joker强力驱动,详情访问http://mynook.info/)';// 很无耻地加了点附加信息~~
$sendBaseUrl='http://'.$_SERVER['HTTP_HOST'].'/pafetion1.4.php';// 定义pafetion的路径
functiongetWeatherData($cId){// 这个比较简单,从中国天气网获取天气信息的函数
if(empty($cId)){returnfalse;}$wurl='http://m.weather.com.cn/data/'.$cId.'.html';$json=file_get_contents($wurl);$json=get_object_vars(json_decode($json));$json=get_object_vars($json['weatherinfo']);$Weah=$json['weather2'];$Temp=$json['temp2'];$tomo=array('Weah'=>$Weah,'Temp'=>$Temp);return$tomo;}function_get_http_result($_url){// 发送http请求的函数,因为不想每一次都curl整个来一遍,所以分离出来了
if(empty($_url)){returnfalse;}$ch=curl_init();curl_setopt($ch,CURLOPT_URL,"$_url");curl_setopt($ch,CURLOPT_TIMEOUT,8);curl_setopt($ch,CURLOPT_HEADER,0);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);$result=curl_exec($ch);$info=curl_getinfo($ch);curl_close($ch);if($result){return'Success';}}foreach($toas$cityName=>$group){//外层循环,对于每个组查询一次天气信息
$cityId=$cityList[$cityName];// Get every city's id according to city's name.
$groupUsers=$to[$cityName];// Get th users of current group.
$tomorrow=getWeatherData($cityId);// Get current city's weather.
foreach($groupas$user){// Get users in current group.// 内层循环,针对每个用户生成不同短信内容
$msg=$user['nick'].',您好!'.$cityName.'明天'.$tomorrow['Weah'].',温度'.$tomorrow['Temp'].',关注天气变化,注意身体哦~'.$serviceIntro;$url=$sendBaseUrl.'?phone='.$mobile.'&pwd='.$mobilePwd.'&to='.$user['mobileNo'].'&u=1&msg='.$msg;// 生成短信请求的URL
$sendResult=_get_http_result($url);if($sendResult=='Success'){echo'Success';}else{echo'Fail';}sleep(1);}}?>
这个服务使用到了 pafetion 的飞信 API ,详情参看作者的博客:<http://3haku.net/tag/pafetion “pafetion”>。城市数据由知友的原版天气预报订阅服务的源码提取:http://zhi.hu/DFWO。