php获取小程序码,适用于需要的码数量极多的业务场景。
注:必须是发布的小程序才可以!
请求地址
POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
请求参数
1 access_token string 是 接口调用凭证
2 scene string 是 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&’()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
page string 主页 否 必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
3 width number 430 否 二维码的宽度,单位 px,最小 280px,最大 1280px
错误码
45009 调用分钟频率受限(目前5000次/分钟,会调整),如需大量小程序码,建议预生成。
41030 所传page页面不存在,或者小程序没有发布
40001 access_token过期了
第一步 获取 access_token
https://developers.weixin.qq.com/miniprogram/dev/api/getAccessToken.html
第二步 发送CURL请求 post
url: https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN
protected function https_request( $url, $data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
第三步 根据返回存储图片
public function SaveImage($res){
$filename="goods_".time().mt_rand(1000,9999).".png";///要生成的图片名字
$xmlstr = $res;
$file = fopen("./".$imgDir.$filename,"w");//打开文件准备写入
fwrite($file,$xmlstr);//写入
fclose($file);//关闭
$filePath = './'.$imgDir.$filename;
$_filePath = '/'.$imgDir.$filename;
//图片是否存在
if(!file_exists($filePath))
{
return 'createFail';
exit();
}
return $_filePath;
}