PHP server-side SDK integration
This topic describes how to integrate the server-side SDK for PHP.
Prerequisites
You have an AccessKey for your Alibaba Cloud account. For more information, see Create an AccessKey.
You have downloaded the server-side SDK package for PHP from the Captcha console.
The server-side development environment is PHP 5.3 or later.
Install the SDK
Decompress the downloaded `php_sdk.zip` package to any folder on the server and ensure that the folder has read permissions.
In your PHP file, include the required SDK file and import it using an alias. Ensure that you specify the correct file path. For example:
<?php include_once './aliyun-php-sdk-core/Config.php'; use afs\Request\V20180112 as Afs; //Do something below...
Notes
Classes related to the Alibaba Cloud Captcha server-side SDK are in the `com.aliyuncs.IAcsClient` namespace.
The default connection timeout for SDK API calls is 3 seconds, and the read timeout is 80 seconds. To customize these timeout periods, call the constructor with the `connectTimeout` and `readTimeout` parameters.
Initialize IClientProfile
`IClientProfile` is the interface used for all SDK operations that interact with the Alibaba Cloud Captcha server.
`IClientProfile` is reusable. We recommend that you set it as a globally unique object for your application.
<?php
include_once './aliyun-php-sdk-core/Config.php';
use afs\Request\V20180112 as Afs;
// Replace YOUR ACCESSKEY and YOUR ACCESS_SECRET with your Alibaba Cloud AccessKey ID and AccessKey secret.
$iClientProfile = DefaultProfile::getProfile("cn-hangzhou", "YOUR ACCESSKEY", "YOUR ACCESS_SECRET");
$client = new DefaultAcsClient($iClientProfile);
DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "afs", "afs.aliyuncs.com");
Call the Alibaba Cloud Captcha server-side API
After you initialize `IClientProfile`, you can call the `AnalyzeNvcRequest` API. You also need to develop a handler class for frontend requests and a method to process the results.
<?php
$request = new Afs\AnalyzeNvcRequest();
$request->setData("xxx"); // Required. The value is obtained from the frontend by calling the getNVCVal method.
// Use the setScoreJsonStr method to define the mapping between server-side results and frontend actions. This also notifies the Captcha server to authorize secondary authentication.
// Note: The frontend page must strictly follow this mapping. Otherwise, the call will fail.
// For example, if you declare "400":"NC" in the setScoreJsonStr method, your frontend must trigger slide verification (NC) when the server returns 400. If you trigger any other type of verification, the process will fail.
$request->setScoreJsonStr("{\"200\":\"PASS\",\"400\":\"NC\",\"800\":\"BLOCK\"}"); // Set the client-side action for each result as needed.
$response = $client->getAcsResponse($request);
print_r($response);
?>