获取SDK
composer require alibabacloud/sdk
说明:阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
强烈建议不要把AccessKey和AccessKeySecret保存到代码里,会存在密钥泄漏风险,在此提供通过配置环境变量的方式来保存和访问aksk
Linux和macOS系统配置方法export NLP_AK_ENV=<access_key_id>
export NLP_SK_ENV=<access_key_secret>
其中<access_key_id>替换为已准备好的AccessKey ID,<access_key_secret>替换为AccessKey Secret
Windows系统配置方法新建环境变量文件,添加环境变量NLP_AK_ENV和NLP_SK_ENV,并写入已准备好的AccessKey ID和AccessKey Secret。重启Windows系统。
使用示例
<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\NlpAutoml\NlpAutoml;
// Download:https://github.com/aliyun/openapi-sdk-php
// Usage:https://github.com/aliyun/openapi-sdk-php
$access_key_id = getenv("NLP_AK_ENV");
$access_key_secret = getenv("NLP_SK_ENV");
AlibabaCloud::accessKeyClient($access_key_id, $access_key_secret);
// use STS Token
// AlibabaCloud::stsClient('<your-access-key-id>', '<your-access-key-secret>', '<your-sts-token>')
->regionId('cn-hangzhou')
->asDefaultClient()->options([
]);
try {
$request = NlpAutoml::v20191111()->getPredictResult();
$result = $request
->withModelId(1818)
->withContent("中华人民共和国解放军")
->withModelVersion("V1")
->debug(true) // Enable the debug will output detailed information
->request();
print_r($result->toArray());
} catch (ClientException $exception) {
echo $exception->getMessage() . PHP_EOL;
} catch (ServerException $exception) {
echo $exception->getMessage() . PHP_EOL;
echo $exception->getErrorCode() . PHP_EOL;
echo $exception->getRequestId() . PHP_EOL;
echo $exception->getErrorMessage() . PHP_EOL;
}
文档内容是否对您有帮助?