从Facebook Graph API获取100多个数据

时间:2018-03-07 20:15:48

标签: php facebook api facebook-graph-api facebook-php-sdk

当我从Facebook Graph 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": ""
}
}

1 个答案:

答案 0 :(得分:1)

我无法用PHP找到解决方案,所以我为此制作了一个Python代码。这是下面的代码。如果有人需要它。
您必须从终端发送此功能的两个参数。这是下面的终端代码:python get_like_info.py" facebook_id" "&的access_token#34;

#!/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()