API獲取天氣預報,以及使用飛信API,給好友發送短信
加上計劃任務,就可以持續每天自動發送天氣預報給自己和親人啦。。。
<?php ########## # jiam 2012/12/5 # [email protected] #自動發送飛信 #中間涉及到飛信接口 #天氣預報API #以及城市代碼 ########## #101180101是鄭州城市代碼 #101180301是新鄉城市代碼 $url='http://m.weather.com.cn/data/101180101.html'; $xinxiang='http://m.weather.com.cn/data/101180301.html'; $w=curl($url); $weather=json_decode($w); $weatherinfo=object_to_array($weather); $info=$weatherinfo['weatherinfo']; $str=$info['date_y'].','.$info['week'].'。'.$info['city'].'今天氣溫:'.$info['temp1'].',天氣:'.$info['weather1'].'有'.$info['wind1'].',風力:'.$info['fx1'].',穿衣建議:'.$info['index_d'].$info['index48_d']; $feixin='http://wjima.a173.cnaaa4.com/feixin/str.php'; $postArray['userName']='手機號';//飛信賬號 $postArray['password']='密碼';//飛信密碼 $postArray['content']=$str;//發送內容 $postArray['other']='接收方手機號';//接收方手機號,無此參數,默認給自己發送 $send=curl($feixin,$postArray); print_r($send);die(); function curl($url, $postFields = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //https 請求 if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if (is_array($postFields) && 0 < count($postFields)) { $postBodyString = ""; $postMultipart = false; foreach ($postFields as $k => $v) { if("@" != substr($v, 0, 1))//判斷是不是文件上傳 { $postBodyString .= "$k=" . urlencode($v) . "&"; } else//文件上傳用multipart/form-data,否則用www-form-urlencoded { $postMultipart = true; } } unset($k, $v); curl_setopt($ch, CURLOPT_POST, true); if ($postMultipart) { curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); } else { curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); } } $reponse = curl_exec($ch); if (curl_errno($ch)) { throw new Exception(curl_error($ch),0); } else { $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if (200 !== $httpStatusCode) { throw new Exception($reponse,$httpStatusCode); } } curl_close($ch); return $reponse; } function object_to_array($obj) { $_arr = is_object($obj) ? get_object_vars($obj) : $obj; foreach ($_arr as $key => $val) { $val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val; $arr[$key] = $val; } return $arr; } ?>