Retrieving likes of Facebook Posts in php - php

I am looking for a way to retrieve the number of likes of a Facebook post, based on its post_id. I got hold of a php code from this forum itself..and it's something like
<?php
function fetchUrl($url){
return file_get_contents($url);
}
$json_object = fetchUrl("https://graph.facebook.com/{post_id}/likes?access_token={token}&limit=5000000"); //
$feedarray = json_decode($json_object, true);
$likesNum = count($feedarray['data']); // return the number of items in `data` array
print $likesNum;
?>
But the problem is this method does not retrieve the total likes of a post, since likes are displayed in blocks of 1000, after which theres another link to a different page containing the next set of 1000 likes and so on.
Is there a way to get the total number of likes of a facebook post by a single query?

Add the summary flag ** summary=true**
"https://graph.facebook.com/{post_id}/likes?access_token={token}&summary=true"
{
"data": [
{
"id": "663342380429664",
"name": "Luis Mendoza"
},
{
"id": "1532100840406448",
"name": "Sakazuki Akainu"
},
{
"id": "780666205412913",
"name": "Joaito KoRn"
},
{
"id": "1060933433919925",
"name": "Adrian Sosa"
},
{
"id": "860704407276452",
"name": "Sarah Rosenstrauch"
},
{
"id": "1947974762009431",
"name": "David Prieto"
},
{
"id": "804864302928112",
"name": "Ronal Ortega"
},
{
"id": "1505075359814934",
"name": "Gonzalo Larzen"
},
{
"id": "1431207613804483",
"name": "Victor Clc"
},
{
"id": "508785009283633",
"name": "Rxdry EzDe Cerrx Mcmxii"
},
{
"id": "435355413265946",
"name": "Ángel Fernando Huillca Alonso"
},
{
"id": "163773913961445",
"name": "Pelado Miguel Pin Macias"
},
{
"id": "1479227465674392",
"name": "Releck Solitario"
},
{
"id": "161610054193539",
"name": "MD Sahin MD Sahin"
},
{
"id": "798431050242097",
"name": "Brian Nahuel"
},
{
"id": "624869574305480",
"name": "Saul Alfredo"
},
{
"id": "1642733362665392",
"name": "Junior Zurita"
},
{
"id": "134907406871404",
"name": "Wil Peña"
},
{
"id": "10153052770952668",
"name": "Miguel Peña Cáceres"
},
{
"id": "1461494580846182",
"name": "Darian Suarez"
},
{
"id": "365762500250317",
"name": "Igarashi Ganta"
},
{
"id": "750032685093387",
"name": "Camila Barbé"
},
{
"id": "781013541941152",
"name": "Gonzalo Nievas"
},
{
"id": "756520927743339",
"name": "Jonathan C. Duran Cuellar"
},
{
"id": "1504488093199860",
"name": "Maxi Russo"
}
],
"paging": {
"cursors": {
"before": "NjYzMzQyMzgwNDI5NjY0",
"after": "MTUwNDQ4ODA5MzE5OTg2MAZDZD"
},
"next": "https://graph.facebook.com/v2.3/1009501939072385/likes?access_token=TOKEN..."
},
"summary": {
"total_count": 4303
}
}

Related

Extract element from nested JSON

There is json response:
{
"id": "1234567890123456789",
"creation_date": 12345678,
"event": "WAITING_PAYMENT",
"version": "2.0.0",
"data": {
"product": {
"id": 213344,
"name": "Product Name",
"has_co_production": false
},
"affiliates": [
{
"name": "Affiliate name"
}
],
"buyer": {
"email": "buyer#email.com"
},
"producer": {
"name": "Producer Name"
},
"commissions": [
{
"value": 0.65,
"source": "MARKETPLACE"
},
{
"value": 3.10,
"source": "PRODUCER"
}
],
"purchase": {
"approved_date": 1231241434453,
"full_price": {
"value": 134.0
},
"original_offer_price": {
"currency_value": "EUR"
"value": 100.78,
},
"price": {
"value": 150.6
},
"order_date": "123243546",
"status": "STARTED",
"transaction": "HP02316330308193",
"payment": {
"billet_barcode": "03399.33335 33823.303087 198801027 2 876300015000",
"billet_url": "https://billet-link.com/bHP023163303193",
}
},
"subscription": {
"status": "ACTIVE",
"plan": {
"name": "plan name"
},
"subscriber": {
"code": "12133421"
}
}
}
}
My question is how to extract data["buyer"]["email"] in PHP ?
I only need to extract the email information from the buyer table inside the data table.
First, you need to decode the json to a PHP array (or an object), then you can access the requested information from the decoded data.
$data = json_decode('the json string most place here', true);
$email = $data['buyer']['email'];
Place your json string in the first argument of json_decode() function.

Laravel - How To Access and Manipulate JSON Array Elements

Can someone teach me how can i access each of the category element in the data array?? PS: I post this JSON using Postman and try to manipulate it in a laravel project controller method.
But i keep on getting this error:
ErrorException: Trying to get property 'data' of non-object
There is no problem with my routes as i have successful using get and post request for other simple process.
I also use Content-Type with application/json header to send this JSON using the Postman software.
My controller method:
public function requestStore(Request $request) {
//
}
JSON:
{
"id": "126612213872902",
"name": "Jack Daniel",
"likes": {
"data": [
{
"category": "Steakhouse",
"id": "330668300343348"
},
{
"category": "Steakhouse",
"id": "793734850733472"
},
{
"category": "Steakhouse",
"id": "650634241797601"
},
{
"category": "Steakhouse",
"id": "146621545355340"
},
{
"category": "Italian Restaurant",
"id": "146805911480"
},
{
"category": "Ice Cream Shop",
"id": "545851225458015"
},
{
"category": "Fast Food Restaurant",
"id": "24540959832"
},
{
"category": "Fast Food Restaurant",
"id": "478890372318431"
},
{
"category": "Fast Food Restaurant",
"id": "78934590040"
},
{
"category": "Korean Restaurant",
"id": "553600018012159"
},
{
"category": "Korean Restaurant",
"id": "388180681558608"
},
{
"category": "Chinese Restaurant",
"id": "288066224573571"
},
{
"category": "Chinese Restaurant",
"id": "77730698813"
},
{
"category": "Steakhouse",
"id": "80555139083"
},
{
"category": "Steakhouse",
"id": "276973702580"
},
{
"category": "Halal Restaurant",
"id": "407606596237025"
},
{
"category": "Malaysian Restaurant",
"id": "337074526415500"
},
{
"category": "Malaysian Restaurant",
"id": "607366992933940"
},
{
"category": "Malaysian Restaurant",
"id": "523902944321612"
},
{
"category": "Malaysian Restaurant",
"id": "194113415406"
},
{
"category": "Malaysian Restaurant",
"id": "177159752316413"
},
{
"category": "Malaysian Restaurant",
"id": "705777492887089"
},
{
"category": "Malaysian Restaurant",
"id": "1545544055681553"
},
{
"category": "Steakhouse",
"id": "558487800860920"
},
{
"category": "Steakhouse",
"id": "129509727619"
}
],
"paging": {
"cursors": {
"before": "MzMwNjY4MzAwMzQzMzQ4",
"after": "MTI5NTA5NzI3NjE5"
}
}
}
}
Assuming you're posting raw json through Postman like this don't forget to set type to JSON
Access the data like you would an array instead
Route::post('/test', function () {
$data = request()->all();
foreach ($data['likes']['data'] as $key => $value) {
dd($value['category']);
}
});
Result:
I hope this helps

transform data collection into laravel

I am starting to work with laravel and to obtain my data I use eloquent, so I have a collection of data that looks more or less like this:
[
{
"title": "Expense",
"name": "list",
"id": 2
},
{
"title": "Expense",
"name": "register",
"id": 3
},
{
"title": "Expense",
"name": "show",
"id": 4
},
{
"title": "Expense",
"name": "update",
"id": 5
},
{
"title": "Expense",
"name": "remove",
"id": 6
},
{
"title": "Income",
"name": "list",
"id": 7
},
{
"title": "Income",
"name": "register",
"id": 8
},
{
"title": "Income",
"name": "show",
"id": 9
},
{
"title": "Income",
"name": "update",
"id": 10
},
{
"title": "Income",
"name": "remove",
"id": 11
}
]
So I was wondering how I could transform this collection with the methods provided by it, or do I need to work with native arrays so that the collection I showed earlier can be as follows:
[
{
"title":"Expense",
"permissions":[
{
"id":2,
"name":"list"
},
{
"id":3,
"name":"show"
},
{
"id":4,
"name":"register"
},
{
"id":5,
"name":"update"
},
{
"id":6,
"name":"remove"
}
]
},
{
"title":"Income",
"permissions":[
{
"id":7,
"name":"list"
},
{
"id":8,
"name":"show"
},
{
"id":9,
"name":"register"
},
{
"id":10,
"name":"update"
},
{
"id":11,
"name":"remove"
}
]
}
]
This is more a PHP issue rather than a Laravel one. Eloquent won't change it for you, unless the permissions are linked to another table and then you will get them in that format, what you need to do is simply edit your array natively.
you can use groupBy in laravel collection
like this
$collection = new Collection([
{
"title": "Expense",
"name": "list",
"id": 2
},
{
"title": "Expense",
"name": "register",
"id": 3
},
{
"title": "Expense",
"name": "show",
"id": 4
},
{
"title": "Expense",
"name": "update",
"id": 5
},
{
"title": "Expense",
"name": "remove",
"id": 6
},
{
"title": "Income",
"name": "list",
"id": 7
},
{
"title": "Income",
"name": "register",
"id": 8
},
{
"title": "Income",
"name": "show",
"id": 9
},
{
"title": "Income",
"name": "update",
"id": 10
},
{
"title": "Income",
"name": "remove",
"id": 11
}
]);
$grouped = $collection->groupBy('title');
$grouped->toArray();

How to get facebook name of user who left comment

Newbie here, I'm trying to get facebook name of user who left comment on my facebook page post but its not working. I'm using graph API v2.6. Here is my code:
$data = $this->check_file_get_contents($this->graph_api.$object_id.'/comments?
fields=from,name&access_token='.$access_token);
//$data = $this->check_file_get_contents($this->graph_api.$object_id.'/&access_token='.$access_token);
$res = json_decode($data);
$uname = $res->name;
echo $uname;
Any idea of what could be wrong here? Little guidance would be very helpful. What did I miss?
The $object_id is the Post ID. This is the output from graph api:
{
"data": [
{
"from": {
"name": "John Doe",
"id": "932242153456808"
},
"id": "585601208257495_620658141418468"
},
{
"from": {
"name": "Jason King",
"id": "10153078961444510"
},
"id": "585601208257495_622535501230732"
},
{
"from": {
"name": "Jason King",
"id": "10153078961444510"
},
"id": "585601208257495_622588581225424"
}
],
"paging": {
"cursors": {
"before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOakl3TmpVNE1UUXhOREU0TkRZANE9qRTBOalEyTkRRMU5EZAz0ZD",
"after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOakl5TlRnNE5UZA3hNakkxTkRJME9qRTBOalV3TnpVNE9Uaz0ZD"
}
}
}
Try this out
<?php
$json='{
"data": [
{
"from": {
"name": "John Doe",
"id": "932242153456808"
},
"id": "585601208257495_620658141418468"
},
{
"from": {
"name": "Jason King",
"id": "10153078961444510"
},
"id": "585601208257495_622535501230732"
},
{
"from": {
"name": "Jason King",
"id": "10153078961444510"
},
"id": "585601208257495_622588581225424"
}
],
"paging": {
"cursors": {
"before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOakl3TmpVNE1UUXhOREU0TkRZANE9qRTBOalEyTkRRMU5EZAz0ZD",
"after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOakl5TlRnNE5UZA3hNakkxTkRJME9qRTBOalV3TnpVNE9Uaz0ZD"
}
}
}';
$res=json_decode($json,true);
$data=$res['data'];
for ($x = 0; $x <= count($data)-1; $x++) {
echo $data[$x]['from']['name']."<br>\n";
}
?>
Output:
John Doe<br>
Jason King<br>
Jason King<br>

Verify one layer only

I want to verify a variable holding json only has one level simple format. Can someone provide an example where only one level is verified - not a multi-level verification?
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
isValidOnlayer($json); // True
$json = '{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}'
isValidOnlayer($json); // False
?>
You can use this as a result of your function:
count(array_filter(json_decode($json, 1), 'is_array'))
$tmp = json_decode($json, true, 2); // depth=2, the array "itself" is level 1, its elements are level 2
if $tmp is null an error occurred. In case the depth limit was the problem json_last_error() will return JSON_ERROR_DEPTH

Categories