php 生成带参数微信二维码(场景码)

作者: wxfeng 分类: php 发布时间: 2023-02-23 11:13    阅读 1,254 次
<?php

namespace app\index\controller;

class Qrcode
{
    public function create()
    {
        $accessInfo = $this->ch("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=********&secret=*****");
//        var_dump($accessInfo);exit;

        //永久二维码
        $accessToken = $accessInfo['access_token'];
        $url         = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $accessToken;
        $postArr     = [
            "action_name" => "QR_LIMIT_SCENE",
            "action_info" => [
                'scene' => ['scene_str' => "aaa"],
            ],
        ];
        $postJson    = json_encode($postArr);

        $res = $this->ch($url, 'post', 'json', $postJson);

        $ticket = $res['ticket'];
//        exit($ticket);
        $url    = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . urlencode($ticket);

        echo "<img src='" . $url . "'>";

    }

    //url请求ticket
    public function ch($url, $type='post', $res='json', $arr="")
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        if ($type == 'post') {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
        }

        $cnt = curl_exec($ch);
        if (curl_errno($ch)) {
            return;
        }

        curl_close($ch);

        if ($res == 'json') {
            return json_decode($cnt, true);
        }

        return $cnt;
    }
}


如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

2条评论

发表评论

您的电子邮箱地址不会被公开。