给thinkphp3.2集成阿里云短信,开始不使用Composer直接下载sdk一直调用不成功,不得以只能全新重新来一波Composer部署,成功运行。
效果演示
[root@localhost ~]# ln -s /usr/local/php/bin/php /usr/bin/php
[root@localhost ~]# php -v
PHP 7.2.1 (cli) (built: Nov 14 2022 18:49:27) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
[root@localhost ~]# curl -sS https://getcomposer.org/installer | php
Downloading...
Composer (version 2.2.24) successfully installed to: /root/composer.phar
Use it: php composer.phar
Some settings on your machine may cause stability issues with Composer.
If you encounter issues, try to change the following:
PHP was compiled with --with-curlwrappers which will cause issues with HTTP authentication and GitHub.
Recompile it without this flag if possible
You have mail in /var/spool/mail/root
[root@localhost ~]# mv composer.phar /usr/local/bin/composer
[root@localhost ~]# composer -V
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Composer version 2.2.24 2024-06-10 22:51:52
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
最后一行为修改composer源为阿里源,
当你决定使用阿里云短信时,你需要在阿里云 1、开通短信服务、购买一个短信资源包 2、进行资质认证 3、进行签名申请 3、进行模板申请(默认送了一个验证码模板)
使用Composer安装阿里云短信sdk
在thinkphp3.2的项目根目录下执行
composer require alibabacloud/dysmsapi-20170525
thinkphp3.2 短信控制器代码
accessKeyId和accessKeySecret ,是阿里云子帐号,应创建一个子帐号,并把权限设置在短信上。
<?php
namespace Home\Controller;
use Think\Controller;
require_once __DIR__ . '/../../../vendor/autoload.php';
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\Tea\Exception\TeaError;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class SmsController extends Controller {
private function createClient(){
$config = new Config([
"accessKeyId" => '你的key',
"accessKeySecret" => '你的Secret'
]);
$config->endpoint = "dysmsapi.aliyuncs.com";
return new Dysmsapi($config);
}
public function sendSms(){
$client = $this->createClient();
$sendSmsRequest = new SendSmsRequest([
"phoneNumbers" => "收短信手机",
"signName" => "短信签名",
"templateCode" => "模板ID",
"templateParam" => json_encode(["code" => "1234"]) // 根据你的模板参数进行调整
]);
try {
$response = $client->sendSmsWithOptions($sendSmsRequest, new RuntimeOptions([]));
// 处理发送成功的情况
$this->ajaxReturn(['status' => 1, 'msg' => '发送成功', 'data' => $response->toMap()]);
} catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
// 处理发送失败的情况
$this->ajaxReturn(['status' => 0, 'msg' => '未知异常']);
}
}
}
本文《2024年linux tp3.2安装使用Composer》由爱思考吧 isres.com 分享,转载请注明出处。本文网址:https://www.isres.com/bushu/lnmp-Composer.html
本站主要收集测评能够节省时间和提升效率的软件工具并分享使用与学习的过程和感受,任何情况下用户都需遵守所使用软件资源的相关协议。与《2024年linux tp3.2安装使用Composer》有关的本站文章及资源仅供个人学习交流使用,相关资源请下载后24小时内删除,请勿用于其它用途,因此产生的任何问题由您自行承担。