获取来自Facebook的图形API超过100个数据(Get more than 100 data

2019-11-04 14:54发布

我用这个功能,当我从Facebook图形API的用户页喜欢。 前2个月前这个功能工作,从Facebook获得超过100个数据。 但现在这个功能得到比100页喜欢为每个用户只有更低。 Facebook的API有下一节,通常这个功能工作良好。 我怎样才能解决这个问题呢? 下面的代码的Facebook API的例子。 我需要去API的下一个页面,但我不能。

    public function handle() {
    $fb = new Facebook([
        'app_id' => 'xxxxxx',
        'app_secret' => 'xxxxxxx',
        'default_graph_version' => 'v2.10',
    ]);

    //$fb->setDefaultAccessToken($this->accessToken);

    $likes = $fb->get("/$this->uid/likes?fields=id,name,fan_count,category,picture&limit=100000", $this->accessToken)->getGraphEdge();
    $totalLikes = array();
    if ($fb->next($likes)) {
        $likesArray = $likes->asArray();
        $totalLikes = array_merge($totalLikes, $likesArray);
        while ($likes = $fb->next($likes)) {
            $likesArray = $likes->asArray();
            $totalLikes = array_merge($totalLikes, $likesArray);
        }
    } else {
        $likesArray = $likes->asArray();
        $totalLikes = array_merge($totalLikes, $likesArray);
    }

    if (Likes::where('facebook_id', '=', $this->uid)->exists()) {
        //Session::put('facebookId', $uid);
    } else {
        foreach ($totalLikes as $totalLike) {
            $pageLike = Likes::create();
            $pageLike->facebook_id = $this->uid;
            $pageLike->page_id = $totalLike['id'];
            $pageLike->page_name = $totalLike['name'];
            $pageLike->fan_count = $totalLike['fan_count'];
            $pageLike->category = $totalLike['category'];
            $pageLike->save();
        }
        //Session::put('facebookId', $uid);
    }

} //function end -- //

   {
"data": [
],
"paging": {
"cursors": {
"before": "MTUzNTIwNjI0NzQ5MzEy",
"after": "MzQ0NTkzNzU3Mjk5"
},
"next": ""
}
}

Answer 1:

我找不到与PHP的解决方案,所以我maked一个Python代码这一点。 这是下面的代码。 如果有人需要它。
你必须从终端发送两个参数,这个功能。 这是下面的终端代码:蟒get_like_info.py“facebook_id”“的access_token”

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import urllib2
import MySQLdb
import sys
import time
import datetime

uid = sys.argv[1]
access_token = sys.argv[2]
def main(uid = uid, access_token = access_token):
    db = MySQLdb.connect('localhost', 'username', 'password', 'databasename', charset='utf8')
    cursor = db.cursor()
    picture = ""


    fb_data = get_link("https://graph.facebook.com/v2.11/{0}/likes?fields=id%2Cname%2Cfan_count%2Ccategory%2Cpicture&access_token={1}&limit=100".format(uid, access_token))
    i = 0

    query = "SELECT DISTINCT facebook_id FROM likes WHERE facebook_id = {0}".format(uid)
    a = cursor.execute(query)
    rows = cursor.fetchall()
    time2 = datetime.datetime.now()
    for key in fb_data["data"]:
        i += 1
        #print (str(i) + " " + key["name"]).encode('utf-8') 
        picture = ""
        if not rows:
            cursor.execute('''INSERT INTO `likes` (facebook_id, page_name, page_id, fan_count, category, picture,created_at,updated_at) VALUES(%s, %s, %s, %s, %s, %s, %s, %s)''', (uid, key["name"], key["id"], key["fan_count"], key["category"], picture, time2, time2))
            db.commit()
    try:
        while fb_data["paging"]["next"]:
            #print fb_data["paging"]["next"]
            fb_data = get_link("{0}".format(fb_data["paging"]["next"]))
            for key in fb_data["data"]:
                i += 1
                #print (str(i) + " " + key["name"]).encode('utf-8')
                if not rows:
                    cursor.execute('''INSERT INTO `likes` (facebook_id, page_name, page_id, fan_count, category, picture,created_at,updated_at) VALUES(%s, %s, %s, %s, %s, %s, %s, %s)''', (uid, key["name"], key["id"], key["fan_count"], key["category"], picture, time2, time2))
                    db.commit()
    except KeyError:
        print ("key error")
        sys.exit()



def get_link(link):
        fb_link = urllib2.urlopen(link)
        fb_json = fb_link.read()
        fb_data = json.loads(fb_json)
        return fb_data



main()


文章来源: Get more than 100 data from Facebook Graph API