douluo3256 2018-03-07 20:15
浏览 119
已采纳

从Facebook Graph API获取100多个数据

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": ""
}
}
  • 写回答

1条回答 默认 最新

  • doulin8374 2018-03-08 08:02
    关注

    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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决