直接上代码
<?php
$APPID = '微信平台的APPID';
$APPSECRET = '和APPID相匹配的APPSECRET';
//生成access_token
$url= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;
$access_token=file_get_contents($url);
$access_token = json_decode($access_token,true);
$access_token =$access_token['access_token'];
//调用微信生成小程序码API
$new_url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token;
$scene = uniqid();
$data = [
'scene' => '', //地址带的参数
'page' =>'page/index/index', //扫描小程序码跳转地址
'is_hyaline' => 'true' //小程序码背景设置透明
];
//传入的scene为json格式
$scene = json_encode($data);
function httpRequest($url, $data='', $method='GET'){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if($method=='POST')
{
curl_setopt($curl, CURLOPT_POST, 1);
if ($data != '')
{
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$result = httpRequest( $new_url, $scene,"POST");
file_put_contents("qrcode.png", $result);
$base64_image ="data:image/jpeg;base64,".base64_encode( $result );
var_dump($base64_image);exit;
function upFile($file){
if(!empty($file)){
if(!preg_match('/^(data:\s*image\/(\w+);base64,)/', $file, $result)){
throw new ParameterException([
'msg' => '编码错误'
]);
}
$type = $result[2];
if (strstr($file,",")){
$image = explode(',',$file);
$file = $image[1];
}
$file_name = date('YmdHis',time()).mt_rand(1000,9999) . '.' . $type ;
$file_path = './upload/'. date("Y") .'/'. date("m") . '/' .date('d');
$imageSrc= $file_path."/". $file_name;
if(!is_dir($file_path)){
mkdir($file_path,0777,true);
}
file_put_contents($imageSrc, base64_decode($file));
$new_path = '域名'.substr($file_path,1) .'/'. $file_name;
return $new_path;
}
}
var_dump(upFile($base64_image));exit;