search
Homephp教程PHP源码php SMS interface code

Today I had time to look at the previous SMS docking code and reconstruct it. The official SMS interface document:

https://luosimao.com/docs/api/

<?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://sms-api.luosimao.com/v1/send.json&#39;,
        &#39;send_batch&#39; => &#39;https://sms-api.luosimao.com/v1/send_batch.json&#39;,
        &#39;status&#39;     => &#39;https://sms-api.luosimao.com/v1/status.json&#39;,
    );
 
    private $_api_url = array(
        &#39;send&#39;       => &#39;http://sms-api.luosimao.com/v1/send.json&#39;,
        &#39;send_batch&#39; => &#39;http://sms-api.luosimao.com/v1/send_batch.json&#39;,
        &#39;status&#39;     => &#39;http://sms-api.luosimao.com/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;
    }
}


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor