toplan/phpsms

阿里云短信新用户不能再用阿里大鱼的api。

Opened this issue · 10 comments

2017年6月22日起,阿里大于入驻用户使用的皆为阿里云云通信产品,原大于接口不可使用.

那么现在的问题是什么呢?阿里大于的旧用户用不起?

因现在阿里云短信服务合并到消息服务内,新开通短信用户不能使用原短信服务的api,只能用消息服务的api.

现在phpsms中,Aliyu的sendurl为dysmsapi.aliyuncs.com,新的阿里云短信用户会提示未开通云通信。

新用户得要按https://help.aliyun.com/document_detail/51929.html?spm=5176.doc32381.6.688.3LtGGG 这里方法发送短信才行。

新用户是指 2017-06-22 后的注册用户?但是我是上上周注册的用户(7.1左右吧),测试可以发呢。

你的头像好吓人。。 😅

同样的ak,用phpsms,返回{"Message":"未开通云通信产品的阿里云客户","RequestId":"419E1022-206A-492F-8917-F89C0BF4BED8","Code":"isv.PRODUCT_UN_SUBSCRIPT"}。

用SDK则正常。

按消息服务的sdk,修改了aliyunagent,你看下在你那里能不能用?

在phpsms.php 中增加了'endPoint'和'topicName'

<?php

namespace Toplan\PhpSms;

/**
 * Class AliyunAgent
 *
 * @property string $accessKeyId
 * @property string $accessKeySecret
 * @property string $signName
 * @property string $endPoint
 * @property string $topicName
 */
class AliyunAgent extends Agent implements TemplateSms
{

    private $tempId, $smsParams;

    public function sendTemplateSms($to, $tempId, array $data)
    {
        $this->smsParams = [$to=>$data];
        $this->tempId = $tempId;
        $this->request();

    }

    protected function request()
    {
        $sendurl = $this->endPoint . '/topics/' . $this->topicName . '/messages';
        $result = $this->curlPost($sendurl, [], [
            CURLOPT_HTTPHEADER => $this->createHeaders(),
            CURLOPT_POSTFIELDS => $this->generateBody(),
        ]);
        $this->setResult($result);
    }

    protected function writeMessagePropertiesForPublishXML(\XMLWriter $xmlWriter)
    {

        $xmlWriter->writeElement('MessageBody', 'smsmessage');
        $xmlWriter->startElement('MessageAttributes');
        $jsonArray = array("Type" => "multiContent");
        $jsonArray['FreeSignName'] = $this->signName;
        $jsonArray['TemplateCode'] = $this->tempId;

        if ($this->smsParams != null) {
            if (!is_array($this->smsParams)) {
                throw new MnsException(400, "SmsParams should be an array!");
            }
            if (!empty($this->smsParams)) {
                $jsonArray['SmsParams'] = json_encode($this->smsParams,JSON_FORCE_OBJECT);
            }
        }

        if (!empty($jsonArray)) {
            $xmlWriter->writeElement('DirectSMS', json_encode($jsonArray));
        }
        $xmlWriter->endElement();
    }


    protected function generateBody()
    {
        $xmlWriter = new \XMLWriter;
        $xmlWriter->openMemory();
        $xmlWriter->startDocument("1.0", "UTF-8");
        $xmlWriter->startElementNS(NULL, "Message", "http://mns.aliyuncs.com/doc/v1/");
        $this->writeMessagePropertiesForPublishXML($xmlWriter);
        $xmlWriter->endElement();
        $xmlWriter->endDocument();

      return $xmlWriter->outputMemory();
    }

    protected function createHeaders()
    {
        $params = [
            'Content-Type' => 'text/xml',
            'x-mns-version' => '2015-06-06',
            'Date' => gmdate("D, d M Y H:i:s \\G\\M\\T"),
        ];
        $params['Authorization'] = 'MNS' . " " . $this->accessKeyId . ":" . $this->computeSignature($params);

        $p = [];
        foreach ($params as $k => $v) {
            $p[] = "$k:$v";
        }
        return $p;
    }


    private function computeSignature($parameters)
    {
        $contentMd5 = '';
        $contentType = $parameters['Content-Type'];
        $date = $parameters['Date'];
        $canonicalizedMNSHeaders = 'x-mns-version:' . $parameters['x-mns-version'];
        $canonicalizedResource = '/topics/' . $this->topicName . '/messages';
        $stringToSign = strtoupper('POST') . "\n" . $contentMd5 . "\n" . $contentType . "\n" . $date . "\n" . $canonicalizedMNSHeaders . "\n" . $canonicalizedResource;

        return base64_encode(hash_hmac('sha1', $stringToSign, $this->accessKeySecret, true));
    }
  
    protected function setResult($result)
    {
        if ($result['request']) {
            $this->result(Agent::INFO, $result['response']);
            $result = json_decode($result['response'], true);
            $this->result(Agent::CODE, $result['Code']);
            if ($result['Code'] === 'OK') {
                $this->result(Agent::SUCCESS, true);
            }
        } else {
            $this->result(Agent::INFO, 'request failed');
        }
    }

   
}

好的,稍晚点,现在比较忙

问了客服,我的短信服务是在6.22之前通过消息服务开通的短信服务,需要参考消息服务接口文档来发送短信。

dntc commented

我也遇到同样的问题了,提示未开通云通信。