How to Create UPC Barcodes in PHP

Cloudmersive
2 min readMar 14, 2023

--

If you’re running a growing ecommerce website, creating a simple UPC barcode generation workflow might become a key factor in getting your products out the door efficiently. Thankfully, using the PHP code examples provided below, you can rapidly generate 2D UPC-A and UPC-E barcodes as PNG files using each product’s UPC barcode number. These are low-code, easy-to-use APIs designed to get you up and running at no cost.

First things first, head to our website and register a free account so you can get your free-tier API key. After that, run the following command to install the SDK:

composer require cloudmersive/cloudmersive_barcode_api_client

Next, use the below code examples to generate UPC-A or UPC-E barcodes with your corresponding barcode numbers. You can generate UPC-A using the following examples:

<?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\GenerateBarcodeApi(


new GuzzleHttp\Client(),
$config
);
$value = "value_example"; // string | UPC-A barcode value to generate from

try {
$result = $apiInstance->generateBarcodeUPCA($value);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling GenerateBarcodeApi->generateBarcodeUPCA: ', $e->getMessage(), PHP_EOL;
}
?>

And you can generate UPC-E using the final code examples below:

<?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\GenerateBarcodeApi(


new GuzzleHttp\Client(),
$config
);
$value = "value_example"; // string | UPC-E barcode value to generate from

try {
$result = $apiInstance->generateBarcodeUPCE($value);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling GenerateBarcodeApi->generateBarcodeUPCE: ', $e->getMessage(), PHP_EOL;
}
?>

As outlined in the code comments, you’ll need to include your API key in the $config variable, and parse your barcode values in the $value variable. It’s just that simple — no more code required!

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.