您的位置:首页 > 编程语言 > PHP开发

创蓝253云通讯paas平台PHP短信接口demo分享

2018-01-30 17:30 786 查看
demo:

<?php
header("Content-type:text/html; charset=UTF-8");

class ChuanglanSmsApi {

        //发送短信的接口地址

        const API_SEND_URL='http://sms.253.com/msg/send?';

        //查询余额的接口地址

        constAPI_BALANCE_QUERY_URL='http://sms.253.com/msg/balance?';

        const API_ACCOUNT='*******';//短信账号从 https://zz.253.com/site/login.html 里面获取。

        const API_PASSWORD='*******';//短信密码从from https://zz.253.com/site/login.html 里面获取。

        /**

         * 发送短信需要的接口参数

         *

         * @param string $mobile                手机号码

         * @param string $msg                        想要发送的短信内容

         * @param string $needstatus        是否需要状态报告 '1'为需要'0'位不需要。

         */

        public function sendSMS( $mobile, $msg, $needstatus= 1) {

                

                //发送短信的接口参数

                $postArr = array (

                                        'un' => self::API_ACCOUNT,

                                        'pw' => self::API_PASSWORD,

                                        'msg' => $msg,

                                        'phone' => $mobile,

                                        'rd' => $needstatus

                    );

                

                $result =$this->curlPost( self::API_SEND_URL , $postArr);

                return $result;

        }

        

        /**

         * 

         *

         *  查询余额

         */

        public function queryBalance() {

                

                // 查询接口参数

                $postArr = array ( 

                         'un' => self::API_ACCOUNT,

                         'pw' => self::API_PASSWORD,

                );

                $result =$this->curlPost(self::API_BALANCE_QUERY_URL, $postArr);

                return $result;

        }

        /**

         * 处理接口返回值

         * 

   
c4fb
     */

        public function execResult($result){

               $result=preg_split("/[,\r\n]/",$result);

                return $result;

        }

        /**

         * @param string $url  

         * @param array $postFields 

         * @return mixed

         */

        private function curlPost($url,$postFields){

                $postFields =http_build_query($postFields); 

               if(function_exists('curl_init')){

                       $ch = curl_init ();

                       curl_setopt ( $ch, CURLOPT_POST, 1 );

                       curl_setopt ( $ch, CURLOPT_HEADER, 0 );

                       curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );

                       curl_setopt ( $ch, CURLOPT_URL, $url );

                       curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );

                       $result = curl_exec ( $ch );

                       if(curl_errno($ch))

                       {

                               return 'Curl error: ' . curl_error($ch);

                       }

                       curl_close ( $ch );

               }elseif(function_exists('file_get_contents')){

                       

                       $result=file_get_contents($url.$postFields);

                }

                return $result;

        }

        

        //魔术获取

        public function __get($name){

                return $this->$name;

        }

        

        //魔术设置

        public function __set($name,$value){

                $this->$name=$value;

        }

}

?>

发送短信接入流程:

1.登录 https://zz.253.com/site/login.html 

2.获取接口API账号,密码:选择任意产品>激活>企业认证

3.申请签名(以公司简称或缩写命名)备注:平台申请签名,API接口加上申请签名

4.模板申请(自定义编辑内容):选择任意应用>短息编辑栏目{模板管理}>添加签名   

5.sms_send_demo.php发送短信  

sms_queryBalance_demo.php

sms_queryBalance_demo.php查询短信余额 
备注:申请模板可达到短信免审作用

常见问题:

1.相同号码一天有10条限制(可根据业务需求进行调整)
2.接口账号(使用API账号,密码并非登录管理后台账号,密码)

状态报告接入流程:

1.配置一个外网可以访问的URL来接受创蓝服务器的异步推送

2.参考receive.php
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息