使用订阅号的请用测试号的APPID和APPSECRET,因为订阅号没有接口权限。
一、获取到access_token值
$assecc_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$tokenJson=file_get_contents($assecc_url);
$token=json_decode($tokenJson,true); 二、根据我们所获取到的token值去拿到ticket值 选择是生成永久性的二维码还是临时的
1.临时二维码
http请求方式: POST
URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式:json
POST数据例子:{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
2.永久二维码
http请求方式: POST
URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式:json
POST数据例子:{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}
或者也可以使用以下POST数据创建字符串形式的二维码参数:
{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "123"}}}
$url="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$token;
$dataJson=$this->http_curl_post($url,$json);
$arr=json_decode($dataJson,true);
return $arr['ticket'];
在这里是我自己封装http post 请求类
public function http_curl_post($url,$data){
$ch = curl_init(); //1.初始化
curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");//3.请求方式
//4.参数如下
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0");//模拟浏览器
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: '.strlen($data))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);//6.执行
if (curl_errno($ch)) {//7.如果出错
return curl_error($ch);
}
curl_close($ch);//8.关闭
return $tmpInfo;
}三、获取二维码ticket后,开发者可用ticket换取二维码图片。请注意,本接口无须登录态即可调用。
$imgPath="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".UrlEncode($ticket);
echo '<img src="'.$imgPath.'" alt="">';
2674

被折叠的 条评论
为什么被折叠?



