1. 首页
  2. 文本短信
  3. SDK参考
  4. PHP SDK
菜单
本页目录
<?php

class Test
{
    protected $url = 'http://127.0.0.1:9511';
    protected $spId = '443443';
    protected $password = '91f147eb';

    //发送
    public function postSms()
    {
        //短信发送
        $action = '/api/send-sms-single';
        $mobile = '17630000000';
        $content = '【测试】xxxxxxx';

        $postData = array(
            'sp_id' => $this->spId,
            'mobile' => $mobile,
            'content' => $content,
        );

        $usePassword = true;
        //如果是使用得密码发送
        if ($usePassword) {
            $postData['password'] = md5($this->password);
        } else {
            $postData['signature'] = self::generateSignature($postData, 'POST', $this->password);
        }

        $result = self::doCurl($postData, $this->url, $action);
    }

    public function getReport()
    {
        //短信发送
        $action = '/api/report';//report 或者 reply

        $getData = array(
            'sp_id' => $this->spId
        );

        $usePassword = true;
        //如果是使用得密码发送
        if ($usePassword) {
            $getData['password'] = $this->password;
        } else {
            $getData['signature'] = self::generateSignature($getData, 'GET', $this->password);
        }

        $result = self::doCurl($getData, $this->url, $action);
    }

    public function reportTemplate()
    {
        $action = '/mms/template';//report 或者 reply 或者balance
        $file_name = "/www/sms-admin/public/1622698826047157.mp4";//需要下载的文件
        $fp = fopen($file_name, "r+");//下载文件必须先要将文件打开,写入内存

        $buffer = 10240000;
        $file_data = '';

        while (!feof($fp)) {
            $file_data = fread($fp, $buffer);
        }

        //关闭文件
        fclose($fp);

        $content = '3,mp4|' . chunk_split(base64_encode($file_data));

        $postData = array(
            "account" => $this->spId,
            "password" => $this->password,
            "title" => "dddd",
            "content" => $content,
            "is_personality" => 0
        );

        $result = self::doCurl($postData, $this->url, $action);
    }

    //彩信发送
    public function postMms()
    {
        $action = '/mms/send-temp';

        $postData = array(
            'account' => $this->spId,
            'password' => $this->password,
            'temp_id' => 13,
            'mobile' => '17600000000,17600000001',
            'personality' => [//不是变量短信不需要传
                '17600000000' => ['变量1','变量2'],
                '17600000001' => ['变量1','变量2']
            ]
        );

        $result = self::doCurl($postData, $this->url, $action);
    }

    protected function generateSignature($data, $method, $password)
    {
        ksort($data);

        $uri = $method . '&' . urlencode('/') . '&' . preg_replace(['/\+/', '/\*/', '/%7E/'], ['%20', '%2A', '~'], http_build_query($data));

        return base64_encode(hash_hmac("sha1", $uri, $password));
    }

    protected function doCurl($data, $url, $action)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url . $action);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($ch, CURLOPT_POST, 1);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $output = curl_exec($ch);

        curl_close($ch);

        return $output;
    }
}

(new Test())->postSms();