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 = "APPID"; //APPID
$WXsecret = "secret"; //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;
}