How to Create a QRCode using Google QRCode API in PHP

May 30, 2020
PHP
google qrcode api

Getting Started

I’ve used Bootstrap in this tutorial which is a CSS framework and is included in the downloadable of this tutorial, but if you want, you may download Bootstrap using this link.

Creating a QR Code

QR Code will be generated as an image, and it will look something like this:

<img src='https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=$code&choe=UTF-8'>

Descriptions:

cht=qrRequiredSpecifies a QR code.
chs=RequiredImage size.
chl=RequiredThe data to encode. Data can be digits (0-9), alphanumeric characters, binary bytes of data, or Kanji.
choe=OptionalHow to encode the data in the QR code.

For additional info, you may visit Google’s documentation on QR Code using this link.

Creating a Script

To provide a working example, let’s create a form and generate a QR Code on the inputted text. Please create a new file, name it as index.php and paste the codes below.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>How to Create a QRCode using Google QRCode API</title>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1 class="page-header text-center">QRCode using Google QRCode API</h1>
    <div class="row">
        <div class="col-sm-3 col-sm-offset-3">
            <form method="POST">
                <div class="form-group">
                    <label for="">Text to Convert to QRCode</label>
                    <input type="text" class="form-control" name="text_code">
                </div>
                <button type="submit" class="btn btn-primary" name="generate">Generate QRCode</button>
            </form>
        </div>
        <div class="col-sm-3">
            <?php
                if(isset($_POST['generate'])){
                    $code = $_POST['text_code'];
                    echo "
                        <img src='https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=$code&choe=UTF-8'>
                    ";
                }
            ?>
        </div>
    </div>
</div>
</body>
</html>

That ends this tutorial. Happy Coding!

Download Here

Leave a Reply

Your email address will not be published. Required fields are marked *