Introduction: Today, when QR codes are widely used, automatically generating corresponding QR codes in web sites is the most basic requirement. The article introduces three ways to automatically generate QR codes using PHP.
Get method implementation method 1:
$urlToEncode="163.com";
generateQRfromGoogle($urlToEncode);
function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0')
{
$url = urlencode($url);
return ''; }
Post method implementation:
$width = 300;
$height = 300;
$string = "163.com";
function qrcode($width,$height,$string)
{
$post_data = array();
$post_data['cht'] = 'qr';
$post_data['chs'] = $width."x".$height;
$post_data['chl'] = $string;
$post_data['choe'] = "UTF-8";
$url = "http://chart.apis.google.com/chart";
$data_Array = array();
foreach($post_data as $key => $value)
{
$data_Array[] = $key.'='.$value;
}
$data = implode("&",$data_Array);
//echo $data;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
//echo ""; Note that the header is not written
return $result; }
header("Content-type:image/png");
echo qrcode($width,$height,$string);
2. Use the php class library PHP QR Code to implement
First download the class library package
Address: http://phpqrcode.sourceforge.net/
Download: http://sourceforge.net/projects/phpqrcode/
API Documentation
Detailed example
include "./phpqrcode/phpqrcode.php";
$value="http://www.weste.net";
$errorCorrectionLevel = "L";
$matrixPointSize = "4";
QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
exit;
?>