站長資訊網
        最全最豐富的資訊網站

        掌握php短信接口代碼

        掌握php短信接口代碼

        本文實例為大家分享了幾個常用的php短信接口代碼,供大家參考,具體內容如下

        相關學習推薦:php編程(視頻)

        1. 短信調用class

        <?php /**  * User: Administrator  * Date: 2016/5/8 0008  * Time: 下午 2:36  */ class Sms{       //Luosimao api key   private $_api_key = '';     private $_last_error = array();       private $_use_ssl = FALSE;     private $_ssl_api_url = array(     'send'    => 'https://www.jb51.net/v1/send.json',     'send_batch' => 'https://www.jb51.net/v1/send_batch.json',     'status'   => 'https://www.jb51.net/v1/status.json',   );     private $_api_url = array(     'send'    => 'https://www.jb51.net/v1/send.json',     'send_batch' => 'https://www.jb51.net/send_batch.json',     'status'   => 'https://www.jb51.net/v1/status.json',   );     /**    * @param array $param 配置參數    * api_key api秘鑰,在luosimao短信后臺短信->觸發發送下面可查看    * use_ssl 啟用HTTPS地址,HTTPS有一定性能損耗,可選,默認不啟用    */   public function __construct( $param = array() ){       if( !isset( $param['api_key'] ) ){       die("api key error.");     }       if( isset( $param['api_key'] ) ){       $this->_api_key = $param['api_key'];     }       if( isset( $param['use_ssl'] ) ){       $this->_use_ssl = $param['use_ssl'];     }     }     //觸發,單發,適用于驗證碼,訂單觸發提醒類   public function send( $mobile , $message = '' ){     $api_url = !$this->_use_ssl ? $this->_api_url['send'] : $this->_ssl_api_url['send'];     $param = array(       'mobile' => $mobile ,       'message' => $message,     );     $res = $this->http_post( $api_url ,$param );     return @json_decode( $res ,TRUE );   }     //批量發送,用于大批量發送   public function send_batch( $mobile_list = array() , $message = array() , $time = '' ){     $api_url = !$this->_use_ssl ? $this->_api_url['send_batch'] : $this->_ssl_api_url['send_batch'];     $mobile_list = is_array( $mobile_list ) ? implode( ',' , $mobile_list ) : $mobile_list;     $param = array(       'mobile_list' => $mobile_list ,       'message' => $message,       'time'  => $time,     );     $res = $this->http_post( $api_url ,$param );     return @json_decode( $res ,TRUE );   }     //獲取短信賬號余額   public function get_deposit(){     $api_url = !$this->_use_ssl ? $this->_api_url['status'] : $this->_ssl_api_url['status'];     $res = $this->http_get( $api_url );     return @json_decode( $res ,TRUE );   }     /**    * @param string $type 接收類型,用于在服務器端接收上行和發送狀態,接收地址需要在luosimao后臺設置    * @param array $param 傳入的參數,從推送的url中獲取,官方文檔:https://luosimao.com/docs/api/    */   public function recv( $type = 'status' , $param = array() ){     if( $type == 'status' ){       if( $param['batch_id'] && $param['mobile'] && $param['status'] ){ //狀態         // do record       }     }elseif( $type == 'incoming' ){ //上行回復       if( $param['mobile'] && $param['message'] ){         // do record       }     }   }     /**    * @param string $api_url 接口地址    * @param array $param post參數    * @param int $timeout 超時時間    * @return bool    */   private function http_post( $api_url = '' , $param = array() , $timeout = 5 ){       if( !$api_url ){       die("error api_url");     }       $ch = curl_init();     curl_setopt( $ch, CURLOPT_URL, $api_url );       curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0 );     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );     curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE);     curl_setopt( $ch, CURLOPT_HEADER, FALSE);       if( parse_url( $api_url )['scheme'] == 'https' ){       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , FALSE);       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , FALSE);     }       curl_setopt( $ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);     curl_setopt( $ch, CURLOPT_USERPWD , 'api:key-'.$this->_api_key );     curl_setopt( $ch, CURLOPT_POST, TRUE);     curl_setopt( $ch, CURLOPT_POSTFIELDS, $param );       $res  = curl_exec( $ch );     $error = curl_error( $ch );     curl_close( $ch );     if( $error ){       $this->_last_error[] = $error;       return FALSE;     }     return $res;   }     /**    * @param string $api_url 接口地址    * @param string $timeout 超時時間    * @return bool    */   private function http_get( $api_url = '' , $timeout = '' ){       if( !$api_url ){       die("error api_url");     }       $ch = curl_init();     curl_setopt( $ch, CURLOPT_URL, $api_url );       curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0 );     curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );     curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE);     curl_setopt( $ch, CURLOPT_HEADER, FALSE);       if( parse_url( $api_url )['scheme'] == 'https' ){       curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST , FALSE);       curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER , FALSE);     }     curl_setopt( $ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);     curl_setopt( $ch, CURLOPT_USERPWD , 'api:key-'.$this->_api_key );       $res  = curl_exec( $ch );     $error = curl_error( $ch );     curl_close( $ch );     if( $error ){       $this->_last_error[] = curl_error( $ch );       return FALSE;     }     return $res;   }     public function last_error(){     return $this->_last_error;   } }

        2.短信發送示例

        //send 單發接口   require 'sms.php'; $sms = new Sms( array('api_key' => '86f52f3ce0647dc24da53eafe29fadd4' , 'use_ssl' => FALSE ) ); $res = $sms->send_batch( array('13761428268') , '驗證碼:19272【腳本之家】'); if( $res ){   if( isset( $res['error'] ) && $res['error'] == 0 ){     echo 'success';   }else{     echo 'failed,code:'.$res['error'].',msg:'.$res['msg'];   } }else{   var_dump( $sms->last_error() ); } exit;

        3.批量發送示例

        require 'sms.php'; $sms = new Sms( array('api_key' => '86f52f3ce0647dc24da53eafe29fadd4' , 'use_ssl' => FALSE ) );     //send 單發接口 $res = $sms->send_batch( array('13761428268') , '驗證碼:19272【腳本之家】'); if( $res ){   if( isset( $res['error'] ) && $res['error'] == 0 ){     echo 'success';   }else{     echo 'failed,code:'.$res['error'].',msg:'.$res['msg'];   } }else{   var_dump( $sms->last_error() ); } exit;

        4.獲取余額示例

        //deposit 余額查詢 require 'sms.php'; $sms = new Sms( array('api_key' => '86f52f3ce0647dc24da53eafe29fadd4' , 'use_ssl' => FALSE ) );   $res = $sms->get_deposit(); if( $res ){   if( isset( $res['error'] ) && $res['error'] == 0 ){     echo 'desposit:'.$res['deposit'];   }else{     echo 'failed,code:'.$res['error'].',msg:'.$res['msg'];   } }else{   var_dump( $sms->last_error() ); } exit;

        相關學習推薦:編程視頻

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 国产乱码伦精品一区二区三区麻豆| 亚洲精品视频在线看| 久草欧美精品在线观看| 91精品国产91久久久久福利| 国产午夜精品理论片久久| 国产韩国精品一区二区三区久久| 亚洲国产精品乱码一区二区| 久久se精品一区二区影院| 99精品影院| 精品久久久久久国产| 精品无码日韩一区二区三区不卡| 欧美人与性动交α欧美精品| 国产精品福利电影一区二区三区四区欧美白嫩精品 | 欧美国产精品va在线观看| 99热这里只有精品6国产免费| 91精品国产福利在线导航| 国产三级精品三级在线专区1| 欧洲精品99毛片免费高清观看| 亚洲精品国产美女久久久| 亚洲精品动漫免费二区| 人妻无码精品久久亚瑟影视| 精品综合久久久久久88小说| 国产亚洲精品无码专区| 国产精品色视频ⅹxxx| 国产精品va久久久久久久| 四虎国产精品永久地址51| 香蕉久久夜色精品国产小说| 一本久久a久久精品综合夜夜| 四虎成人精品免费影院| 亚洲精品欧美综合在线| 欧美日激情日韩精品| 99久久精品九九亚洲精品| 国产精品福利在线观看免费不卡| 北岛玲日韩精品一区二区三区| 国产精品v欧美精品v日韩| 国产成人精品综合久久久| 国产精品99久久精品爆乳| 精品人妻少妇一区二区三区在线| 久久精品国产一区二区| 午夜三级国产精品理论三级| 在线涩涩免费观看国产精品|