<?php /* 1: 先把下面的代码粘到config.yaml文件中 计划任务为每天8点 appname: zhangya45481 version: 1 cron: - description: feixin_tianqiyubao url: index2.php schedule: every day of month 08:00 2: 在跟目录新建index2.php把下面代码粘进去 */ //中国气象json数据地址 $url = 'http://m.weather.com.cn/data/101210101.html'; //转换返回的数据 $js_arr = json_decode(get_html($url),true); //var_dump($url);die(); $str = '你要查询的地区是: ' . $js_arr['weatherinfo']['city'] . '<br />'; $str .= '今天是: ' . $js_arr['weatherinfo']['date_y'] . '<br />'; $str .= '今天是: ' . $js_arr['weatherinfo']['week'] . '<br />'; $str .= '今天温度是: ' . $js_arr['weatherinfo']['temp1'] . '<br />'; $str .= '今天穿衣指数: ' . $js_arr['weatherinfo']['index'] . ' ' . $js_arr['weatherinfo']['index_d'] . '<br />'; $str .= '48小时穿衣指数: ' . $js_arr['weatherinfo']['index48'] . ' ' . $js_arr['weatherinfo']['index48_d'] . '<br />'; $str .= '紫外线及48小时紫外线: ' . $js_arr['weatherinfo']['index_uv'] . ' ' . $js_arr['weatherinfo']['index48_uv'] . '<br />'; $str .= '洗车: ' . $js_arr['weatherinfo']['index_xc'] . '<br />'; $str .= '旅游: ' . $js_arr['weatherinfo']['index_tr'] . '<br />'; $str .= '舒适指数: ' . $js_arr['weatherinfo']['index_co'] . '<br />'; $str .= '晨练: ' . $js_arr['weatherinfo']['index_cl'] . '<br />'; $str .= '晾晒: ' . $js_arr['weatherinfo']['index_ls'] . '<br />'; $str .= '过敏: ' . $js_arr['weatherinfo']['index_ag'] . '<br /><br />'; //echo $str; $str = get_enhtml($str); //var_dump($str); //载入飞信类 //require './lib/feixin.php'; // 手机号、飞信密码 $fetion = new PHPFetion('', ''); //接收的号码,与天气预报信息 $fetion->send('', $str); echo 'ok!发送成功!'; //字符串检测函数 function utf8_str($str){ $mb = mb_strlen($str,'utf-8'); $st = strlen($str); if($st==$mb) return '纯英文'; if($st%$mb==0 && $st%3==0) return '纯汉字'; return '汉英混合'; } //curl抓取页面 function get_html($url,$options = array()){ $options[CURLOPT_RETURNTRANSFER] = true; $options[CURLOPT_TIMEOUT] = 5; $ch = curl_init($url); curl_setopt_array($ch,$options); $html = curl_exec($ch); curl_close($ch); if($html === false){ return false; } return $html; } //过滤html为纯文本 function get_enhtml($string) { $pattern=array ("'<script[^>]*?>.*?</script>'si",// 去掉 javascript "'<style[^>]*?>.*?</style>'si",// 去掉 HTML 标记 "'<[/!]*?[^<>]*?>'si",//去掉 HTML 标记 "'<!--[/!]*?[^<>]*?>'si", // 去掉 注释标记 "'([rn])[s]+'", // 去掉空白字符 "'&(quot|#34);'i", "'&(amp|#38);'i", "'&(lt|#60);'i", "'&(gt|#62);'i", "'&(nbsp|#160);'i", "'&(iexcl|#161);'i", "'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i", "'&#(d+);'e"); $replace=array ("", "", "", "", "1", "", "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(1)"); $string=preg_replace($pattern, $replace, $string); $string=preg_replace("/<(.*?)>/","",$string); $string=str_replace("n","",$string); $string=str_replace("r","",$string); $string=str_replace(" ","",$string); $string=str_replace(" ","",$string); return addslashes(trim($string)); } /** * PHP飞信发送类 * * @author quanhengzhuang <blog.quanhz.com> * @version 1.5.0 */ class PHPFetion { /** * 发送者手机号 * @var string */ protected $_mobile; /** * 飞信密码 * @var string */ protected $_password; /** * Cookie字符串 * @var string */ protected $_cookie = ''; /** * Uid缓存 * @var array */ protected $_uids = array(); /** * csrfToken * @var string */ protected $_csrfToten = null; /** * 构造函数 * @param string $mobile 手机号(登录者) * @param string $password 飞信密码 */ public function __construct($mobile, $password) { if ($mobile === '' || $password === '') { return; } $this->_mobile = $mobile; $this->_password = $password; $this->_login(); } /** * 析构函数 */ public function __destruct() { $this->_logout(); } /** * 登录 * @return string */ protected function _login() { $uri = '/huc/user/space/login.do?m=submit&fr=space'; $data = 'mobilenum='.$this->_mobile.'&password='.urlencode($this->_password); $result = $this->_postWithCookie($uri, $data); //解析Cookie preg_match_all('/.*?rnSet-Cookie: (.*?);.*?/si', $result, $matches); if (isset($matches[1])) { $this->_cookie = implode('; ', $matches[1]); } $result = $this->_postWithCookie('/im/login/cklogin.action', ''); return $result; } /** * 向指定的手机号发送飞信 * @param string $mobile 手机号(接收者) * @param string $message 短信内容 * @return string */ public function send($mobile, $message) { if ($message === '') { return ''; } //判断是给自己发还是给好友发 if ($mobile == $this->_mobile) { return $this->_toMyself($message); } else { $uid = $this->_getUid($mobile); return $uid === '' ? '' : $this->_toUid($uid, $message); } } /** * 获取飞信ID * @param string $mobile 手机号 * @return string */ protected function _getUid($mobile) { if (empty($this->_uids[$mobile])) { $uri = '/im/index/searchOtherInfoList.action'; $data = 'searchText='.$mobile; $result = $this->_postWithCookie($uri, $data); //匹配 preg_match('/toinputMsg.action?touserid=(d+)/si', $result, $matches); $this->_uids[$mobile] = isset($matches[1]) ? $matches[1] : ''; } return $this->_uids[$mobile]; } /** * 获取csrfToken,给好友发飞信时需要这个字段 * @param string $uid 飞信ID * @return string */ protected function _getCsrfToken($uid) { if ($this->_csrfToten === null) { $uri = '/im/chat/toinputMsg.action?touserid='.$uid; $result = $this->_postWithCookie($uri, ''); preg_match('/name="csrfToken".*?value="(.*?)"/', $result, $matches); $this->_csrfToten = isset($matches[1]) ? $matches[1] : ''; } return $this->_csrfToten; } /** * 向好友发送飞信 * @param string $uid 飞信ID * @param string $message 短信内容 * @return string */ protected function _toUid($uid, $message) { $uri = '/im/chat/sendMsg.action?touserid='.$uid; $csrfToken = $this->_getCsrfToken($uid); $data = 'msg='.urlencode($message).'&csrfToken='.$csrfToken; $result = $this->_postWithCookie($uri, $data); return $result; } /** * 给自己发飞信 * @param string $message * @return string */ protected function _toMyself($message) { $uri = '/im/user/sendMsgToMyselfs.action'; $result = $this->_postWithCookie($uri, 'msg='.urlencode($message)); return $result; } /** * 退出飞信 * @return string */ protected function _logout() { $uri = '/im/index/logoutsubmit.action'; $result = $this->_postWithCookie($uri, ''); return $result; } /** * 携带Cookie向f.10086.cn发送POST请求 * @param string $uri * @param string $data */ protected function _postWithCookie($uri, $data) { $fp = fsockopen('f.10086.cn', 80); fputs($fp, "POST $uri HTTP/1.1rn"); fputs($fp, "Host: f.10086.cnrn"); fputs($fp, "Cookie: {$this->_cookie}rn"); fputs($fp, "Content-Type: application/x-www-form-urlencodedrn"); fputs($fp, "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1rn"); fputs($fp, "Content-Length: ".strlen($data)."rn"); fputs($fp, "Connection: closernrn"); fputs($fp, $data); $result = ''; while (!feof($fp)) { $result .= fgets($fp); } fclose($fp); return $result; } } /* { "weatherinfo": { <!-- 基本信息 --> "city":"北京", "city_en":"beijing", "date_y":"2012年8月14日", "date":"", "week":"星期二", "fchh":"18", "cityid":"101010100", <!-- 从今天开始到第六天的每天的天气情况,这里的温度是摄氏温度 --> "temp1":"19℃~31℃", "temp2":"21℃~31℃", "temp3":"22℃~28℃", "temp4":"20℃~28℃", "temp5":"21℃~29℃", "temp6":"22℃~31℃", <!-- 从今天开始到第六天的每天的天气情况,这里的温度是华氏温度 --> "tempF1":"66.2℉~87.8℉", "tempF2":"69.8℉~87.8℉", "tempF3":"71.6℉~82.4℉", "tempF4":"68℉~82.4℉", "tempF5":"69.8℉~84.2℉", "tempF6":"71.6℉~87.8℉", <!-- 天气描述 --> "weather1":"阵雨转晴", "weather2":"晴", "weather3":"多云转雷阵雨", "weather4":"雷阵雨", "weather5":"多云转阴", "weather6":"雷阵雨", <!-- 天气描述图片序号 --> "img1":"3", "img2":"0", "img3":"0", "img4":"99", "img5":"1", "img6":"4", "img7":"4", "img8":"99", "img9":"1", "img10":"2", "img11":"4", "img12":"99", "img_single":"0", <!-- 图片名称 --> "img_title1":"阵雨", "img_title2":"晴", "img_title3":"晴", "img_title4":"晴", "img_title5":"多云", "img_title6":"雷阵雨", "img_title7":"雷阵雨", "img_title8":"雷阵雨", "img_title9":"多云", "img_title10":"阴", "img_title11":"雷阵雨", "img_title12":"雷阵雨", "img_title_single":"晴", <!-- 风速描述 --> "wind1":"微风", "wind2":"微风", "wind3":"微风", "wind4":"微风", "wind5":"微风", "wind6":"微风", "fx1":"微风", "fx2":"微风", <!-- 风速级别描述 --> "fl1":"小于3级", "fl2":"小于3级", "fl3":"小于3级", "fl4":"小于3级", "fl5":"小于3级", "fl6":"小于3级", <!-- 今天穿衣指数 --> "index":"炎热", "index_d":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫等清凉夏季服装。", <!-- 48小时穿衣指数 --> "index48":"炎热", "index48_d":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫等清凉夏季服装。", <!-- 紫外线及48小时紫外线 --> "index_uv":"很强", "index48_uv":"强", <!-- 洗车 --> "index_xc":"较不宜", <!-- 旅游 --> "index_tr":"适宜", <!-- 舒适指数 --> "index_co":"较不舒适", "st1":"31", "st2":"17", "st3":"31", "st4":"22", "st5":"27", "st6":"22", <!-- 晨练 --> "index_cl":"适宜", <!-- 晾晒 --> "index_ls":"极适宜", <!-- 过敏 --> "index_ag":"易发" } } */