编程语言
首页 > 编程语言> > PHP-为什么QR Generator在生成的QR图像中没有给我正确的URL?

PHP-为什么QR Generator在生成的QR图像中没有给我正确的URL?

作者:互联网

我正在使用QR Generator,并为此使用了Google API.我正在生成一个具有URL的QR码,或者可以说是QR码中指向网站的链接.当我打印链接时该链接是正确的,但是问题是当我将其传递给QR生成器时,链接不正确.正确的链接是这样的. (http://localhost/crs/web/index.php/birthPublicSearch/birthCertificate/view/cert/B/MTc5OQ%3D%3D).但是在QR代码中/ MTC5OQ之后的字符未显示在链接中,这意味着QR图像中未显示==.谁可以帮我这个事.下面是我的代码.
另一个问题是,当我使用手机重定向到对图像进行编码的URL时,URL中的斜杠更改为/,并且URL无法打开,并且显示了有关使用Neo Reader的感谢消息.我该怎么解决.

<?php
class QRGenerator { 
protected $size; 
protected $data; 
protected $encoding; 
protected $errorCorrectionLevel; 
protected $marginInRows;
protected $debug; 

public function __construct($data,$size='100',$encoding='UTF-8',$errorCorrectionLevel='L',$marginInRows=4,$debug=false) { 

    $this->data=urlencode($data); 
    $this->size=100;
    $this->encoding=($encoding == 'Shift_JIS' || $encoding == 'ISO-8859-1' || $encoding == 'UTF-8') ? $encoding : 'UTF-8'; 
    $this->errorCorrectionLevel=($errorCorrectionLevel == 'L' || $errorCorrectionLevel == 'M' || $errorCorrectionLevel == 'Q' || $errorCorrectionLevel == 'H') ?  $errorCorrectionLevel : 'L';
    $this->marginInRows=($marginInRows>0 && $marginInRows<10) ? $marginInRows:4; 
    $this->debug = ($debug==true)? true:false;     
} 
public function generate(){ 

    $QRLink = "https://chart.googleapis.com/chart?cht=qr&chs=".$this->size."x".$this->size.                 
               "&chl=" . $this->data .  
               "&choe=" . $this->encoding . 
               "&chld=" . $this->errorCorrectionLevel . "|" . $this->marginInRows; 
if ($this->debug) echo $QRLink;          
return $QRLink;} } ?>

我在其中打印QR码的TD为:

<td align="center" valign="top">
    <div style="float: left; margin-left: -230px; margin-top: 34px; padding-right: 20px;">
     <script type="text/javascript">                                                                            
       (function() {
         var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
          po.src = 'https://apis.google.com/js/plusone.js';
          var s = document.getElementsByTagName('script')[0];     
          s.parentNode.insertBefore(po, s);
     });
      </script>
  </div>                                                   
<?php


$unique_value = base64_encode($birhtId);// Unique Value is MTc5OQ

$data='localhost/web/index.php/birthPublicSearch/birthCertificate/view/cert/B/'.$unique_value.'%3D%3D';
$size='200';
$ex1 = new QRGenerator($data,$size); 
$img1 = "<img src=".$ex1->generate().">";
$content ='<table>
    <tr>
         <td colspan="2">'.$img1.'</td>
    </tr>                                                                     
  </table>';                                                         
    print_r($content);
?>
</td>

解决方法:

因为构建URL时必须自己对参数运行urlencode函数,所以:

$data='localhost/web/index.php/birthPublicSearch/birthCertificate/view/cert/B/' . urlencode($unique_value) . '%3D%3D';

您必须在此处进行urlencode的原因是,您不仅拥有MTc5OQ,还拥有MTc5OQ ==. =必须进行编码.

您还应该考虑在其他地方也这样做:

$QRLink = "https://chart.googleapis.com/chart?cht=qr&chs=" . urlencode($this->size."x".$this->size) .                 
           "&chl=" . urlencode($this->data) .  
           "&choe=" . urlencode($this->encoding) . 
           "&chld=" . urlencode($this->errorCorrectionLevel . "|" . $this->marginInRows);

标签:php,qr-code
来源: https://codeday.me/bug/20191009/1882793.html