Current Location: Home> Latest Articles> Complete Guide to Using PHP with Alibaba Cloud OCR for Identity Card Recognition

Complete Guide to Using PHP with Alibaba Cloud OCR for Identity Card Recognition

M66 2025-07-18

Introduction

With the advent of the digital age, leveraging technology to improve the efficiency and accuracy of identity card information recognition has become increasingly important. Alibaba Cloud OCR (Optical Character Recognition) technology uses image recognition to convert identity cards and other paper documents into digital text, significantly enhancing data processing speed and accuracy. This article will explain how to use PHP in combination with Alibaba Cloud OCR to recognize long-unupdated identity card information.

Creating an Alibaba Cloud Account and Obtaining API Keys

Before using Alibaba Cloud OCR services, you need to create an Alibaba Cloud account and obtain API keys. The API keys will allow you to access the Alibaba Cloud OCR interface and authenticate your identity. After logging into the Alibaba Cloud console, click on the “AccessKey” menu at the top right corner to generate your own API keys.

Installing and Configuring PHP Environment

Ensure that your server has PHP installed and that the version is compatible with Alibaba Cloud OCR SDK. In the PHP configuration file, enable the following two extensions: extension=php_openssl.dll and extension=php_curl.dll. These extensions will allow PHP to communicate with Alibaba Cloud OCR services.

Downloading and Configuring Alibaba Cloud OCR PHP SDK

Alibaba Cloud provides an OCR SDK for PHP developers, making it easier to integrate OCR functionality. You can download and extract the SDK from the Alibaba Cloud website. Once downloaded, add the path of the extracted SDK folder to your PHP project's include_path configuration.

Writing PHP Code for Identity Card Recognition

Before writing the code, you need to review Alibaba Cloud OCR's API documentation to ensure correct usage of the related interfaces and parameters. Here is a simple PHP code example that demonstrates how to use Alibaba Cloud OCR to recognize identity card information:

<?php
require_once 'aliyun-php-sdk-core/Config.php';

use GreenRequestV20180509TextScanRequest;
use CrowdRequestV20171020RecognizeIdentityCardRequest;

$accessKeyId = "<YourAccessKeyId>";
$accessSecret = "<YourAccessSecret>";

$iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $accessKeyId, $accessSecret);
$client = new DefaultAcsClient($iClientProfile);

$request = new RecognizeIdentityCardRequest();
$request->setRegionId("cn-hangzhou");
$imageURL = "<YourImageURL>";
$request->setImageURL($imageURL);

$response = $client->getAcsResponse($request);

// Parse front-side information of the identity card
$frontInfo = $response->getData()->getFrontResult()->getCardArea();
$backInfo = $response->getData()->getBackResult()->getCardArea();

echo "Front-side information of the identity card:";
echo "Name: " . $frontInfo->name . "\n";
echo "Gender: " . $frontInfo->sex . "\n";
echo "Nationality: " . $frontInfo->nationality . "\n";
echo "Date of Birth: " . $frontInfo->birth . "\n";
echo "Address: " . $frontInfo->address . "\n";

echo "Back-side information of the identity card:";
echo "Issuing Authority: " . $backInfo->issue . "\n";
echo "Validity: " . $backInfo->valid_date . "\n";
?>

Running the Code and Obtaining Identity Card Information

Ensure that your network connection is properly configured and able to access Alibaba Cloud servers. In your terminal, navigate to the directory where the PHP file is stored and run the command php filename.php. If everything is set up correctly, you will see the identity card's name, gender, nationality, date of birth, address, issuing authority, and validity information displayed in the console.

Conclusion

Through this tutorial, you have learned how to use PHP and Alibaba Cloud OCR services to recognize identity card information. With Alibaba Cloud OCR's powerful image recognition capabilities, developers can quickly and efficiently recognize identity card and other document information for a variety of real-world applications. We hope this article has been helpful and inspires you to further explore and apply related technologies.