从Facebook Graph API获取100多个数据 [英] Get more than 100 data from Facebook Graph API

查看:112
本文介绍了从Facebook Graph API获取100多个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从Facebook Graph API获得喜欢的用户页面时,会使用此功能.在2个月前,此功能有效,并从Facebook获取了100多个数据.但是现在该功能对每个用户的点赞率低于100. Facebook API下一节,通常此功能运行良好.我该如何解决这个问题?下面是Facebook API示例的代码.我需要转到api的下一页,但不能.

I use this function when I get user page likes from Facebook Graph API. Before 2 months ago this function is working and get more than 100 data from Facebook. but now this function get only lower than 100 page likes for every user . Facebook API has a next section and normally this function is working good. How can I resolve this problem? Facebook API example below the code. I need to go next page of the api but I can't.

    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": ""
}
}

推荐答案

我找不到使用PHP的解决方案,因此为此编写了Python代码.这是下面的代码.如果其他人需要它.
您必须从终端发送两个参数才能使用此功能.这是下面的终端代码:python get_like_info.py"facebook_id""access_token"

I couldn't find the solution with PHP so I maked a Python code for that. Here is the code below. if someone else needs it.
You have to send two parameters from terminal for this function. This is the terminal code below: 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()

这篇关于从Facebook Graph API获取100多个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆