微信小程序和PHP生成二维码 太阳码

小程序注册上线后填入appid

<?php
namespace app\index\controller;

class Index
{
    
    
    public function pathImg()
    {
    
     
        $goods_id = '7'; //商品id
        //配置APPID、APPSECRET
        $APPID = "填入你的appid";
        $APPSECRET =  "秘钥";
        //获取access_token
        $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";
        $json = $this->httpRequest($access_token);
        $json = json_decode($json, true);
        $ACCESS_TOKEN = $json['access_token'];
        //如果要获取小程序码,请求这个接口
        $qcode ="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$ACCESS_TOKEN";
        $param = json_encode(array("page"=>"pages/index/index","scene"=>$goods_id));
        //如果要获取二维码,请求这个接口
        // $qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN";
        // $param = json_encode(array("path"=>"pages/comm_details/comm_details?goods_id=$goods_id","width"=> 150));
        //POST参数
        $result = $this->httpRequest($qcode, $param, "POST");
        //生成二维码
        file_put_contents("SP7.png", $result);
        //qrcode.png这个就是你生成的二维码图片,可以存到你指定的路径,例如:/update/img/qrcode.png
        $base64_image ="data:image/jpeg;base64,".base64_encode($result);
        echo $base64_image;
    }
    
    //curl请求
    public 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;
    }
}

使用postman请求PHP接口
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45557228/article/details/114968511