1. 程式人生 > >php小程序生成二維碼

php小程序生成二維碼

out img ray spa peer encode family pty top

<?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小程序生成二維碼