QR 코드를 스캔하여 WeChat 공식 계정을 팔로우하고 원클릭으로 팔로우하기 위한 구현 코드

小云云
풀어 주다: 2023-03-21 15:42:02
원래의
16204명이 탐색했습니다.

이 기사에서는 모든 사람이 WeChat 공개 계정 기능을 더 잘 개발하는 데 도움이 되기를 바라며 QR 코드를 스캔하여 WeChat 공개 계정을 팔로우하고 원클릭으로 팔로우하는 구현 코드를 주로 공유합니다.

 * 获取一键关注授权标识
 * */
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 = &#39;自己公众号的appid&#39;;
$secret = &#39;自己公众号的secret &#39;;
$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 &#39;Curl error: &#39; . curl_error($ch);
}
curl_close($ch);
$access_tokens = json_decode($result, true);
//print_r($access_tokens);
$access_token = $access_tokens[&#39;access_token&#39;];
function getTemporaryQrcode($access_token, $orderId) {
    $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $access_token . "";
    $qrcode = &#39;{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": &#39; . $orderId . &#39;}}}&#39;;
    $result = api_notice_increment($url, $qrcode);
    $rs = json_decode($result, true);
    return $rs;
//    return urldecode($rs[&#39;url&#39;]);
}
$rs = getTemporaryQrcode($access_token, 1123);
//print_r($rs);
$ticket = $rs[&#39;ticket&#39;];
$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, &#39;Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)&#39;);
    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;
    }
}
?>
<p style="text-align: center;">
<p>关注素材火公众号</p>
<img src="<?php echo $qrcode; ?>" alt="关注公众号二维码" style="width:100px;height:100px;"/>
</p>
로그인 후 복사

그런 다음 변경된 코드를 인터페이스에 넣습니다

포스트 메소드

class Wxfollow
{
    protected $appid = &#39;wxf1d959b99f33b156&#39;;
    protected $secret = &#39;248f3a560604555ec96215c085cb2723&#39;;
    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[&#39;access_token&#39;];

    }
    public function Follow(){
        //非必传项
        $rs = $this->getTemporaryQrcode($this->access_tokens, 123);
        $ticket = $rs[&#39;ticket&#39;];
        $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 = &#39;{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": &#39; . $orderId . &#39;}}}&#39;;
        $momo = json_decode($qrcode, true);

        $result = curl_post($url, $momo);
        $rs = json_decode($result, true);
        return $rs;
    }
로그인 후 복사

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(
            &#39;Content-Type: application/json&#39;
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}
로그인 후 복사

관련 권장 사항:

PHP를 사용하여 사용자가 WeChat 공개 계정을 팔로우하는지 확인합니다

WeChat ID 코드 QR 코드 사진을 기반으로 WeChat 공개 계정을 자동으로 생성하고 팔로우하는 방법

PHP 배경에서 WeChat 공개 계정 개발 예

위 내용은 QR 코드를 스캔하여 WeChat 공식 계정을 팔로우하고 원클릭으로 팔로우하기 위한 구현 코드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!