McAPI - QR API PHP Sample

PHP sample code to create QR barcodes with the McAPI QR REST API service. The sample uses PHP 7 with plain cURL; alternatives are Unirest or any of the available cURL wrappers (see for example here). When using Laravel, an alternative to cURL is Guzzle.

Requirements: A free RapidAPI account. Replace YOUR_API_KEY with your own api key.

The sample can be used with the free tier of the API, see RapidAPI McAPI QR Listing for available plans.

See the overview page for available parameters and error codes.

Create QR code using PHP

Shown is the creation of a QR code with PHP encoding the web address https://mcapi.io. The code is created in red (dot color) and blue (finder color) with a dot size of 10 pixels. The dot style is set to "1" (for separated dots), the shape is set to "1" (for dots in disc or circle shape). The output format is "png". As the QR error correction is not set, the API will per default use ECC level "M", sufficient for most purposes. Use "Q" (numeric value "3") for highest error correction capability.

The source code:

<?php
$curl = curl_init();

curl_setopt_array($curl, [
	CURLOPT_URL => "https://mcapi-qr.p.rapidapi.com/",
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_FOLLOWLOCATION => true,
	CURLOPT_ENCODING => "",
	CURLOPT_MAXREDIRS => 10,
	CURLOPT_TIMEOUT => 30,
	CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST => "POST",
	CURLOPT_POSTFIELDS => "{
    \"data\": \"https://mcapi.io",
    \"dotSize\": 10,
    \"finderColor\": \"#0000FF\",
    \"dotColor\": \"#FF0000\",
    \"dotStyle\": 1,
    \"dotShape\": 1,
    \"format\": \"png\"
}",
	CURLOPT_HTTPHEADER => [
		"content-type: application/json",
		"x-rapidapi-host: mcapi-qr.p.rapidapi.com",
		"x-rapidapi-key: YOUR_API_KEY"
	],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
	echo "cURL Error #:" . $err;
} else {
	echo $response;
}
?>

The response data will be delivered as a JSON object in $response, e.g.:

{
  "service":"McAPI QR Code Generator, https://mcapi.io",
  "version":"V1",
  "format":"png",
  "code":"data:image/png;base64,iVBORw ... K5CYII="
}

McAPI QR API - Sample QR barcode with PHP

The returned code from the request:

Image of McAPI QR API Sample Barcode PHP

Encoding UTF-8 characters and text in QR codes with PHP

PHP will natively handle UTF-8 characters or strings (like german "umlauts", e.g. "ÄÖÜ", or non-latin script, e.g. "日本語"). Here's an example with the same parameters as above but with Unicode content (umlauts and japanese characters):

...

	CURLOPT_POSTFIELDS => "{
    \"data\": \"123456abcdefÄÖÜ日本語\",
    \"dotSize\": 10,
    \"finderColor\": \"#0000FF\",
    \"dotColor\": \"#FF0000\",
    \"dotStyle\": 1,
    \"dotShape\": 1,
    \"format\": \"png\"
}"

...

The generated QR code with Unicode content:

Image of McAPI QR API Sample Barcode Unicode PHP

Scanning QR codes

It's recommended to test scan the created QR codes. On iOS, the built-in camera app will natively detect and scan QR codes in the camera's view. For Android, use Google's Zxing Barcode Scanner app (free). Both apps will scan from the computer display, no need to print the codes.

Back to McAPI QR API main page.