List the most tanned posts

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
                }
            }
        }]
    }
}

In this way he brings me the amount of tanned per post. How to list the most tanned posts?

    
asked by anonymous 01.08.2016 / 21:26

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 < = > is only available in PHP 7!

This will sort the array ($ return) from the largest number of tanned to the smallest, as desired.

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 see this in the PHP Sandbox.

  

I've edited some API returns, to hide some data (like the ones I liked, my publications id ...), but I've preserved all indexes.

    
02.08.2016 / 00:20