<?php
require_once __DIR__ . '/vendor/autoload.php'; // 引入阿里雲SDK
use AlibabaCloud\SDK\Core\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
AlibabaCloud::accessKeyClient('your-ak', 'your-sk') // 填入Access Key
-> regionId('cn-shanghai') // 區域ID
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()
->product('Ocr')
->version('2015-11-15')
->action('RecognizeCharacter')
->method('POST')
->host('ocr.cn-shanghai.aliyuncs.com') // API地址->options([
'query' => [
'RegionId' => 'cn-shanghai', // 區域ID
'ImageUrl' => 'your-image-url', // 填入圖片URL
],
])
->request();
print_r($result->toArray()); // 出口識別結果} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}
執行成功後,您將會看到輸出的識別結果,包括文字內容、置信度以及文字位置等詳細信息。