php使用腾讯云服务

安装 组件

php 复制代码
//这个全量组件
composer require tencentcloud/tencentcloud-sdk-php:^3.0
//在安装一个cos 的组件
composer require qcloud/cos-sdk-v5:^2.6
//如果失败了,应该是 composer 的镜像问题,切换到默认镜像
composer config -g --unset repos.packagist
//获取切换到腾讯云的镜像
composer config -g repo.packagist composer https://mirrors.tencent.com/composer/

cos 怎么用,静态云存储(后台上传)(uniapp回头写示例)

后端生成临时 密钥
php 复制代码
$sts = new Sts();
 $config = array(
      'url' => 'https://sts.tencentcloudapi.com/', // url和domain保持一致
      'domain' => 'sts.tencentcloudapi.com', // 域名,非必须,默认为 sts.tencentcloudapi.com
      'proxy' => '',
      'secretId' => "xxx", // 固定密钥,若为明文密钥,请直接以'xxx'形式填入,不要填写到getenv()函数中
      'secretKey' => "xxx", // 固定密钥,若为明文密钥,请直接以'xxx'形式填入,不要填写到getenv()函数中
      'bucket' => 'xxx', // 换成你的 bucket
      'region' => 'ap-nanjing', // 换成 bucket 所在园区 
      'durationSeconds' => 1800, // 密钥有效期
      'allowPrefix' => array('*'), // 这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
      // 密钥的权限列表。简单上传和分片需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
      'allowActions' => array (
          // 简单上传
          'name/cos:PutObject',
          'name/cos:PostObject',
          // 分片上传
          'name/cos:InitiateMultipartUpload',
          'name/cos:ListMultipartUploads',
          'name/cos:ListParts',
          'name/cos:UploadPart',
          'name/cos:CompleteMultipartUpload'
      ),
      // 临时密钥生效条件,关于condition的详细设置规则和COS支持的condition类型可以参考 https://cloud.tencent.com/document/product/436/71306
      "condition" => array()
  );

// 获取临时密钥,计算签名
$tempKeys = $sts->getTempKeys($config);
View::assign('tempKeys',$tempKeys);
前端上传
查看官方文档 https://cloud.tencent.com/document/product/436/109014
html 复制代码
<div class="row cl wenzhang">
  <label class="form-label col-xs-3 col-sm-2">上传视频:</label>
  <div class="formControls col-xs-9 col-sm-9">
    <div class="uploader-thum-container"> <a href="javascript:void();" class="btn btn-success radius"><i class="icon Hui-iconfont">&#xe642;</i> 选择视频上传</a>
      <input type="file" id="input-file" class="input-file" onchange='onpicv(this)'  accept='video/*' style="font-size: 20px;left:0;" />
    </div>
    <div class="progress" style="display: none; margin-top:8px;">
      <div class="progress-bar"><span class="sr-only" style="width:0%"></span></div>
    </div>
  </div>
</div>
<script type="text/javascript" src="__INDEX__js/wxobs.js"></script> 
javascript 复制代码
function onpicv() {
	var file = document.getElementById("input-file").files[0];
	const cos = new COS({
		SecretId:'{$tempKeys.credentials.tmpSecretId}', // sts服务下发的临时 secretId
		SecretKey: '{$tempKeys.credentials.tmpSecretKey}', // sts服务下发的临时 secretKey
		SecurityToken: '{$tempKeys.credentials.sessionToken}', // sts服务下发的临时 SessionToken
		StartTime: {$tempKeys.startTime}, // 建议传入服务端时间,可避免客户端时间不准导致的签名错误
		ExpiredTime: {$tempKeys.expiredTime}, // 临时密钥过期时间
	});

	const date = new Date();
	const path = `${date.getFullYear()}${String(date.getMonth()+1).padStart(2,'0')}${String(date.getDate()).padStart(2,'0')}`;
	let key = 'dalian/mp4/'+path+'/'+file['name'];
	cos.putObject({
		Bucket: 'xxxx', // 填入您自己的存储桶,必须字段
		Region: 'ap-nanjing',  // 存储桶所在地域,例如 ap-beijing,必须字段
		Key: key,  // 存储在桶里的对象键(例如1.jpg,a/b/test.txt),必须字段
		Body: file, // 必须,上传文件对象,可以是 input[type="file"]标签选择本地文件后得到的 file 对象
		onProgress: function(progressData) {
			console.log(JSON.stringify(progressData),4444,progressData.percent);
			$('.progress').show()
			$('.progress .sr-only').width(progressData.percent*100+'%')
		}
	}, function(err, data) {
		if (err) {
			console.log('上传失败', err);
		} else {
			console.log('上传成功', data);
			$('.progress').hide()
			let vio= 'https://xxxx/'+key   //这填写自己的域名
			if(!vio){
				 layer.msg('请填写地址!', {icon: 5, time: 1000});
			}else{
				if(vio.slice(0,4)!='http'){
					 layer.msg('地址请http开头!', {icon: 5, time: 1000});
				}else{
					let ue1 = UE.getEditor('editor');
					let html='<video  autoplay="autoplay" style="margin: 0 auto;width:100%" src="'+vio+'" controls="controls">您的浏览器不支持 video 标签。</video><p></p>';
					ue1.setContent(html,true);
				}
			}
		}
	});
}
注意修改 腾讯云的 文件权限

修改文件夹的权限为,公共读私有写(这里的文件夹是 dalian)

cos 怎么用,短信使用

php后端
php 复制代码
// 导入对应产品模块的client
use TencentCloud\Sms\V20210111\SmsClient;
// 导入要请求接口对应的Request类
use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
// 导入可选配置类
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;


 //发送短信
public function sendSms(Request $request){
    $phone = $request->post('phone');
    $code=mt_rand(100000,999999);
    try {
        // 实例化一个证书对象,入参需要传入腾讯云账户 SecretId,SecretKey
        // 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中。
        // 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
        // SecretId、SecretKey 查询: https://console.cloud.tencent.com/cam/capi
        // $cred = new Credential("SecretId", "SecretKey");
        $cred = new Credential("xxxxxxxxx","xxxxxx");
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        $httpProfile = new HttpProfile();
        // 配置代理(无需要直接忽略)
        // $httpProfile->setProxy("https://ip:port");
        $httpProfile->setReqMethod("GET");  // get请求(默认为post请求)
        $httpProfile->setReqTimeout(10);    // 请求超时时间,单位为秒(默认60秒)
        $httpProfile->setEndpoint("sms.tencentcloudapi.com");  // 指定接入地域域名(默认就近接入)

        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        $clientProfile = new ClientProfile();
        $clientProfile->setSignMethod("TC3-HMAC-SHA256");  // 指定签名算法
        $clientProfile->setHttpProfile($httpProfile);

        // 实例化要请求产品(以sms为例)的client对象,clientProfile是可选的
        // 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8
        $client = new SmsClient($cred, "ap-guangzhou", $clientProfile);

        // 实例化一个 sms 发送短信请求对象,每个接口都会对应一个request对象。
        $req = new SendSmsRequest();

        /* 填充请求参数,这里request对象的成员变量即对应接口的入参
         * 您可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
         * 基本类型的设置:
         * 帮助链接:
         * 短信控制台: https://console.cloud.tencent.com/smsv2
         * 腾讯云短信小助手: https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81 */

        /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
        // 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
        $req->SmsSdkAppId = "xxxx";
        /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
        // 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
        $req->SignName = "xxxx";
        /* 模板 ID: 必须填写已审核通过的模板 ID */
        // 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看
        $req->TemplateId = "xxxx";
        /* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空*/
        $req->TemplateParamSet = array($code);
        /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
         * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
        $req->PhoneNumberSet = array("+86".$phone);
        // 通过client对象调用SendSms方法发起请求。注意请求方法名与请求对象是对应的
        // 返回的resp是一个SendSmsResponse类的实例,与请求对象对应
        $resp = $client->SendSms($req);
        // 输出json格式的字符串回包
        //print_r($resp->toJsonString());
        Cache::set($code.$phone, $resp->toJsonString());
        s();
        // 也可以取出单个值。
        // 您可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
        // print_r($resp->TotalCount);

        /* 当出现以下错误码时,快速解决方案参考
         * [FailedOperation.SignatureIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.signatureincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
         * [FailedOperation.TemplateIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.templateincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
         * [UnauthorizedOperation.SmsSdkAppIdVerifyFail](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunauthorizedoperation.smssdkappidverifyfail-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
         * [UnsupportedOperation.ContainDomesticAndInternationalPhoneNumber](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunsupportedoperation.containdomesticandinternationalphonenumber-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
         * 更多错误,可咨询[腾讯云助手](https://tccc.qcloud.com/web/im/index.html#/chat?webAppId=8fa15978f85cb41f7e2ea36920cb3ae1&title=Sms)
         */
    } catch (TencentCloudSDKException $e) {
        echo $e;
        e('发送短信错误!');
    }
}

文本 检测,视频检测(回头写)

php代码 (文本检测)
php 复制代码
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Tms\V20201229\TmsClient;
use TencentCloud\Tms\V20201229\Models\TextModerationRequest;
use TencentCloud\Ims\V20201229\ImsClient;
use TencentCloud\Ims\V20201229\Models\ImageModerationRequest;
//判断是否有非法数据
// 初始化客户端
$cred = new Credential("xxxxx","xxxxxxx");
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("tms.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new TmsClient($cred, "ap-guangzhou", $clientProfile);

// 构建请求
$req = new TextModerationRequest();
$params = [
    "Content" => base64_encode($content), // 必须Base64编码
    "BizType" => "xxxxx" // 业务类型,需在控制台配置
];
$req->fromJsonString(json_encode($params));

// 发送请求
try {
    $resp = $client->TextModeration($req);
    $arr=json_decode($resp->toJsonString(),true);
	// print_r($resp->toJsonString());
    if($arr["Suggestion"]== "Block"){
        e("请勿发布违法违规内容,请调整后提交!");
    }
    s();

} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
    e("留言失败,文本审核失败,请稍后重试!");
}
相关推荐
无敌最俊朗@6 小时前
C++ STL Deque 高频面试题与答案
开发语言·c++
清羽_ls6 小时前
bash 基础编程的核心语法
开发语言·bash
和编程干到底6 小时前
C++基础
开发语言·c++
Z_Xshan6 小时前
docker 容器web站点 中文文件名访问404问题
linux·开发语言·docker
kkkkk0211067 小时前
【Rust创作】Rust 错误处理:从 panic 到优雅控制
开发语言·算法·rust
John.Lewis7 小时前
C++初阶(14)list
开发语言·c++·笔记
hsjkdhs8 小时前
C++文件操作
开发语言·c++
hoiii1878 小时前
C#实现近7天天气预报
开发语言·c#
赵谨言8 小时前
基于Python楼王争霸劳动竞赛数据处理分析
大数据·开发语言·经验分享·python