• 掌握php短信接口代码


      本文实例为大家分享了几个常用的php短信接口代码,供大家参考,具体内容如下

    1. 短信调用class

     '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',
      );
     
      
      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 );
      }
     
      
      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
          }
        }
      }
     
      
      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;
      }
     
      
      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;

    以上就是掌握php短信接口代码的详细内容,更多请关注考高分网其它相关文章!

  • 相关阅读:
    切比雪夫不等式
    Myeclipse中可以正常显示,但运行后的网页找不到图片
    Hibernate Is Not Mapped(实体名 is not mapped [from book where id='0'])
    struts2-core-2.1.6.jar!/struts-default.xml无法加载的问题
    myeclipse 8.5最新注册码(过期时间到2016年)
    三大框架SSH(struts2+spring+hibernate)整合时相关配置文件的模板
    Initializing Spring root WebApplicationContext
    Unable to load bean: type: struts-derfault.xml异常
    Error creating bean with name 'bookDao'
    hibernate异常:Could not determine type for: java.util.Set
  • 原文地址:https://www.cnblogs.com/68xi/p/15969922.html
Copyright © 2020-2023  润新知