php 使用 google 无法在服务器上生成二维码 api 给出了一个没有任何内容的文件
php qr code not generating on server by using google api gives a file with nothing in it
我正在使用 Google API 创建二维码,但它给了我一个没有任何内容的文件。
我的代码是这样的
$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 可以为您创建二维码。但是如果你想得到 API
的准确回复,你需要一些转换并坚持基本原则
我可以看到您正在将响应保存在 $file 中,这可能是将名称保存在 db 中的原因。
所以为什么不这样做呢
$path = $folder_path.$file;
//Note: you need to give the server path; not the URL
但是等等...您正在为字符串 "Hello World" 创建 QR 码,字符串中有一个空白 space 并且您正在使用 get 方法传递该字符串url 所以你需要 url 编码来将 space 编码为 "%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));
我正在使用 Google API 创建二维码,但它给了我一个没有任何内容的文件。
我的代码是这样的
$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 可以为您创建二维码。但是如果你想得到 API
的准确回复,你需要一些转换并坚持基本原则我可以看到您正在将响应保存在 $file 中,这可能是将名称保存在 db 中的原因。
所以为什么不这样做呢
$path = $folder_path.$file;
//Note: you need to give the server path; not the URL
但是等等...您正在为字符串 "Hello World" 创建 QR 码,字符串中有一个空白 space 并且您正在使用 get 方法传递该字符串url 所以你需要 url 编码来将 space 编码为 "%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));