php小程序生成二维码

Posted 码蚁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php小程序生成二维码相关的知识,希望对你有一定的参考价值。

<?php

getwxacode();

//生成二维码
function getwxacode(){
$url = "https://api.weixin.qq.com/wxa/getwxacode?";
$url .= "access_token=" . getToken();
$postdata = [
"path" => "pages/index/index?clientId=3",
"width" => 430,
];

$res = curl_post($url,json_encode($postdata),$options=array());
$img = ‘./img/‘.time().‘.jpg‘;
$r = file_put_contents($img,$res);
echo "<img src=‘".$img."‘>";

}


//发送获取token请求,获取token(2小时)
function getToken() {
$url = getTokenUrlStr();
$res = curl_post($url,$postdata=‘‘,$options=array());

$data = json_decode($res,JSON_FORCE_OBJECT);
return $data[‘access_token‘];
}

//获取token的url参数拼接
function getTokenUrlStr()
{
$getTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?"; //获取token的url
$WXappid = "wx0399845015067c51"; //APPID
$WXsecret = "7d7205f85b79805006ecfa2121ef4272"; //secret
$str = $getTokenUrl;
$str .= "grant_type=client_credential&";
$str .= "appid=" . $WXappid . "&";
$str .= "secret=" . $WXsecret;

return $str;
}

function curl_post($url=‘‘,$postdata=‘‘,$options=array()){
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if(!empty($options)){
curl_setopt_array($ch, $options);
}
$data=curl_exec($ch);
curl_close($ch);
return $data;
}

function curl_get($url=‘‘,$options=array()){
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,5);
if(!empty($options)){
curl_setopt_array($ch,$options);
}
$data=curl_exec($ch);
curl_close($ch);
return $data;
}









































































以上是关于php小程序生成二维码的主要内容,如果未能解决你的问题,请参考以下文章

php小程序生成二维码

PHP生成小程序二维码合成图片生成文字

小程序参数二维码生成

thinkPHP5 生成微信小程序二维码 保存在本地

PHP获取小程序码并返回前端显示图片

微购小程序怎么生成二维码