php教程 PHP源码 PHP SMS 인터페이스 코드

PHP SMS 인터페이스 코드

Nov 08, 2016 pm 01:25 PM

오늘은 이전 SMS 도킹 코드를 살펴보고 이를 재구성하는 시간을 가졌습니다. 공식 SMS 인터페이스 문서:

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;
    }
}
로그인 후 복사


본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)