List the most liked posts

Asked

Viewed 139 times

1

Using the Graph API Explorer with the call below:

 me?fields=posts{likes.summary(true).filter(stream)}

I get my posts, who liked and a summary containing the total:

{
    "posts": {
        "data": [{
            "id": "xxxxxxxxxxxxxxxxxxx",
            "likes": {
                "data": [{
                    "id": "xxxxxxxxxxxxxxxxxxx",
                    "name": "xxxxxxxxxxxxxxxxxxx"
                }],
                "paging": {
                    "cursors": {
                        "before": "...",
                        "after": "..."
                    },
                    "next": "..."
                },
                "summary": {
                    "total_count": 35,
                    "can_like": true,
                    "has_liked": false
                }
            }
        }, {
            "id": "zzzzzzzzzzzzzz",
            "likes": {
                "data": [{
                    "id": "zzzzzzzzzzzzzz",
                    "name": "zzzzzzzzzzzzzz"
                }],
                "paging": {
                    "cursors": {
                        "before": "...",
                        "after": "..."
                    },
                    "next": "..."
                },
                "summary": {
                    "total_count": 30,
                    "can_like": true,
                    "has_liked": false
                }
            }
        }]
    }
}

This way he brings me the amount of likes per post. How to list the posts with more likes?

1 answer

2


The API does not provide this, as far as I know.

You should treat this in your application, you can use:

$retorno = json_decode($curl, true);

uasort($retorno["posts"]["data"], function($a, $b) {
    return $b['likes']['summary']['total_count'] <=> $a['likes']['summary']['total_count'];
});

The <=> operator is only available in PHP 7!

This will sort the array ($return) from the largest number of likes to the smallest, as you wish.

Demonstration:

For a better demonstration, see here:

PHP:

<?php

$json = json_decode('{
  "posts": {
    "data": [
      {
        "id": "a",
        "likes": {
          "summary": {
            "total_count": 4,
            "can_like": true,
            "has_liked": false
          }
        }
      },
      {
        "id": "b",
        "likes": {
          "summary": {
            "total_count": 1,
            "can_like": true,
            "has_liked": false
          }
        }
      },
      {
        "id": "c",
        "likes": {
          "summary": {
            "total_count": 3,
            "can_like": true,
            "has_liked": false
          }
        }
      },
      {
        "id": "d",
                "likes": {
          "summary": {
            "total_count": 0,
            "can_like": true,
            "has_liked": false
          }
        }
      },
      {
        "id": "e",
        "likes": {
          "summary": {
            "total_count": 1,
            "can_like": true,
            "has_liked": false
          }
        }
      },
      {
        "id": "f",
        "likes": {
          "summary": {
            "total_count": 4,
            "can_like": true,
            "has_liked": false
          }
        }
      },
      {
        "id": "g",
        "likes": {
          "summary": {
            "total_count": 5,
            "can_like": true,
            "has_liked": false
          }
        }
      }
    ],
    "paging": {
      "previous": "",
      "next": ""
    }
  },
  "id": ""
}', true);



uasort($json["posts"]["data"], function($a, $b) {
    return $b['likes']['summary']['total_count'] <=> $a['likes']['summary']['total_count'];
});


foreach($json["posts"]["data"] as $postagens){

    echo 'Postagem: '.$postagens['id'].' contem '.$postagens['likes']['summary']['total_count'].' curtidas';
    echo '<br>';

}

Answer:

Postagem: g contem 5 curtidas
Postagem: a contem 4 curtidas
Postagem: f contem 4 curtidas
Postagem: c contem 3 curtidas
Postagem: b contem 1 curtidas
Postagem: e contem 1 curtidas
Postagem: d contem 0 curtidas

If you prefer to see this in PHP Sandbox.

I edited some API returns to hide some data (like who liked it, my posts id...), but I preserved all the indexes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.