掌握php短信接口代码

掌握php短信接口代码,第1张

概述掌握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;

相关学习推荐:编程视频

总结

以上是编程之家为你收集整理的掌握php短信接口代码全部内容,希望文章能够帮你解决掌握php短信接口代码所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/997490.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存