Introduction: In the two-dimensional code widely used today, in the Web site automatically generate the corresponding QR code is the most basic requirements. This article describes three ways to automatically generate two-dimensional code using PHP.
Get method Implementation method one:
$urlToEncode = "163.com";
Generateqrfromgoogle ($urlToEncode);
function Generateqrfromgoogle ($chl, $widhtHeight = ' Max ', $EC _level= ' L ', $margin = ' 0 ') {
$url = UrlEncode ($url );
return ' chld= '. $EC _level. ' | '. $margin. ' &chl= '. $chl. ' "alt=" QR Code "widhtheight=" '. $size. ' widhtheight= ' '. $size. ' /> ';
}
How to implement the Post method:
$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 you do not write the header
return $result;
}
Header ("Content-type:image/png");
Echo QRCode ($width, $height, $string);
2. Use PHP library PHP QR code to achieve
First download the Class library package
Address: http://phpqrcode.sourceforge.net/
Download: http://sourceforge.net/projects/phpqrcode/
API documentation
A detailed example
Include "./phpqrcode/phpqrcode.php";
$value = "Http://www.weste.net";
$errorCorrectionLevel = "L";
$matrixPointSize = "4";
QRCode::p ng ($value, False, $errorCorrectionLevel, $matrixPointSize);
Exit
?>
http://www.bkjia.com/PHPjc/735051.html www.bkjia.com true http://www.bkjia.com/PHPjc/735051.html techarticle Introduction : In the two-dimensional code widely used today, in the Web site automatically generate the corresponding QR code is the most basic requirements. This article describes three ways to automatically generate two-dimensional code using PHP. ...