溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php短信接口代碼是什么

發(fā)布時(shí)間:2020-08-28 14:29:37 來(lái)源:億速云 閱讀:271 作者:小新 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)php短信接口代碼是什么,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

1. 短信調(diào)用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 配置參數(shù)
   * api_key api秘鑰,在luosimao短信后臺(tái)短信->觸發(fā)發(fā)送下面可查看
   * use_ssl 啟用HTTPS地址,HTTPS有一定性能損耗,可選,默認(rèn)不啟用
   */
  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'];
    }
 
  }
 
  //觸發(fā),單發(fā),適用于驗(yàn)證碼,訂單觸發(fā)提醒類(lèi)
  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 );
  }
 
  //批量發(fā)送,用于大批量發(fā)送
  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 );
  }
 
  //獲取短信賬號(hào)余額
  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 接收類(lèi)型,用于在服務(wù)器端接收上行和發(fā)送狀態(tài),接收地址需要在luosimao后臺(tái)設(shè)置
   * @param array $param 傳入的參數(shù),從推送的url中獲取,官方文檔:https://luosimao.com/docs/api/
   */
  public function recv( $type = 'status' , $param = array() ){
    if( $type == 'status' ){
      if( $param['batch_id'] && $param['mobile'] && $param['status'] ){ //狀態(tài)
        // do record
      }
    }elseif( $type == 'incoming' ){ //上行回復(fù)
      if( $param['mobile'] && $param['message'] ){
        // do record
      }
    }
  }
 
  /**
   * @param string $api_url 接口地址
   * @param array $param post參數(shù)
   * @param int $timeout 超時(shí)時(shí)間
   * @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 超時(shí)時(shí)間
   * @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.短信發(fā)送示例    

//send 單發(fā)接口
 
require 'sms.php';
$sms = new Sms( array('api_key' => '86f52f3ce0647dc24da53eafe29fadd4' , 'use_ssl' => FALSE ) );
$res = $sms->send_batch( array('13761428268') , '驗(yàn)證碼: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.批量發(fā)送示例    

require 'sms.php';
$sms = new Sms( array('api_key' => '86f52f3ce0647dc24da53eafe29fadd4' , 'use_ssl' => FALSE ) );
 
 
//send 單發(fā)接口
$res = $sms->send_batch( array('13761428268') , '驗(yàn)證碼: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;

關(guān)于php短信接口代碼是什么就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI