首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用"endroid/qr-code“库生成qrcode时的未知错误

用"endroid/qr-code“库生成qrcode时的未知错误
EN

Stack Overflow用户
提问于 2020-01-15 18:46:26
回答 1查看 3.2K关注 0票数 1

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

代码语言:javascript
复制
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

我在这里已经有一段时间了,我被困住了。现在确实有关于库的深入文档,请帮助我解决问题。这是我的代码:

代码语言:javascript
复制
<?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>

下面是指向图书馆的链接

EN

回答 1

Stack Overflow用户

发布于 2020-01-15 18:49:57

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

代码语言:javascript
复制
  $qrcode = new QrCode("$url");
  echo "</br>";
  header('Contert-Type: application/' . $qrCode->getContentType());
  $createCode = $qrCode->writeString();

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

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59757833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档