调用bilibiliAPI获取B站账户粉丝数

Root
2022-05-12 / 1 评论 / 182 阅读 / 正在检测是否收录...

l4cjp9qt.png

B站的粉丝人数相关的api请求地址为:
https://api.bilibili.com/x/relation/stat?vmid=你的UID号
访问这个地址后返回一串json代码,这里仍以我的为例,如下:

{"code":0,"message":"0","ttl":1,"data":{"mid":35199034,"following":13,"whisper":0,"black":0,"follower":516}}

解析格式化后如下:

{
    "code": 0,
    "message": "0",
    "ttl": 1,
    "data": {
        "mid": 35199034,
        "following": 13,
        "whisper": 0,
        "black": 0,
        "follower": 516
    }
}

我们想要的数据为data中的follower值,也就是粉丝数,接下来开始写统计代码。
首先用file_get_contents()函数,file_get_contents() 函数把可以整个文件读入一个字符串中,先将api返回的值用file_get_contents()函数赋给变量data:

$data = file_get_contents("https://api.bilibili.com/x/relation/stat?vmid=419312164");

随后通过json_decode()函数对json值解码并将结果赋给arr变量:
$arr = json_decode($data,true);

这样就得到了数据返回后的值,因为我们需要的是data中follower的值(也就是粉丝数,翻译应该是 追随者),所以最后只要在任意位置插入以下代码就能获取B站的粉丝量了:

<?php echo($arr["data"]["follower"]);?>

我已经弄好API了,可以直接进行调用 地址如下:
https://www.cloudapi.vip/doc/bilibili_follower.html

本文共 211 个字数,平均阅读时长 ≈ 1分钟
5

打赏

海报

正在生成.....

评论 (1)

取消
  1. 头像
    YGBKS
    ·Windows 10 · Google Chrome
    沙发

    不错不错表情

    回复 删除 垃圾