Why QR Generator does not give me the correct URL

2019-02-21 02:42发布

问题:

I am using QR Generator and using the google API for this. I am generating a QR code which has a URL or we can say link to a website in the QR code. The link is correct when i print it But the problem is that when i pass it to the QR generator, the link is not correct. The correct link is like this. (http://localhost/crs/web/index.php/birthPublicSearch/birthCertificate/view/cert/B/MTc5OQ%3D%3D). But in QR Code The characters after /MTC5OQ does not show in link means that %3D%3D in not shown in QR Image. Can any one help me on this. Below is my code. The other problem is also that when i redirect to the url encoding the image using my cell phone the slashes in the url changes to %2F and the url does not open and a Thank you message is shown for using Neo Reader. How can I solve it.

<?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;} } ?>

And the TD in which i am printing the QR code is as:

<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>

回答1:

Because you have to run a urlencode function on the parameters yourself when building the url:

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

The reason you have to urlencode here is because you don't have just MTc5OQ but MTc5OQ==. The = needs to be encoded.

You should consider doing the same in that other spot as well:

$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);


回答2:

<?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; 
} 
} 
?>

And the place where we want to show the QR code, place this code:

<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>


标签: php qr-code