目录
1、申请3个月免费短信
2、获取阿里云AccessKey ID 和 AccessKey Secret
3、php安装阿里云sdk
4、复制下面代码调用sdk替换自己key、运行该代码
1、申请3个月免费短信
2、获取阿里云AccessKey ID 和 AccessKey Secret
3、php安装阿里云sdk
开发文档短信服务_SDK中心-阿里云OpenAPI开发者门户
使用composer之前需使用阿里云镜像
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
安装sdk
composer require alibabacloud/dysmsapi-20170525 2.0.24
安装成功后出现该目录
4、复制下面代码调用sdk替换自己key、运行该代码
// This file is auto-generated, don't edit it. Thanks.
namespace AlibabaCloud\SDK\Sample;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
require_once 'vendor/autoload.php';
class Sample {
/**
* 使用AK&SK初始化账号Client
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Dysmsapi Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
$config = new Config([
// 必填,您的 AccessKey ID
"accessKeyId" => $accessKeyId,
// 必填,您的 AccessKey Secret
"accessKeySecret" => $accessKeySecret
]);
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
$config->endpoint = "dysmsapi.aliyuncs.com";
return new Dysmsapi($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main(){
// 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html
$client = self::createClient('accessKeyId','accessKeySecret');
$sendSmsRequest = new SendSmsRequest([
"signName" => "阿里云短信测试",
"templateCode" => "SMS_154950909",
"phoneNumbers" => "13888888888",
"templateParam" => "{\"code\":\"1234\"}"
]);
$runtime = new RuntimeOptions([]);
try {
// 复制代码运行请自行打印 API 的返回值
$client->sendSmsWithOptions($sendSmsRequest, $runtime);
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
// 错误 message
var_dump($error->message);
// 诊断地址
var_dump($error->data["Recommend"]);
Utils::assertAsString($error->message);
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
Sample::main();
运行该代码发送成功
如出现
cURL error 60: SSL certificate problem: unable to get local issuer certificate
测试环境可不校验,HTTPS改成HTTP