php簡訊介面代碼_php技巧

來源:互聯網
上載者:User

本文執行個體為大家分享了幾個常用的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'    => 'http://www.jb51.net/v1/send.json',    'send_batch' => 'http://www.jb51.net/v1/send_batch.json',    'status'   => 'http://www.jb51.net/v1/status.json',  );   private $_api_url = array(    'send'    => 'http://www.jb51.net/v1/send.json',    'send_batch' => 'http://www.jb51.net/send_batch.json',    'status'   => 'http://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;

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.