How to Scan Different Barcodes in PHP
While some barcodes are certainly more common than others (such as EAN and UPC), there are, in fact, a surprisingly large number of different barcode formats which your business might encounter. Thankfully, the free-to-use API provided in this article allows you to easily scan and retrieve information from a wide variety of barcode formats, accepting regular 2d barcode image files (in common formats such as PNG, JPG) for each request.
You can quickly & easily structure your API call using the ready-to-run PHP code examples included below. To authenticate your requests, register a free account on our website to get your free-tier API key first (this will allow up to 800 API calls per month, with no commitments upon reaching that limit).
First, run this command to install the SDK:
composer require cloudmersive/cloudmersive_barcode_api_client
Next, use the following code to structure your request:
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\BarcodeScanApi(
new GuzzleHttp\Client(),
$config
);
$image_file = "/path/to/inputfile"; // \SplFileObject | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try {
$result = $apiInstance->barcodeScanImage($image_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling BarcodeScanApi->barcodeScanImage: ', $e->getMessage(), PHP_EOL;
}
?>
This operation will first identify the type of barcode it processed and subsequently provide a raw text string with information on the associated product. Nice and simple!