登录  /  注册

掌握php短信接口代码

coldplay.xixi
发布: 2020-08-15 17:15:21
转载
4089人浏览过

掌握php短信接口代码

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

相关学习推荐:php编程(视频)

1. 短信调用class     

<?php
/**
 * User: Administrator
 * Date: 2016/5/8 0008
 * Time: 下午 2:36
 */
class Sms{
 
 
  //Luosimao api key
  private $_api_key = &#39;&#39;;
 
  private $_last_error = array();
 
 
  private $_use_ssl = FALSE;
 
  private $_ssl_api_url = array(
    &#39;send&#39;    => &#39;https://www.jb51.net/v1/send.json&#39;,
    &#39;send_batch&#39; => &#39;https://www.jb51.net/v1/send_batch.json&#39;,
    &#39;status&#39;   => &#39;https://www.jb51.net/v1/status.json&#39;,
  );
 
  private $_api_url = array(
    &#39;send&#39;    => &#39;https://www.jb51.net/v1/send.json&#39;,
    &#39;send_batch&#39; => &#39;https://www.jb51.net/send_batch.json&#39;,
    &#39;status&#39;   => &#39;https://www.jb51.net/v1/status.json&#39;,
  );
 
  /**
   * @param array $param 配置参数
   * api_key api秘钥,在luosimao短信后台短信->触发发送下面可查看
   * use_ssl 启用HTTPS地址,HTTPS有一定性能损耗,可选,默认不启用
   */
  public function __construct( $param = array() ){
 
    if( !isset( $param[&#39;api_key&#39;] ) ){
      die("api key error.");
    }
 
    if( isset( $param[&#39;api_key&#39;] ) ){
      $this->_api_key = $param[&#39;api_key&#39;];
    }
 
    if( isset( $param[&#39;use_ssl&#39;] ) ){
      $this->_use_ssl = $param[&#39;use_ssl&#39;];
    }
 
  }
 
  //触发,单发,适用于验证码,订单触发提醒类
  public function send( $mobile , $message = &#39;&#39; ){
    $api_url = !$this->_use_ssl ? $this->_api_url[&#39;send&#39;] : $this->_ssl_api_url[&#39;send&#39;];
    $param = array(
      &#39;mobile&#39; => $mobile ,
      &#39;message&#39; => $message,
    );
    $res = $this->http_post( $api_url ,$param );
    return @json_decode( $res ,TRUE );
  }
 
  //批量发送,用于大批量发送
  public function send_batch( $mobile_list = array() , $message = array() , $time = &#39;&#39; ){
    $api_url = !$this->_use_ssl ? $this->_api_url[&#39;send_batch&#39;] : $this->_ssl_api_url[&#39;send_batch&#39;];
    $mobile_list = is_array( $mobile_list ) ? implode( &#39;,&#39; , $mobile_list ) : $mobile_list;
    $param = array(
      &#39;mobile_list&#39; => $mobile_list ,
      &#39;message&#39; => $message,
      &#39;time&#39;  => $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[&#39;status&#39;] : $this->_ssl_api_url[&#39;status&#39;];
    $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 = &#39;status&#39; , $param = array() ){
    if( $type == &#39;status&#39; ){
      if( $param[&#39;batch_id&#39;] && $param[&#39;mobile&#39;] && $param[&#39;status&#39;] ){ //状态
        // do record
      }
    }elseif( $type == &#39;incoming&#39; ){ //上行回复
      if( $param[&#39;mobile&#39;] && $param[&#39;message&#39;] ){
        // do record
      }
    }
  }
 
  /**
   * @param string $api_url 接口地址
   * @param array $param post参数
   * @param int $timeout 超时时间
   * @return bool
   */
  private function http_post( $api_url = &#39;&#39; , $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 )[&#39;scheme&#39;] == &#39;https&#39; ){
      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 , &#39;api:key-&#39;.$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 = &#39;&#39; , $timeout = &#39;&#39; ){
 
    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 )[&#39;scheme&#39;] == &#39;https&#39; ){
      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 , &#39;api:key-&#39;.$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 &#39;sms.php&#39;;
$sms = new Sms( array(&#39;api_key&#39; => &#39;86f52f3ce0647dc24da53eafe29fadd4&#39; , &#39;use_ssl&#39; => FALSE ) );
$res = $sms->send_batch( array(&#39;13761428268&#39;) , &#39;验证码:19272【脚本之家】&#39;);
if( $res ){
  if( isset( $res[&#39;error&#39;] ) && $res[&#39;error&#39;] == 0 ){
    echo &#39;success&#39;;
  }else{
    echo &#39;failed,code:&#39;.$res[&#39;error&#39;].&#39;,msg:&#39;.$res[&#39;msg&#39;];
  }
}else{
  var_dump( $sms->last_error() );
}
exit;
登录后复制

3.批量发送示例

require &#39;sms.php&#39;;
$sms = new Sms( array(&#39;api_key&#39; => &#39;86f52f3ce0647dc24da53eafe29fadd4&#39; , &#39;use_ssl&#39; => FALSE ) );
 
 
//send 单发接口
$res = $sms->send_batch( array(&#39;13761428268&#39;) , &#39;验证码:19272【脚本之家】&#39;);
if( $res ){
  if( isset( $res[&#39;error&#39;] ) && $res[&#39;error&#39;] == 0 ){
    echo &#39;success&#39;;
  }else{
    echo &#39;failed,code:&#39;.$res[&#39;error&#39;].&#39;,msg:&#39;.$res[&#39;msg&#39;];
  }
}else{
  var_dump( $sms->last_error() );
}
exit;
登录后复制

4.获取余额示例

//deposit 余额查询
require &#39;sms.php&#39;;
$sms = new Sms( array(&#39;api_key&#39; => &#39;86f52f3ce0647dc24da53eafe29fadd4&#39; , &#39;use_ssl&#39; => FALSE ) );
 
$res = $sms->get_deposit();
if( $res ){
  if( isset( $res[&#39;error&#39;] ) && $res[&#39;error&#39;] == 0 ){
    echo &#39;desposit:&#39;.$res[&#39;deposit&#39;];
  }else{
    echo &#39;failed,code:&#39;.$res[&#39;error&#39;].&#39;,msg:&#39;.$res[&#39;msg&#39;];
  }
}else{
  var_dump( $sms->last_error() );
}
exit;
登录后复制

相关学习推荐:编程视频

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

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:jb51网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号