Current Location: Home> Latest Articles> How to Implement Business Card Recognition with PHP and Alibaba Cloud OCR: A Complete Guide

How to Implement Business Card Recognition with PHP and Alibaba Cloud OCR: A Complete Guide

M66 2025-06-12

How to Implement Business Card Recognition with PHP and Alibaba Cloud OCR: A Complete Guide

With the development of the digital era, business card recognition technology has become an important tool for improving work efficiency. By integrating Alibaba Cloud OCR services with PHP, developers can easily automate the recognition of information on business cards, reducing the time and errors associated with manual entry. This article will introduce how to use PHP to call Alibaba Cloud OCR services to implement business card recognition and provide detailed code examples.

1. Register an Alibaba Cloud Account and Enable OCR Service

First, we need to register an account on Alibaba Cloud and enable the OCR service. Follow the steps below:

  1. Visit the official Alibaba Cloud website and complete the registration.
  2. Log in to the Alibaba Cloud Console.
  3. Search for "OCR" in the console and go to the OCR service page.
  4. Click the "Enable Service" button and complete the activation process.

2. Obtain Alibaba Cloud OCR Access Key and Access Secret

Before using the OCR service, you need to obtain the Access Key and Access Secret for Alibaba Cloud OCR. Follow these steps:

  1. Log in to the Alibaba Cloud Console.
  2. Search for "Access Key" and go to the management page.
  3. Click "Create Access Key" and save the generated credentials.

3. Install PHP's aliyun-sdk Dependency Library

To call Alibaba Cloud OCR services from PHP, we need to install the aliyun-sdk dependency library. Here’s how to do it:

  1. Create a composer.json file in your project root directory.
  2. Add the following content to the composer.json file:
{
    "require": {
        "aliyuncs/oss-sdk-php": "^2.5"
    }
}
  1. Open the terminal, navigate to your project directory, and run the following command to install the dependencies:
composer install

4. Write PHP Code to Implement Business Card Recognition

Once the setup is complete, we can start writing the PHP code to call the Alibaba Cloud OCR service and recognize business card information. Here is a simple code example:

<?php
require 'vendor/autoload.php';

use AliyunApiOcrRequestV20191230RecognizeBusinessCardRequest;
use AliyunCoreDefaultAcsClient;
use AliyunCoreProfileDefaultProfile;

function recognizeBusinessCard($imagePath) {
    $accessKeyId = 'YourAccessKeyId'; // Replace with your own Access Key Id
    $accessSecret = 'YourAccessSecret'; // Replace with your own Access Secret
    $regionId = 'cn-shanghai'; // Replace with your region id

    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessSecret);
    $client = new DefaultAcsClient($profile);
    
    $request = new RecognizeBusinessCardRequest();
    $request->setImageURL("http://your-domain.com/your-image.jpg"); // Replace with your image URL

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

$imagePath = './business-card.jpg'; // Replace with your business card image path
$result = recognizeBusinessCard($imagePath);

var_dump($result);
?>

In this code, we first use the require statement to include the aliyun-sdk dependency library. Then, we define a function called `recognizeBusinessCard` that sends a request to Alibaba Cloud's OCR API to recognize the business card's content. Inside the function, we create a `DefaultAcsClient` object and use `RecognizeBusinessCardRequest` to initiate the recognition request. Finally, we return the recognition results.

Note: Make sure to replace the Access Key and image path with your actual credentials and image URL or path.

5. Run the PHP Code

After writing the code, you can run the PHP file from the command line and view the recognition results. Use the following command:

php your-php-file.php

After running the command, you should see the business card recognition results output in the terminal.

Conclusion

By combining PHP with Alibaba Cloud OCR services, developers can easily implement business card recognition functionality. Simply register and enable OCR services on Alibaba Cloud, install the necessary dependencies, and write a small amount of PHP code to automatically recognize business card details, improving work efficiency and reducing the errors from manual entry.