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

3356053003.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

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 共1条
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片
    • 头像YGBKS0