Implementation code for scanning QR code to follow and one-click following WeChat official account

小云云
Release: 2023-03-21 15:42:02
Original
16174 people have browsed it

This article mainly shares with you the implementation code of scanning the QR code to follow and one-click following the WeChat public account, hoping to help everyone better develop the WeChat public account function.

* 获取一键关注授权标识 * */ 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); }
Copy after login

php custom interception of the middle part of the string method, used above, post it!

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

WeChat public account scan code to follow the code

First go to the WeChat public account before and after Scan the code to focus on the code that is not separated

 

关注素材火公众号

关注公众号二维码

Copy after login

and then put the code changed into the interface

post method

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; }
Copy after login

curl encapsulation class

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); }
Copy after login

Related recommendations :

Use php to determine whether the user follows the WeChat public account

How to automatically generate a QR code image for following the WeChat public account based on the WeChat id code

PHP background development WeChat public account example

The above is the detailed content of Implementation code for scanning QR code to follow and one-click following WeChat official account. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn