欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-09 21:20:04
开始教你怎么弄 一、需要有一个php环境。为了图省事下载apmserv软件快速搭建php环境 二、下载天气预报短信通知php程序点这里下载代码 三、修改php程序配置 用记...
开始教你怎么弄
一、需要有一个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,"<div class=\"blk-s3\">");
    $return = substr($wf,$star,1000);
    $end2=strpos($return,"<!-- box end-->");
    $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<=$sum;$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
完成。