简体   繁体   English

在PHP中生成动态二维码

[英]Generate dynamic QR codes in PHP

I am working on a project where we need to generate dynamic QR code.我正在做一个需要生成动态二维码的项目。 After the QR code is printed, we tend to make changes to the data so we need to generate dynamic QR code where there is no need to reprint the QR code.二维码打印出来后,我们往往会修改数据,所以我们需要生成动态二维码,不需要重新打印二维码。

I already used one of the PHP library to generate QR code but it is static which changes every time the data is altered.我已经使用了PHP 库之一来生成 QR 码,但它是 static,每次更改数据时都会更改。

This is an example of generating a QR code:这是生成二维码的示例:

<?php 

    include('../lib/full/qrlib.php'); 

    // outputs image directly into browser, as PNG stream 
    QRcode::png('PHP QR Code :)');

?>

Is there anything that I can use to generate dynamic QR codes?有什么东西可以用来生成动态二维码吗?

Thanks in advance.提前致谢。

Once printed/generated there is no way to change the content of a QRcode, it will always be the same.一旦打印/生成,就无法更改二维码的内容,它将始终相同。

But you can make it "dynamic" by storing in your QRcode a distinct URL* and on the server side pointed by the URL, you can change dynamically the content associated with this unique URL to whatever you want (with URL rewriting or the querying of a database), with the same QRcode.但是您可以通过在您的 QRcode 中存储一个不同的 URL* 并在该 URL 指向的服务器端使其“动态”,您可以将与此唯一 URL 关联的内容动态更改为您想要的任何内容(通过 URL 重写或查询一个数据库),具有相同的二维码。

*it can also be an id, depending on the usage, if you have an application reading the QRcode for example, the application could render the content associated with the id in question, and you could change which content is associated with each id in your application/backend without changing the QRcode. *它也可以是一个 id,具体取决于用法,例如,如果您有一个应用程序读取 QRcode,该应用程序可以呈现与相关 id 关联的内容,并且您可以更改与您的每个 id 关联的内容应用程序/后端而无需更改 QR 码。

On these both cases, you dynamically change on the application side the relation between the unique URL/id and the associated content, not on the representation side (the printed on screen/paper QRcode)在这两种情况下,您在应用程序端动态更改唯一 URL/id 和相关内容之间的关系,而不是在表示端(打印在屏幕/纸上的二维码)

I just came up with a simple solution.我只是想出了一个简单的解决方案。 Rather than just storing all the data on the QR code and trying to change it when altering the data, I would just store the URL in the QR code pointing to server side and the data can be stored there.与其只是将所有数据存储在 QR 码上并在更改数据时尝试更改它,我只会将 URL 存储在指向服务器端的 QR 码中,并且数据可以存储在那里。 It can easily be altered there and hence our QR code will remain same.它可以在那里轻松更改,因此我们的二维码将保持不变。

And yet there is another technically more practical way, which is the method where you can use an already ready-made API, like the one below, to create and manage your dynamic QR codes还有另一种技术上更实用的方法,即您可以使用已经准备好的 API(如下图所示)来创建和管理您的动态二维码

<?php

$request = new HttpRequest();
$request->setUrl('https://qr-code-dynamic-and-static1.p.rapidapi.com/qrcode/dynamic');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders([
'content-type' => 'application/json',
'api-key' => '<REQUIRED>',
'X-RapidAPI-Key' => '275379ed5emsh4b47685996c279ap1bcf73jsnc90319ab3f13',
'X-RapidAPI-Host' => 'qr-code-dynamic-and-static1.p.rapidapi.com'
]);

$request->setBody('{
"title": "This is a dynamic QR Code",
"description": "The link can be modified later",
"link": "https://rapidapi.com/updeploy-tools/api/qr-code-dynamic-and-static1"
}');

try {
$response = $request->send();

echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}

See this link for more information: https://rapidapi.com/updeploy-tools/api/qr-code-dynamic-and-static1有关更多信息,请参阅此链接: https://rapidapi.com/updeploy-tools/api/qr-code-dynamic-and-static1

Try google API, I use for generating bitcoin QR code:试试谷歌 API,我用来生成比特币二维码:

<?php
$btcadress = "BTCADRESS";
echo '<img src="https://chart.googleapis.com/chart?chs=250x250&cht=qr&chl='.$btcadress.'">';
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM