Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

php批量获取关注信息

作者:鱼宝宝睡觉觉   发布日期:2025-12-17   浏览:523

要批量获取关注信息,你需要使用微信公众平台的开发接口。以下是一个使用PHP编写的示例代码,用于获取关注用户的信息:

<?php
// 公众号的AppID和AppSecret
$appId = 'your_app_id';
$appSecret = 'your_app_secret';

// 获取access_token
$accessToken = getAccessToken($appId, $appSecret);

// 获取关注用户的openid列表
$openIds = getOpenIds($accessToken);

// 批量获取关注用户的信息
$userInfoList = getUserInfoList($accessToken, $openIds);

// 输出用户信息
foreach ($userInfoList as $userInfo) {
    echo 'OpenID: ' . $userInfo['openid'] . '<br>';
    echo 'Nickname: ' . $userInfo['nickname'] . '<br>';
    echo 'Sex: ' . $userInfo['sex'] . '<br>';
    echo 'Country: ' . $userInfo['country'] . '<br>';
    echo 'Province: ' . $userInfo['province'] . '<br>';
    echo 'City: ' . $userInfo['city'] . '<br>';
    echo 'SubscribeTime: ' . $userInfo['subscribe_time'] . '<br>';
    echo '<br>';
}

// 获取access_token
function getAccessToken($appId, $appSecret) {
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appId . '&secret=' . $appSecret;
    $response = file_get_contents($url);
    $responseData = json_decode($response, true);
    return $responseData['access_token'];
}

// 获取关注用户的openid列表
function getOpenIds($accessToken) {
    $url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=' . $accessToken;
    $response = file_get_contents($url);
    $responseData = json_decode($response, true);
    return $responseData['data']['openid'];
}

// 批量获取关注用户的信息
function getUserInfoList($accessToken, $openIds) {
    $url = 'https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=' . $accessToken;
    $postData = array();
    foreach ($openIds as $openId) {
        $postData[] = array(
            'openid' => $openId,
            'lang' => 'zh_CN'
        );
    }
    $postDataJson = json_encode(array('user_list' => $postData));
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-Type: application/json',
            'content' => $postDataJson
        )
    );
    $context = stream_context_create($options);
    $response = file_get_contents($url, false, $context);
    $responseData = json_decode($response, true);
    return $responseData['user_info_list'];
}
?>

请注意,上述代码中的your_app_idyour_app_secret需要替换为你自己的AppID和AppSecret。此外,这里使用了PHP的file_get_contents函数来发送HTTP请求,确保你的服务器已经开启了该函数。如果你的服务器禁用了该函数,你可以使用其他的HTTP请求库来替代。

上一篇:php圆形头像代码

下一篇:php 判断时间是否昨天

大家都在看

php session用法

php 定义常量

phpisset函数

php html转图片

php后端

php爬虫框架

php读取csv文件

php+mysql动态网站开发

phpmyadmin docker

php session id

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站