我正在使用QR生成器并使用谷歌API。我正在生成一个包含URL的QR码,或者我们可以说在QR码中链接到一个网站。当我打印它时链接是正确的但问题是当我将它传递给QR生成器时,链接不正确。正确的链接是这样的。 (http://localhost/crs/web/index.php/birthPublicSearch/birthCertificate/view/cert/B/MTc5OQ%3D%3D)。但在QR码中/ MTC5OQ之后的字符未显示在链接中意味着%3D%3D未显示在QR图像中。谁可以帮我这个事。以下是我的代码。 另一个问题是当我使用我的手机重定向到编码图像的网址时,网址中的斜杠更改为%2F并且网址未打开,并显示使用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>
答案 0 :(得分:1)
因为在构建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);
答案 1 :(得分:0)
<?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=$data; // This is where I changed
$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=". urlencode($this->size."x".$this->size) .
"&chl=" . $this->data . // This is where I changed
"&choe=" .urlencode($this->encoding) .
"&chld=" .urlencode($this->errorCorrectionLevel . "|" . $this->marginInRows);
if ($this->debug) echo $QRLink;
return $QRLink;
}
}
?>
我们想要显示QR码的地方,请输入以下代码:
<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
$birhtId='1234';
$bir_id = base64_encode($birhtId);
$data='crsdemo.lsipl.com/crs/web/index.php/birthPublicSearch/birthCertificate/view/cert/B/'.urlencode($bir_id);
?>
<script>
console.log("<?php echo $data ?>");
</script>
<?php
$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>