獲取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;
}
文檔內容是否對您有幫助?