在小程序内直接请求到返回的乱码无法保存成图片,要在后台进行请求。下面给出php的后台代码,传入要生成二维码页面的路径及参数,对其进行请求即可。
<?php
//传入要生成二维码的页面路径及参数
$qpath = $_GET[‘qpath’];
//获取access_token。填入自己小程序的appid和secret
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= &secret= ";
$html = file_get_contents($url);
$back_prama = json_decode($html, true);
//通过access_token获取二维码并临时保存在服务器
$url2 = “https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$back_prama["access_token”];
$post_data = array(
“path” => $qpath,
“width” => ‘430’
);
$data_string = json_encode($post_data);
$res = http_post_data($url2, $data_string);
$result = file_put_contents(‘pic.png’,$res[1]);
function http_post_data($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json; charset=utf-8’,
'Content-Length: ’ . strlen($data_string))
);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($return_code, $return_content);
}