Home >Backend Development >PHP Tutorial >php结合飞信 免费天气预报短信_php实例

php结合飞信 免费天气预报短信_php实例

WBOY
WBOYOriginal
2016-06-07 17:26:23703browse

开始教你怎么弄
一、需要有一个PHP环境。为了图省事下载APMServ软件快速搭建PHP环境
二、下载天气预报短信通知PHP程序点这里下载代码
三、修改PHP程序配置
用记事本打开WeatherForecast.php文件

复制代码 代码如下:


/**
* 飞信通知天气预报
* 2009-4-23
*/

//禁止执行时间限制
set_time_limit (0);

//设置MB编码
mb_internal_encoding("GB2312");

/**
* 采集新浪天气预报信息
*
* @param string $city    城市名称
* @return unknown
*/
function getWF($city){

    if(empty($city)) return "";
    $wf=@file_get_contents('http://php.weather.sina.com.cn/search.php?city='.urlencode($city).'&f=1&dpc=1');
    if (empty($wf)){
        return "";
    }

    //截取相关信息
    $return = "";
    $star=strpos($wf,"
");
    $return = substr($wf,$star,1000);
    $end2=strpos($return,"");
    $return = strip_tags(substr($return,0,$end2));

    
    //过滤
    $return = str_replace(" ","",$return);
    $return = str_replace(" ","",$return);
    $return = str_replace("\t","",$return);
    $return = str_replace("\n","",$return);
    $return = str_replace("℃","度",$return);
    $return = str_replace("≤","",$return);
    $return = trim(str_replace("\r",",",$return),",");
    $return = str_replace("~","-",$return);
    $return = str_replace(":",":",$return);

    //返回值
    return $return;
}

//飞信账号设置
$Fetion['user']='';
$Fetion['pass']='';

//设置天气预报城市
$citys[]=array('id'=>'bj','name'=>'北京');
$citys[]=array('id'=>'sy','name'=>'沈阳');

//北京手机号
$tel['bj'][]='137XXXXXXX';
$tel['bj'][]='137XXXXXXX';
//沈阳手机号
$tel['sy'][]='138XXXXXXXX';
$tel['sy'][]='137XXXXXXX';


if (is_array($citys)&&count($citys)>0){

    //遍历城市
    foreach($citys as $city){

        //获取采集信息
        $info=getWF($city['name']);

        if (!empty($info)){
            if (is_array($tel[$city['id']])&&count($tel[$city['id']])>0){

                //大信息量处理
                if (mb_strlen($info)>171){
                    $sum=ceil(mb_strlen($info)/168)-1;
                    for($i=0;$i                        $starj=168*$i;
                        $qInfo=mb_substr($info,$starj,168);
                        $n=$i+1;
                        foreach($tel[$city['id']] as $val){
                            $url="http://sms.api.bz/fetion.php?username=".$Fetion['user']."&password=".$Fetion['pass']."&sendto=".$val."&message=".urlencode("天气预报[".$n."],"."明天".date("Y年m月d日")." ".$city['name']." ".$qInfo);
                            @file_get_contents($url);
                        }
                    }
                }else{
                    foreach($tel[$city['id']] as $val){
                        $url="http://sms.api.bz/fetion.php?username=".$Fetion['user']."&password=".$Fetion['pass']."&sendto=".$val."&message=".urlencode("天气预报,明天".date("Y年m月d日")." ".$city['name']." ".$info);
                        @file_get_contents($url);
                    }
                }
            }
        }
    }
}
?>

修改
复制代码 代码如下:

//飞信账号设置
$Fetion['user']='自己的飞信账号手机号';
$Fetion['pass']='登录飞信密码';

你所在的城市修改
复制代码 代码如下:

//设置天气预报城市
$citys[]=array('id'=>'bj','name'=>'北京');

修改你要收到天气预报的手机号码可以为多个
复制代码 代码如下:

//北京手机号
$tel['bj'][]='137XXXXXXX';
$tel['bj'][]='137XXXXXXX';

这里的bj要与设置城市中的bj相同
好了配置完成可以测试执行一下是否可以收到天气预报信息。
四、设置每日定时发送
如果是WINDOWS环境设置计划任务
开始->程序->附近->系统工具->计划任务
添加任务 根据提示操作 重点在执行程序选择PHP.exe
选择你安装APMServ所在位子我本地的是D:\APMServ5.2.6\PHP\php.exe WeatherForecast.php绝对路径
我本地为 D:\APMServ5.2.6\PHP\php.exe D:\APMServ5.2.6\www\htdocs\WeatherForecast.php
完成。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn