Emily Thomas Emily Thomas - 1 year ago 36
PHP Question

php qr code not generating on server by using google api gives a file with nothing in it

i am using Google API for creating QR Codes but it gives me a file with nothing in it.

my code something like that

$file = file_get_contents('https://chart.googleapis.com/chart?cht=qr&chs=177x177&chl=Hello World');
move_uploaded_file($file, $path.$file);

i tried most of the thing but clueless
please if anyone can help me. thanks in advance.

Answer Source

Google Chart API can create QR for you. but you need some conversion and stick to the basics if you want accurate response from API

i can see you are saving the response in $file can may be a reason to save the name in db.

so why not do it like this

$path = $folder_path.$file;
//Note:  you need to give the server path; not the URL

but wait... you are creating a QR code for a String saying "Hello World", there is a blank space in the string and you are passing that string with the get method in the url so you need url encoding to encode the spaces to "%20" and so on... for correct url passing.

adding this will solve the problem

$my_qr_string = urlencode("Hello World");

another thing you are missing is character support for the QR

try using "UTF-8" character encoding to avoid any issues.

for uploading use file_put_contents() method for assured upload

in the end you got

file_put_contents($path, file_get_contents('https://chart.googleapis.com/chart?cht=qr&chs=177x177&choe=UTF-8&chl='.$my_qr_string));