PHP生成微信小程序码

直接上代码 

<?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;


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

❀想容

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值