生成微信公众号二维码

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();

猜你喜欢

转载自www.cnblogs.com/xuey/p/9346370.html