Skip to content

Commit ec539ae

Browse files
author
Vladislav Gubarev
committed
Added PHP/curl sample.
1 parent 6bd3e52 commit ec539ae

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ This project run to help API users with ready-to-use examples in a set of the mo
4949
* [fetch](./js/fetch)
5050
* [Axios](./js/axios)
5151
* [jQuery](./js/jquery)
52-
53-
Coming soon:
54-
5552
* `PHP`:
5653
* [cURL](./php/curl)
5754

php/curl/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# API4AI OCR text recognition sample
2+
3+
This directory contains a minimalistic sample that sends requests to the API4AI Text Recognition API.
4+
The sample is implemented in `PHP` using [cURL](https://www.php.net/manual/ru/book.curl.php) extension.
5+
6+
7+
## Overview
8+
9+
This API processes images and performs Optical Character Recognition.
10+
11+
12+
## Getting started
13+
14+
*PHP version `7.x` or greater and `php-curl` extension is required to run this script.*
15+
16+
Try sample with default args:
17+
18+
```bash
19+
php index.php
20+
```
21+
22+
Try sample with your local image:
23+
24+
```bash
25+
php index.php image.jpg
26+
```
27+
28+
29+
## About API keys
30+
31+
This demo by default sends requests to free endpoint at `demo.api4ai.cloud`.
32+
Demo endpoint is rate limited and should not be used in real projects.
33+
34+
Use [RapidAPI marketplace](https://rapidapi.com/api4ai-api4ai-default/api/ocr43/details) to get the API key. The marketplace offers both
35+
free and paid subscriptions.
36+
37+
[Contact us](https://api4.ai/contacts?utm_source=ocr_example_repo&utm_medium=readme&utm_campaign=examples) in case of any questions or to request a custom pricing plan
38+
that better meets your business requirements.
39+
40+
41+
## Links
42+
43+
* 📩 Email: hello@api4.ai
44+
* 🔗 Website: [http://api4.ai](https://api4.ai?utm_source=ocr_example_repo&utm_medium=readme&utm_campaign=examples)
45+
* 🤖 Telegram demo bot: https://t.me/a4a_ocr_bot
46+
* 🔵 Our API at RapidAPI marketplace: https://rapidapi.com/api4ai-api4ai-default/api/ocr43/details

php/curl/image.jpg

323 KB
Loading

php/curl/index.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env php
2+
3+
<?php
4+
// Example of using API4AI OCR text recognition.
5+
6+
// Use 'demo' mode just to try api4ai for free. Free demo is rate limited.
7+
// For more details visit:
8+
// https://api4.ai
9+
10+
// Use 'rapidapi' if you want to try api4ai via RapidAPI marketplace.
11+
// For more details visit:
12+
// https://rapidapi.com/api4ai-api4ai-default/api/ocr43/details
13+
$MODE = 'demo';
14+
15+
// Your RapidAPI key. Fill this variable with the proper value if you want
16+
// to try api4ai via RapidAPI marketplace.
17+
$RAPIDAPI_KEY = null;
18+
19+
$OPTIONS = [
20+
'demo' => [
21+
'url' => 'https://demo.api4ai.cloud/ocr/v1/results',
22+
'headers' => ['A4A-CLIENT-APP-ID: sample']
23+
],
24+
'rapidapi' => [
25+
'url' => 'https://ocr43.p.rapidapi.com/v1/results',
26+
'headers' => ["X-RapidAPI-Key: {$RAPIDAPI_KEY}"]
27+
]
28+
];
29+
30+
// Initialize request session.
31+
$request = curl_init();
32+
33+
// Check if path to local image provided.
34+
$data = ['url' => 'https://storage.googleapis.com/api4ai-static/samples/ocr-1.png'];
35+
if (array_key_exists(1, $argv)) {
36+
if (strpos($argv[1], '://')) {
37+
$data = ['url' => $argv[1]];
38+
} else {
39+
$filename = pathinfo($argv[1])['filename'];
40+
$data = ['image' => new CURLFile($argv[1], null, $filename)];
41+
}
42+
}
43+
44+
// Set request options.
45+
curl_setopt($request, CURLOPT_URL, $OPTIONS[$MODE]['url']);
46+
curl_setopt($request, CURLOPT_HTTPHEADER, $OPTIONS[$MODE]['headers']);
47+
curl_setopt($request, CURLOPT_POST, true);
48+
curl_setopt($request, CURLOPT_POSTFIELDS, $data);
49+
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
50+
51+
// Execute request.
52+
$result = curl_exec($request);
53+
54+
// Decode response.
55+
$raw_response = json_decode($result, true);
56+
57+
// Print raw response.
58+
echo join('',
59+
["💬 Raw response:\n",
60+
json_encode($raw_response),
61+
"\n"]);
62+
63+
// Parse response and get recognized text.
64+
$text = $raw_response['results'][0]['entities'][0]['objects'][0]['entities'][0]['text'];
65+
66+
// Close request session.
67+
curl_close($request);
68+
69+
// Print recognized text.
70+
echo join('',
71+
["\n💬 Recognized text: \n",
72+
$text,
73+
"\n"]);
74+
?>

0 commit comments

Comments
 (0)