phpqr代码不是通过使用googleapi在服务器上生成的,它给出的文件中没有任何内容


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

我使用Google API创建QR码,但它给了我一个文件,其中没有任何内容。

我的代码有点像

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

我试了大部分,但毫无头绪如果有人能帮我,请帮忙。提前谢谢。

Google Chart API可以为您创建QR。但如果你想从API 得到准确的响应,你需要一些转换并坚持基本原则

我可以看到你正在将响应保存在$file中。这可能是将名称保存在db中的原因。

为什么不像这个

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

但是等等。。。你正在为一个字符串创建一个二维码,上面写着"Hello World",字符串中有一个空格,你在url中用get方法传递该字符串,所以你需要url编码来将空格编码为"%20",以此类推…以获得正确的url传递。

添加此项将解决问题

$my_qr_string = urlencode("Hello World");

你缺少的另一件事是QR 的字符支持

尝试使用"UTF-8"字符编码来避免任何问题。

上传使用file_put_contents()方法确保上传

最后你得到了

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