Installation
composer require alibabacloud/alinlp-20200629
Configure access credentials using environment variables
Note:
An AccessKey for an Alibaba Cloud account has full access permissions to all APIs. This poses a high security threat. We recommend that you create and use a Resource Access Management (RAM) user for API calls and routine O&M. To create a RAM user, log on to the RAM console.
Do not save your AccessKey ID and AccessKey secret in your code. Leaking these keys can lead to security threats. Instead, configure environment variables to save and access your keys.
Configure environment variables on Linux and macOS
export NLP_AK_ENV=<access_key_id> export NLP_SK_ENV=<access_key_secret>Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret. For more information about how to obtain an AccessKey ID and an AccessKey secret, see Step 2: Obtain an AccessKey for your account.
Windows configuration method
Create an environment variable file. Add the
NLP_AK_ENVandNLP_SK_ENVenvironment variables. Then, enter your AccessKey ID and AccessKey secret.Restart the Windows operating system.
Code example
<?php
namespace demo;
require __DIR__ . '/vendor/autoload.php';
use AlibabaCloud\SDK\Alinlp\V20200629\Alinlp;
use AlibabaCloud\SDK\Alinlp\V20200629\Models\GetNerChEcomRequest;
use Darabonba\OpenApi\Models\Config;
$config = new Config();
/**
* An AccessKey for an Alibaba Cloud account has full access permissions to all APIs. This poses a high security threat.
* Create and use a Resource Access Management (RAM) user for API calls and routine O&M. To create a RAM user, log on to the RAM console.
* This example shows how to save the AccessKey ID and AccessKey secret in environment variables. You can also save them in a configuration file as needed.
* Do not save your AccessKey ID and AccessKey secret in your code. This can lead to security threats if the keys are leaked.
*/
$config->accessKeyId = getenv('NLP_AK_ENV');
$config->accessKeySecret = getenv('NLP_SK_ENV');
$config->regionId = "cn-hangzhou";
$config->endpoint = "alinlp.cn-hangzhou.aliyuncs.com";
$client = new Alinlp($config);
$request = new GetNerChEcomRequest();
$request->serviceCode = 'alinlp';
$request->text = 'Electric multi-function grinder';
try {
$response = $client->getNerChEcom($request);
$json_string = json_encode($response->body, JSON_UNESCAPED_UNICODE);
echo $json_string;
} catch (TeaUnableRetryError $e) {
var_dump($e->getMessage());
var_dump($e->getErrorInfo());
var_dump($e->getLastException());
var_dump($e->getLastRequest());
}
Note: In the code `GetNerChEcomRequest`, `GetNerChEcom` is the `actionName` of the algorithm. Replace it with the value for the algorithm that you want to use. You can find this value in the API Reference documentation under Request Parameters > Action > Sample Value.
For example, to call the Basic Edition - Chinese Word Segmentation - General algorithm, go to Chinese Word Segmentation (Basic Edition), copy the sample value shown in the following figure, and replace `GetNerChEcomRequest` with `GetWsChGeneralRequest`. After the replacement, you must also change the request parameters for the algorithm. For more information, see the API documentation.
Troubleshoot call exceptions
If an exception occurs during a call, see Troubleshoot call exceptions (error codes). The document contains a table that describes the cause of each error and provides a solution.