用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2047|回复: 0
收起左侧

[库 Library] 短信发送library

[复制链接]
发表于 2016-5-19 22:41:53 | 显示全部楼层 |阅读模式
本帖最后由 yzcj007 于 2016-5-19 22:44 编辑
PHP复制代码
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Luosimao sms send libraiy
 *
*/

 
// ------------------------------------------------------------------------
 
/**
 * Sms send class
 *
 * @package                Sms
 * @author                jichen
 * @link                https://luosimao.com/docs/api
 */

class Sms{
 
 
    //Luosimao api key
    private $_api_key = '';
 
    private $_last_error = array();
 
 
    private $_use_ssl = FALSE;
 
    private $_ssl_api_url = array(
        'send'       => 'https://sms-api.luosimao.com/v1/send.json',
        'send_batch' => 'https://sms-api.luosimao.com/v1/send_batch.json',
        'status'     => 'https://sms-api.luosimao.com/v1/status.json',
    );
 
    private $_api_url = array(
        'send'       => 'http://sms-api.luosimao.com/v1/send.json',
        'send_batch' => 'http://sms-api.luosimao.com/v1/send_batch.json',
        'status'     => 'http://sms-api.luosimao.com/v1/status.json',
    );
 
 
    public function __construct(){
        $this->CI =& get_instance();
    }
 
    public function init( $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'];
        }
 
        return $this;
    }
 
    //触发,单发,适用于验证码,订单触发提醒类
    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;
    }
}
 
 
复制代码



library使用luosimao.com 的短信接口服务,目前该library特性如下:
1、支持HTTP,HTTPS发送
2、支持验证码发送,批量发送(短信内容可在后台加模板免审)
3、支持上行状态推送,回复推送(需在luosimao后台设置)
4、支持余额实时查询
详细的短信接口文档:
https://luosimao.com/docs/api

发送验证码调用示例:
PHP复制代码
 
$this->load->library('sms');
$this->sms->init( array('api_key' => '3c34e5386b20752fcb8e7d17e299a422') );
$res = $this->sms->send('13766666666' , '验证码:28261【京东商城】');
var_dump($res);
exit;
 
 
复制代码


查询余额调用示例:
PHP复制代码
 
$this->load->library('sms');
$this->sms->init( array('api_key' => '3c34e5386b20752fcb8e7d17e299a422') );
$res = $this->sms->get_deposit();
var_dump($res);
exit;
 
 
复制代码



Sms.rar

1.41 KB, 下载次数: 33

本版积分规则