生成微信公眾號二維碼(用戶掃碼關注公眾號)


1、token 文件

/**
 * https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=11111&lang=zh_CN
 * wechat php test
 *  驗證回調地址   token
 *  公眾號--> 開發 ---> 服務器設置---->服務驗證token
 */

//服務器地址(URL)
//http://域名/wx/wx_token.php

//定義TOKEN密鑰
define("TOKEN","loginzf");

$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

 

2、 配置文件 conf.php

header("Content-type: text/html; charset=utf-8");
// 開發者ID(AppID)
$appid="";
//開發者密碼(AppSecret)
$appsecret="";
// 公眾號--->開發----> 接口權限---> 網頁服務---> 網頁授權 ---> 修改 【按照說明操作】
// 域名/wx   服務器 網頁授權域名 不加 http:// 2處域名要一致
//https://mp.weixin.qq.com/cgi-bin/settingpage?t=setting/function&action=function&token=11111&lang=zh_CN
$re_url=urlencode("http://域名/wx/wx_callback.php");
//
$state=123; // 自定義標識
$scope="snsapi_userinfo"; //應用授權作用域
//$scope="snsapi_base"; // 不彈出授權頁面,直接跳轉,只能獲取用戶openid
include "curl.php"; //https://www.cnblogs.com/xuey/p/8461622.html 引入curl 函數

 

3、獲取開發者的 access_token.php

 // https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183

 include "conf.php";

  // 獲取 access_token
  if(isset($_COOKIE['access_token']))
  {
      $access_token=$_COOKIE['access_token'];
  }
  else
  {
      $token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
      $json_return=curl_url($token_url);
      $json_info = json_decode($json_return, true);
      $access_token =$json_info["access_token"];
      setcookie('access_token',$access_token,86400,'/'); //此步驟可以省略,也可自定義存儲 時間
 }

 

 

4、生成公眾號二維碼 wx_img.php

//生成帶參數的二維碼 ---- 掃碼進入進入公眾號
// https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443433542

function wx_img()
{
    include "access_token.php";

    $qrcode = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 10000}}}';

    $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
    $result =https_post($url,$qrcode);
    $jsoninfo = json_decode($result, true);
    $ticket = $jsoninfo["ticket"];

    $url_ticket="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=$ticket";
    echo  "<img src=".$url_ticket.">";
}
wx_img();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM