在Laravel中发送短信无法与Sinch配合使用

问题描述

在Laravel中发送SMS不能与Sinch一起使用。

我已经做了所有事情,但我不知道要做什么。

我正在尝试使用Sinch发送短信,但是我什么也没收到。

我是密钥和秘密权以及电话号码。

但我不知道。是否缺少某些东西?

您可以在下面看到我的代码

<?PHP

class SinchController
{

    var $key = "xxxxxxxxxx";
    var $secret = "xxxxxxxxx";
    var $contentType = "application/json";
    var $baseurl = " https://verificationapi-v1.sinch.com";
    var $ch;


    public function index()
    {
        $countryCode = "+20";
        $phoneNumber = "xxxxxxx";
        $receivedCode = "4444";
        $result = $this->verifyMobile($countryCode . $phoneNumber,$receivedCode);

    }

    public function sendCode($mobile)
    {
        $url_path = $this->encodeurl('/verification/v1/verifications');
        $this->ch = curl_init($this->baseurl . $url_path);
        $this->setupDefault();
        $this->setupSendData($mobile,$url_path);
        $return = $this->getResult();
        return $return;
    }

    public function verifyMobile($mobile,$code)
    {
        $url_path = $this->encodeurl('/verification/v1/verifications/number/' . $mobile);
        $this->ch = curl_init($this->baseurl . $url_path);
        $this->setupDefault();
        $this->setupVerifyData($code,$url_path);
        $return = $this->getResult();
        return $return;
    }

    private function setupDefault()
    {
        curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($this->ch,CURLOPT_SSL_VERIFYPEER,false);
    }

    private function setupVerifyData($code,$url_path)
    {
        $data = json_encode([
            'method' => 'sms','sms' => [
                'code' => (string)$code
            ]
        ]);
        curl_setopt($this->ch,CURLOPT_CUSTomrEQUEST,"PUT");
        curl_setopt($this->ch,CURLOPT_POSTFIELDS,$data);
        $this->signedHeaders("PUT",$data,$url_path);
    }

    private function setupSendData($mobile,$url_path)
    {
        $data = json_encode([
            'identity' => [
                'type' => 'number','endpoint' => $mobile,],'method' => 'sms'
        ]);
        curl_setopt($this->ch,$data);
        curl_setopt($this->ch,CURLOPT_POST,true);
        $this->signedHeaders("POST",$url_path);
    }

    private function getResult()
    {
        $result = curl_exec($this->ch);
        $return = [];
        if (curl_errno($this->ch)) {
            $return['error'] = curl_error($this->ch);
        } else {
            $return['data'] = $result;
        }
        return $result;
    }

    private function compileContentType()
    {
        return 'content-type: ' . $this->contentType;
    }

    private function signedHeaders($method,$body,$url_path)
    {
        $method = strtoupper($method);
        $date = date("c");
        $contentMd5 = base64_encode(md5(utf8_encode($body),true));
        $xTimestamp = "x-timestamp:" . $date;
        $StringToSign = $method . "\n" .
            $contentMd5 . "\n" .
            $this->contentType . "\n" .
            $xTimestamp . "\n" .
            $url_path;
        $signature = base64_encode(hash_hmac("sha256",utf8_encode($StringToSign),base64_decode($this->secret),true));
        $Authorization = 'Authorization: Application ' . $this->key . ":" . $signature;
        $headers = [
            $this->compileContentType(),$Authorization,$xTimestamp
        ];
        curl_setopt($this->ch,CURLOPT_HTTPHEADER,$headers);
    }

    private function encodeurl($url)
    {
         $url_ = urlencode(utf8_encode($url));
         $url_ = str_replace("\\+","%20",$url_);
         $url_ = str_replace("\\%7E","~",$url_);
        return $url;
    }
}

邮递员结果

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)