Home >php教程 >PHP源码 >利用飞信给自己发送天气预报短信(刚刚写完,实测百分百有效)

利用飞信给自己发送天气预报短信(刚刚写完,实测百分百有效)

PHP中文网
PHP中文网Original
2016-05-25 17:06:121259browse

php代码

<?php
//采集反应时间设置
set_time_limit(0);

//天气预报采集目标网址   杭州天气预报
$url     = &#39;http://www.weather.com.cn/weather/101210101.shtml&#39;;

//实例化采集机器
$gather  = new gather();
//获取目标网址HTML
$html    = $gather->geturlfile($url);
//获取内容
$start   = &#39;<p class="weatherYubaoBox">&#39;;
$end     = &#39;<p id="weatherYubao2" class="weatherYubao">&#39;;
$content = $gather->get_sub_content($html, $start, $end);
$text 	 = substr(SpHtml2Text($content), 42);

//载入飞信类(类自己到网上搜)
require &#39;./lib/PHPFetion.php&#39;;	
// 手机号、飞信密码
$fetion = new PHPFetion(&#39;xxxxxxxxxxx&#39;, &#39;xxxxxxxx&#39;); 
//发送的号码,与天气预报信息
$fetion->send(&#39;18758056856&#39;, $text);

echo &#39;ok!发送成功!&#39;;


/**
* 采集类
*/
class gather {

    public $pagestring = &#39;&#39;;
    private $db;

    function __construct() {
        global $db;
        $this->db = $db;
    }

    function geturlfile($url) {
        $url = trim($url);
        $content = &#39;&#39;;
        if (extension_loaded(&#39;curl&#39;)) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            $content = curl_exec($ch);
            curl_close($ch);
        } else {
            $content = file_get_contents($url);
        }
        return trim($content);
    }

    function s($code) {
        preg_match_all(&#39;/<a.+?href=["|\&#39;]?([^>"\&#39; ]+)["|\&#39;]?\s*[^>]*>([^>]+)<\/a>/is&#39;, $code, $arr);
        return array(&#39;name&#39; => $arr[2], &#39;url&#39; => $arr[1]);
    }

    function get_sub_content($str, $start, $end) {
        $start = trim($start);
        $end = trim($end);
        if ($start == &#39;&#39; || $end == &#39;&#39;) {
            return $str;
        }
        $str = explode($start, $str);
        $str = explode($end, $str[1]);
        return $str[0];
    }

    function vd($var) {
        echo "<p style=\"border:1px solid #ddd;background:#F7F7F7;padding:5px 10px;\">\r\n";
        echo "<pre style=\"font-family:Arial,Vrinda;font-size:14px;\">\r\n";
        var_dump($var);
        echo "\r\n
\r\n"; echo "

"; } } //html转text function SpHtml2Text($str) { $str = preg_replace("/||/isU","",$str); $alltext = ""; $start = 1; for($i=0;$i") { $start = 1; } else if($start==1) { if($str[$i]=="<") { $start = 0; $alltext .= " "; } else if(ord($str[$i])>31) { $alltext .= $str[$i]; } } } $alltext = str_replace(" "," ",$alltext); $alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext); $alltext = preg_replace("/[ ]+/s"," ",$alltext); return $alltext; } ?>
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
Previous article:图像生成器Next article:我在想练习PHP