微信公众号扫码关注和一键关注公众号的方法

一键关注公众号的方法自己看百度链接https://jingyan.baidu.com/article/e75057f2f5ecf9ebc91a89f3.html

一键关注公众号的代码,原来通过获取

获取自定义菜单配置接口取出当前公众号的唯一标识__biz;(看到网上都没有这方面的资料,大家记得点关注或赞,谢谢)

/*
 * 获取一键关注授权标识
 * */
public function getIdentification()
{
    $burl = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" . $this->access_tokens . "";
    $result = curl_get($burl);
    preg_match('/__biz.*&mid/', $result, $matches);//正则截取字符串
    $sVid = $this->get_between($matches[0], "__biz=", "==&mid");//截取出微信公众号唯一标识
    $okurl="https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=".$sVid."==&scene=124#wechat_redirect";
    jumpUrl($okurl);

}

 

php自定义截取中间部分字符串方法,上面用到了,贴出来吧!

/*
 * php截取指定两个字符之间字符串
 * */
function get_between($input, $start, $end)
{
    $substr = substr($input, strlen($start) + strpos($input, $start), (strlen($input) - strpos($input, $end)) * (-1));

    return $substr;

}


微信公众号扫码关注代码

先上前后微信公众号扫码关注端不分离的代码

<?php
header("Content-type: text/html; charset=utf-8");
//http://pay.sucaihuo.com/project/access_token
//php获取微信access_token,appid和app_secret得到微信access_token
//php根据appid和secret获取微信access_token,php通过curl远程获取微信access_token信息
$appid = '自己公众号的appid';
$secret = '自己公众号的secret ';


$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret . "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if($result == false)
{
    echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
$access_tokens = json_decode($result, true);


//print_r($access_tokens);
$access_token = $access_tokens['access_token'];


function getTemporaryQrcode($access_token, $orderId) {


    $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $access_token . "";
    $qrcode = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": ' . $orderId . '}}}';
    $result = api_notice_increment($url, $qrcode);
    $rs = json_decode($result, true);
    return $rs;
//    return urldecode($rs['url']);
}


$rs = getTemporaryQrcode($access_token, 1123);
//print_r($rs);
$ticket = $rs['ticket'];
$qrcode = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . $ticket . "";


//print_r($qrcode);
function api_notice_increment($url, $data) {
    $ch = curl_init();
//    $header = "Content-type: text/xml";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);
    if (curl_errno($ch)) {
        curl_close($ch);
        return $ch;
    } else {
        curl_close($ch);
        return $tmpInfo;
    }
}
?>
<div style="text-align: center;">
<p>关注素材火公众号</p>
<img src="<?php echo $qrcode; ?>" alt="关注公众号二维码" style="width:100px;height:100px;"/>
</div>

 

再放改成接口的代码

post方式

 

class Wxfollow
{
    protected $appid = 'wxf1d959b99f33b156';
    protected $secret = '248f3a560604555ec96215c085cb2723';
    protected $url = "";
    protected $access_tokens = "";


   public function __construct()
    {
        //获取$access_token
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->secret . "";
        $result = curl_post($url);
        $access_tokens = json_decode($result, true);
        $this->access_tokens = $access_tokens['access_token'];

    }
    public function Follow(){
        //非必传项
        $rs = $this->getTemporaryQrcode($this->access_tokens, 123);
        $ticket = $rs['ticket'];
        $qrcode = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . $ticket . "";
        ///打印二维码显示
        jumpUrl($qrcode);
    }

//生成二维码
    public function getTemporaryQrcode($access_tokens,$orderId)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" .$access_tokens . "";
//生成二维码需要的参数
        $qrcode = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": ' . $orderId . '}}}';
        $momo = json_decode($qrcode, true);

        $result = curl_post($url, $momo);
        $rs = json_decode($result, true);
        return $rs;
    }

curl封装类

 

 

function curl_post($url, array $params = array())
{
    $data_string = json_encode($params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER,
        array(
            'Content-Type: application/json'
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}

 

 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Flask是一种轻量级的Python Web框架,能够方便地构建网页应用程序。要实现微信公众号扫码关注并登录网页功能,可以使用Flask结合微信开发接口进行实现。 首先,需要在微信公众平台上注册并创建一个公众号,并获取到相关的开发者ID和密钥。 接下来,搭建Flask应用程序,先安装Flask库,并导入相关的依赖库,如wechatpy和requests库等。 然后,创建一个Flask路由,用于接收微信服务器的验证请求和菜单跳转等请求。根据微信开发文档,编写逻辑代码,进行验证和处理微信服务器的各种请求。 在处理菜单跳转请求时,可以通过微信JS-SDK提供的扫一扫功能,生成一个特定的二维码,用于用户关注公众号。二维码中包含一个唯一的标识码,用于识别用户和公众号之间的关系。 当用户扫描二维码关注公众号后,微信服务器会向事先设置的回调URL发送消息通知。在Flask应用程序中,可以通过编写对应的路由来处理该通知,获取到用户的唯一标识码。然后,可以将该标识码与用户相关的信息存储到数据库中,以便后续使用。 最后,通过编写相关的路由和模板,实现用户登录网页功能。当用户点击网页中的登录按钮时,可以跳转到微信授权登录页面。用户授权后,微信会将用户的唯一标识码和相关信息返回到事先设置的回调URL。在Flask应用程序中,处理该回调URL的路由中,可以获取到用户的标识码,从数据库中获取用户信息,并进行登录操作。 综上所述,通过使用Flask框架结合微信开发接口,可以实现微信公众号扫码关注并登录网页功能。这样的实现方式能够方便地与微信公众号进行交互,并提供给用户一个方便、安全的登录方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值