繁体   English   中英

使用“endroid/qr-code”库生成二维码时未发现错误

[英]Uncaught error while generating qrcode with "endroid/qr-code" library

我正在用 PHP 创建一个二维码生成器程序,我正在使用一个名为qr-code的库。 我已经成功获得了用户希望转换为二维码的字符串,在生成二维码时,我收到以下错误:

Uncaught Error: Call to a member function getContentType() on null in index.php:13 Stack trace: #0 {main} thrown in index.php on line 13

我已经在这里呆了一段时间了,我被卡住了。 现在确实有相关库的深入文档,请帮帮我 这是我的代码:

<?php
require __DIR__ . '/vendor/autoload.php';

use Endroid\QrCode\QrCode;


// Get url from form
if (isset($_GET['submit'])) {
  $url = strval($_GET['url']);
  echo gettype($url);
  $qrcode = new QrCode("$url");
  echo "</br>";
  header('Contert-Type: application/' . $qrCode->getContentType());
  $createCode = $qrCode->writeString();
}

?>


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <meta name="description" content="" />
    <title>QR Code Generator</title>

    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="./bootstrap.min.css" />

    <style>
      body {
        padding-top: 5rem;
      }
      .starter-template {
        padding: 3rem 1.5rem;
        text-align: center;
      }

      .bd-placeholder-img {
        font-size: 1.125rem;
        text-anchor: middle;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
      }

      @media (min-width: 768px) {
        .bd-placeholder-img-lg {
          font-size: 3.5rem;
        }
      }
    </style>
  </head>

  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-danger fixed-top">
      <a class="navbar-brand text-center" href="#">QR Code Generator</a>
    </nav>

    <main role="main" class="container">
      <div class="starter-template">
        <form method="GET" action="<? htmlspecialchars($_SERVER['PHP_SELF']);?>">
          <div class="form-group">
            <label for="url">URL</label>
            <input type="text" class="form-control" id="url" name="url" />
          </div>
          <button type="submit" name="submit" class="btn btn-danger">Generate</button>
        </form>
        <div class="d-flex justify-content-center mt-5">
          <div
            class="starter-template card text-white bg-danger mb-3"
            style="max-width: 20rem;"
          >
            <div class="card-body">
              <p class="card-text">
                <?php echo $createCode;?>
              </p>
            </div>
          </div>
        </div>
      </div>
    </main>
    <!-- /.container -->
    <script
      src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
      integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
      integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
      integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
      crossorigin="anonymous"
    ></script>
  </body>
</html>

这是图书馆的链接

您正在尝试使用不存在的$qrCode ,请仔细查看您的代码:

  $qrcode = new QrCode("$url");
  echo "</br>";
  header('Contert-Type: application/' . $qrCode->getContentType());
  $createCode = $qrCode->writeString();

首先你使用$qrcode然后你使用$qrCode (它不存在)。

将第一行更改为: $qrCode = new QrCode("$url");

暂无
暂无

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

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