利用飞信给自己发送天气预报短信(刚刚写完,实测百分百有效)
程序员文章站
2024-01-11 11:49:16
...
php代码
<?php //采集反应时间设置 set_time_limit(0); //天气预报采集目标网址 杭州天气预报 $url = 'http://www.weather.com.cn/weather/101210101.shtml'; //实例化采集机器 $gather = new gather(); //获取目标网址HTML $html = $gather->geturlfile($url); //获取内容 $start = '<p class="weatherYubaoBox">'; $end = '<p id="weatherYubao2" class="weatherYubao">'; $content = $gather->get_sub_content($html, $start, $end); $text = substr(SpHtml2Text($content), 42); //载入飞信类(类自己到网上搜) require './lib/PHPFetion.php'; // 手机号、飞信密码 $fetion = new PHPFetion('xxxxxxxxxxx', 'xxxxxxxx'); //发送的号码,与天气预报信息 $fetion->send('18758056856', $text); echo 'ok!发送成功!'; /** * 采集类 */ class gather { public $pagestring = ''; private $db; function __construct() { global $db; $this->db = $db; } function geturlfile($url) { $url = trim($url); $content = ''; if (extension_loaded('curl')) { $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('/<a.+?href=["|\']?([^>"\' ]+)["|\']?\s*[^>]*>([^>]+)<\/a>/is', $code, $arr); return array('name' => $arr[2], 'url' => $arr[1]); } function get_sub_content($str, $start, $end) { $start = trim($start); $end = trim($end); if ($start == '' || $end == '') { 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</pre>\r\n"; echo "</p>"; } } //html转text function SpHtml2Text($str) { $str = preg_replace("/<sty(.*)\/style>|<scr(.*)\/script>|<!--(.*)-->/isU","",$str); $alltext = ""; $start = 1; for($i=0;$i<strlen($str);$i++) { if($start==0 && $str[$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; } ?>