php 生成小程序二维码

<?php

//刚开始我调试的时候报各种错误,注意data的数据格式,是拼接成一个json而不是直接的数组。另外账号密码都是我写的测试的,掉用的时候输入成正式的即可

$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx96jjkad81a880c36&secret=dff3ab41iuii43cb109ef9c6469e';
$arr = file_get_contents($url);
$a = json_decode($arr,true);
$access_token = $a['access_token'];

$url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
$path="pages/mine/mine/mine?query=1";
$width=430;
$data='{"path":"'.$path.'","width":'.$width.'}';
$return = request_post($url,$data);


//保存在根目录一个文件
file_put_contents('./'.time().'.jpg',$return);
function request_post($url, $data){
$ch = curl_init();
$header = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return false;
}else{
return $tmpInfo;
}
}

转载于:https://www.cnblogs.com/ayanboke/p/9372401.html

生成小程序二维码可以通过微信官方提供的API实现,而PHP可以通过调用这个API来生成小程序二维码。具体步骤如下: 1. 获取access_token:调用微信官方提供的获取access_token的API,获取access_token。注意:access_token需要缓存,不要频繁调用该API获取。 2. 调用生成小程序二维码的API:调用微信官方提供的生成小程序二维码的API,传入必要的参数,比如path、width等。 3. 保存小程序二维码:把API返回的二进制流保存为图片即可。 下面是示例代码: ```php <?php $app_id = '你的小程序appid'; $app_secret = '你的小程序appsecret'; //获取access_token $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$app_id}&secret={$app_secret}"; $res = file_get_contents($url); $res = json_decode($res, true); $access_token = $res['access_token']; //调用生成小程序二维码的API $path = '/pages/index/index'; $width = 430; $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$access_token}"; $data = array( 'path' => $path, 'width' => $width ); $data = json_encode($data); $options = array( 'http' => array( 'header' => "Content-type:application/json", 'method' => 'POST', 'content' => $data, 'timeout' => 60 ) ); $context = stream_context_create($options); $res = file_get_contents($url, false, $context); //保存小程序二维码 file_put_contents('./qrcode.jpg', $res); ``` 需要注意的是,生成小程序二维码的API有一些限制,比如只能生成已发布的小程序二维码有效期为30天等。具体详情可以参考微信官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值