標籤:class blog code http com get
<?php/** ┃ ┃ code is far away from bug with the animal protecting ┃ ┃ 神獸保佑,代碼無bug ┃ ┗━━━┓ ┃ ┣┓ ┃ ┏┛ ┗┓┓┏━┳┓┏┛ ┃┫┫ ┃┫┫ ┗┻┛ ┗┻┛ * @author 鄒顥 [email protected]
2014-06-12 */class Fetion{private $_url='http://quanapi.sinaapp.com/fetion.php';private $_username;//寄件者 飛信帳號private $_password; //寄件者 飛信密碼/** * 傳送簡訊 * @param string $other * @param string $msg */public function sendMessage($other,$msg){//因為飛信不能發送帶有空格,所以特意用|符號來代替空格$msg=urlencode($msg);$data['u']=$this->_username;$data['p']=$this->_password;$data['to']=$other;$data['m']=$msg;return $this->_curl_get($this->_url,$data);}/** * curl類比http get請求 * @param string $url請求網址 * @param string|array $data請求參數 * @return string網址內容 */private function _curl_get($url,$data=array()){$url=rtrim($url,'/');if(!empty($data)){if(is_array($data)){$first=true;foreach($data as $k=>$v){if($first){$url.='?';$first=false;}else{$url.='&';}$url.="{$k}={$v}";}}else{$data=ltrim('?',$data);$url.='?'.$data;}}$ch = curl_init($url);curl_setopt($ch,CURLOPT_HEADER,false);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 擷取資料返回if(substr($url,0,5)=='https'){curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);}$content = curl_exec($ch);curl_close($ch);return $content;}public function __set($name,$value){$this->$name=$value;}}//使用執行個體$f=new Fetion();$f->_username='111';//設定發送飛信者 賬戶$f->_password='222'; //設定發送飛信者 密碼$result=$f->sendMessage('333','111'); //發送飛信 333是接收者飛信賬戶 111是發送內容$result=json_decode($result,true);if($result['result']===0){//發送成功echo $result['message'];}else{echo $result['message'];}