通过composer安装SDK
composer官网包查询的地址 : 传送门
特别说明 : 通过composer引入之后,在文件内直接使用
// 测试腾讯语音识别
Route::rule("demovoice","tencentDemo/index","GET");
第一步 : 安装sdk 在项目的根目录下执行如下:
composer require tencentcloud/tencentcloud-sdk-php
2.查看TP6项目 vendor目录下增加了sdk包,如下图:
sdk包
如何在控制器内使用SDK的方法(下面例子是语音识别的)
附上腾讯云sdk文档 : 传送门
/**
* Created by PhpStorm.
* User: faith
* motto 学习可以改变自己
* Date: 2020-02-01
* Time: 20:00
*/
namespace app\api\controller;
use app\BaseController;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Asr\V20190614\AsrClient;
use TencentCloud\Asr\V20190614\Models\CreateRecTaskRequest;
use TencentCloud\Common\Profile\HttpProfile;
class TencentDemo extends BaseController
{
public function index()
{
$voice_url = "随便一个mp3的地址用来测试";
// 实例化一个证书对象,入参需要传入腾讯云账户secretId,secretKey
$TENCENTCLOUD_SECRET_ID = 'xxxxxxxxxxx在腾讯云控制台找';
$TENCENTCLOUD_SECRET_KEY = 'xxxxxxxxx在腾讯云控制台找';
$Endpoint = 'asr.tencentcloudapi.com';
$cred = new Credential($TENCENTCLOUD_SECRET_ID, $TENCENTCLOUD_SECRET_KEY);
// 实例化一个http选项,可选的,没有特殊需求可以跳过
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint($Endpoint); // 指定接入地域域名(默认就近接入)
// 实例化一个client选项,可选的,没有特殊需求可以跳过
$clientProfile = new ClientProfile();
$clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256)
$clientProfile->setHttpProfile($httpProfile);
$client = new AsrClient($cred, "ap-shanghai", $clientProfile);
$req = new CreateRecTaskRequest();
$params = '{"EngineModelType":"16k_0","ChannelNum":1,"ResTextFormat":0,"SourceType":0,"Url":"' . $voice_url . '"}';
$req->fromJsonString($params);
$resp = $client->CreateRecTask($req);
return json($resp);
}
}