Need to pay attention to lines 2 and 27 of the following code
<?php
require 'vendor/autoload.php'; //采用composer,这里需要添加autoload,不然下面的这些namespace不能识别,这是官方示例没有说明的,新手容易犯错
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
// Download:https://github.com/aliyun/openapi-sdk-php
// Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md
AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')
->regionId('cn-hangzhou')
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()
->product('Dysmsapi')
// ->scheme('https') // https | http
->version('2017-05-25')
->action('SendSms')
->method('POST')
->host('dysmsapi.aliyuncs.com')
->options([
'query' => [
'RegionId' => "cn-hangzhou",
'PhoneNumbers' => "18022128168",
'SignName' => "知更鸟网",
'TemplateCode' => "SMS_187932702",
'TemplateParam' => "{\"code\":\"验证码\"}", \\这里填写系统生成的验证码,发送给用户手机。
],
])
->request();
print_r($result->toArray());
} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}