php微信公众号开发之生成带参数的二维码

 参考微信公众平台:https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html

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

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

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

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

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

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

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

 //url请求ticket
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;
}

 

posted on 2020-10-22 15:30  Lea。  阅读(403)  评论(0)    收藏  举报