PHP server-side SDK integration

更新时间:
复制 MD 格式

IClientProfile is the interface for interacting with the Alibaba Cloud Captcha server-side API. All SDK operations are performed through IClientProfile.

Prerequisites

  • You have an AccessKey for your Alibaba Cloud account.

  • You have downloaded the PHP server-side SDK package from the Alibaba Cloud Captcha console.

  • The server-side development environment is PHP 5.3 or later.

Install the SDK

  1. Unzip the downloaded php_sdk.zip package to a folder on your server. Ensure that the folder has read permissions.

  2. In your PHP file, add the header file, specify the correct file path, and import the alias. For example:

    <?php
    include_once './aliyun-php-sdk-core/Config.php';
    use  afs\Request\V20180112 as Afs;
    //Do something below...
    

Notes

  • Classes for the Alibaba Cloud Captcha server-side SDK are in the com.aliyuncs.IAcsClient package.

  • The default connection timeout for SDK calls is 3 seconds, and the read timeout is 80 seconds. To customize these timeouts, you can call the constructor with the connectTimeout and readTimeout parameters.

Initialize IClientProfile

Note

IClientProfile is reusable. You can set it as a globally unique object in your application.

<?php
include_once './aliyun-php-sdk-core/Config.php';
use afs\Request\V20180112 as Afs;
// Replace YOUR_ACCESS_KEY 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 initializing IClientProfile, call the APIs provided by the Alibaba Cloud Captcha service. Develop a handler class for frontend page requests and a method to process the results as needed.

For more information about the Alibaba Cloud Captcha service APIs, see Alibaba Cloud Captcha service APIs for web and HTML5 applications.

$request = new Afs\AuthenticateSigRequest();
$request->setSessionId("xxx");// The session ID. Required. Obtain this value from the frontend and do not change it.
$request->setToken("xxx");// The unique request token. Required. Obtain this value from the frontend and do not change it.
$request->setSig("xxx");// The signature string. Required. Obtain this value from the frontend and do not change it.
$request->setScene("xxx");// The scene identifier. Required. Obtain this value from the frontend and do not change it.
$request->setAppKey("xxx");// The application type identifier. Required. Specify this on the backend.
$request->setRemoteIp("xxx");// The client IP address. Required. Specify this on the backend.
$response = $client->getAcsResponse($request);// A return code of 100 means the signature verification passed. A code of 900 means it failed.
print_r($response);