Laravel - How To Access and Manipulate JSON Array Elements - php

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

Related

Need to extract data from POST request with JSON response

I have a JSON response that I'm trying to gather data from. Most of the data is not needed but the way it is structured I'm having a tough time figuring out how to just get the data I need. The strings and values are paired kinda weird.
I need to be able to pull the data using PHP, and I need the info from the characteristic fields. Specifically, I need to pull the value for "qualificationTier".
What would I use to pull that string/value from this JSON response?
{
"externalId": "testinc-334",
"site": [
{
"id": "site-281",
"href": "qualification/site-281",
"description": "Service Qualification Successful",
"interactionDate": "2022-01-22T21:09:53.843Z",
"interactionDateComplete": "2022-01-22T21:09:59.873Z",
"interactionStatus": "Success",
"place": {
"id": "00000TBKM3",
"href": "site/00000TBKM3",
"characteristic": [
{
"name": "latitude",
"value": "32.98972"
},
{
"name": "longitude",
"value": "-117.271838"
},
{
"name": "region",
"value": "INREGION"
},
{
"name": "npa",
"value": "858"
},
{
"name": "nxx",
"value": "720"
},
{
"name": "lata",
"value": "732"
},
{
"name": "swCLLI",
"value": "DLMRCA12"
},
{
"name": "buildingCLLI",
"value": "SOBHCAWE"
},
{
"name": "ILECVendorName",
"value": "PACIFIC BELL"
},
{
"name": "affiliateName",
"value": "AT&T California"
},
{
"name": "unitType",
"value": "STE"
},
{
"name": "unitValue",
"value": "200"
},
{
"name": "standardAddress",
"value": "215 S HIGHWAY 101 STE 200,SOLANA BEACH,CA,92075-1847"
},
{
"name": "qualificationTier",
"value": "Tier 2"
},
{
"name": "fiberFastQualIndicator",
"value": "Y"
},
{
"name": "fiberStatus",
"value": "Fiber Lit"
}
]
},
"serviceAvailability": {
"service": [
{
"id": "adi0001",
"href": "/service/adi0001",
"name": "Customer Managed ADI",
"availability": "available",
"serviceabilityDate": "2022-01-22T21:09:59.880Z",
"serviceSpecification": [
{
"id": "miseth0000002uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000002uo",
"name": "Customer Managed ADI 2 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "2"
},
{
"name": "uploadSpeed",
"value": "2"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000002ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000002ue",
"name": "Customer Managed ADI 2 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "2"
},
{
"name": "uploadSpeed",
"value": "2"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000005uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000005uo",
"name": "Customer Managed ADI 5 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "5"
},
{
"name": "uploadSpeed",
"value": "5"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000005ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000005ue",
"name": "Customer Managed ADI 5 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "5"
},
{
"name": "uploadSpeed",
"value": "5"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000010uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000010uo",
"name": "Customer Managed ADI 10 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "10"
},
{
"name": "uploadSpeed",
"value": "10"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000010ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000010ue",
"name": "Customer Managed ADI 10 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "10"
},
{
"name": "uploadSpeed",
"value": "10"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000020uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000020uo",
"name": "Customer Managed ADI 20 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "20"
},
{
"name": "uploadSpeed",
"value": "20"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000020ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000020ue",
"name": "Customer Managed ADI 20 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "20"
},
{
"name": "uploadSpeed",
"value": "20"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000050uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000050uo",
"name": "Customer Managed ADI 50 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "50"
},
{
"name": "uploadSpeed",
"value": "50"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000050ue",
"availability": "available",
"href": "/serviceSpecification/miseth0000050ue",
"name": "Customer Managed ADI 50 Mbps Ethernet (Electrical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "50"
},
{
"name": "uploadSpeed",
"value": "50"
},
{
"name": "connectivity",
"value": "Electrical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
{
"id": "miseth0000100uo",
"availability": "available",
"href": "/serviceSpecification/miseth0000100uo",
"name": "Customer Managed ADI 100 Mbps Ethernet (Optical)",
"characteristic": [
{
"name": "serviceCode",
"value": "ADI"
},
{
"name": "accessType",
"value": "fiber"
},
{
"name": "serviceType",
"value": "Symmetric"
},
{
"name": "speedUnit",
"value": "Mbps"
},
{
"name": "downloadSpeed",
"value": "100"
},
{
"name": "uploadSpeed",
"value": "100"
},
{
"name": "connectivity",
"value": "Optical"
},
{
"name": "productAbbreviation",
"value": "ADI"
},
{
"name": "fiberFastQualType",
"value": "ONNET"
}
]
},
```
Check if this works.
<?php
$data = json_decode($your_json);
$qualificationTier = "qualificationTier";
// qualificationTier name is at position 13 in characteristic array
$QualificationTierName = $data->site->place->characteristic[13]->name;
$QualificationTierValue = "";
// Confirm indeed if the string name at position 13 in characteristics is indeed qualificationTier before retrieving the value at the same position
if(strcmp($qualificationTier, $QualificationTierName) == 0 ){
// qualificationTier value is at position 13 in characteristic array
$QualificationTierValue = $data->site->place->characteristic[13]->value;
echo "QualificationTier Name : " + $QualificationTierName;
echo "QualificationTier Value : " + $QualificationTierValue;
}
?>
A simple way would be to loop the array and get value when the name matches your field. You should first decode the json response so you will have an object to work with. Then just loop and set the variable when you have a match.
<?php
$response = json_decode($_POST['json-name']);
$qualificationTier = null;
foreach ($response->site->place->characteristic as $c) {
if ($c->name === 'qualificationTier') {
$qualificationTier = $c->value;
}
}

Don´t show empty days in business hours template from JReviews FormBuilder

Good morning, we use Joomla and JReviews and in some listings we show the opening hours. That works fine with the business hours template. Now we want to change this template for some other listings with training times.
We want, that for example the training is just on tuesdays all other days will display none.
The Code is like this:
{
"title": "Periods",
"type": "array",
"format": "table",
"items": {
"title": "Kurszeit",
"type": "object",
"properties": {
"day": {
"title": "Tag",
"type": "string",
"$ref": "#\/definitions\/weekeday",
"options": {
"input_width": "15em"
}
},
"hours-start": {
"title": "Von",
"type": "string",
"$ref": "#\/definitions\/hours",
"options": {
"input_width": "15em"
}
},
"hours-end": {
"title": "Bis",
"type": "string",
"$ref": "#\/definitions\/hours",
"options": {
"input_width": "15em"
}
}
}
},
"definitions": {
"weekeday": {
"type": "string",
"enumSource": [
{
"source": [
{
"value": "1",
"title": "Montag"
},
{
"value": "2",
"title": "Dienstag"
},
{
"value": "3",
"title": "Mittwoch"
},
{
"value": "4",
"title": "Donnerstag"
},
{
"value": "5",
"title": "Freitag"
},
{
"value": "6",
"title": "Samstag"
},
{
"value": "7",
"title": "Sonntag"
}
],
"title": "{{item.title}}",
"value": "{{item.value}}"
}
]
},
"hours": {
"type": "string",
"enumSource": [
{
"source": [
{
"value": "5.0",
"title": "05:00 Uhr"
},
{
"value": "5.25",
"title": "05:15 Uhr"
},
{
"value": "5.5",
"title": "05:30 Uhr"
},
{
"value": "5.75",
"title": "05:45 Uhr"
},
{
"value": "6.0",
"title": "06:00 Uhr"
},
{
"value": "6.25",
"title": "06:15 Uhr"
},
{
"value": "6.5",
"title": "06:30 Uhr"
},
{
"value": "6.75",
"title": "06:45 Uhr"
},
{
"value": "7.0",
"title": "07:00 Uhr"
},
{
"value": "7.25",
"title": "07:15 Uhr"
},
{
"value": "7.5",
"title": "07:30 Uhr"
},
{
"value": "7.75",
"title": "07:45 Uhr"
},
{
"value": "8.0",
"title": "08:00 Uhr"
},
{
"value": "8.25",
"title": "08:15 Uhr"
},
{
"value": "8.5",
"title": "08:30 Uhr"
},
{
"value": "8.75",
"title": "08:45 Uhr"
},
{
"value": "9.0",
"title": "09:00 Uhr"
},
{
"value": "9.25",
"title": "09:15 Uhr"
},
{
"value": "9.5",
"title": "09:30 Uhr"
},
{
"value": "9.75",
"title": "09:45 Uhr"
},
{
"value": "10.0",
"title": "10:00 Uhr"
},
{
"value": "10.25",
"title": "10:15 Uhr"
},
{
"value": "10.5",
"title": "10:30 Uhr"
},
{
"value": "10.75",
"title": "10:45 Uhr"
},
{
"value": "11.0",
"title": "11:00 Uhr"
},
{
"value": "11.25",
"title": "11:15 Uhr"
},
{
"value": "11.5",
"title": "11:30 Uhr"
},
{
"value": "11.75",
"title": "11:45 Uhr"
},
{
"value": "12.0",
"title": "12:00 Uhr"
},
{
"value": "12.25",
"title": "12:15 Uhr"
},
{
"value": "12.5",
"title": "12:30 Uhr"
},
{
"value": "12.75",
"title": "12:45 Uhr"
},
{
"value": "13.0",
"title": "13:00 Uhr"
},
{
"value": "13.25",
"title": "13:15 Uhr"
},
{
"value": "13.5",
"title": "13:30 Uhr"
},
{
"value": "13.75",
"title": "13:45 Uhr"
},
{
"value": "14.0",
"title": "14:00 Uhr"
},
{
"value": "14.25",
"title": "14:15 Uhr"
},
{
"value": "14.5",
"title": "14:30 Uhr"
},
{
"value": "14.75",
"title": "14:45 Uhr"
},
{
"value": "15.0",
"title": "15:00 Uhr"
},
{
"value": "15.25",
"title": "15:15 Uhr"
},
{
"value": "15.5",
"title": "15:30 Uhr"
},
{
"value": "15.75",
"title": "15:45 Uhr"
},
{
"value": "16.0",
"title": "16:00 Uhr"
},
{
"value": "16.25",
"title": "16:15 Uhr"
},
{
"value": "16.5",
"title": "16:30 Uhr"
},
{
"value": "16.75",
"title": "16:45 Uhr"
},
{
"value": "17.0",
"title": "17:00 Uhr"
},
{
"value": "17.25",
"title": "17:15 Uhr"
},
{
"value": "17.5",
"title": "17:30 Uhr"
},
{
"value": "17.75",
"title": "17:45 Uhr"
},
{
"value": "18.0",
"title": "18:00 Uhr"
},
{
"value": "18.25",
"title": "18:15 Uhr"
},
{
"value": "18.5",
"title": "18:30 Uhr"
},
{
"value": "18.75",
"title": "18:45 Uhr"
},
{
"value": "19.0",
"title": "19:00 Uhr"
},
{
"value": "19.25",
"title": "19:15 Uhr"
},
{
"value": "19.5",
"title": "19:30 Uhr"
},
{
"value": "19.75",
"title": "19:45 Uhr"
},
{
"value": "20.0",
"title": "20:00 Uhr"
},
{
"value": "20.25",
"title": "20:15 Uhr"
},
{
"value": "20.5",
"title": "20:30 Uhr"
},
{
"value": "20.75",
"title": "20:45 Uhr"
},
{
"value": "21.0",
"title": "21:00 Uhr"
},
{
"value": "21.25",
"title": "21:15 Uhr"
},
{
"value": "21.5",
"title": "21:30 Uhr"
},
{
"value": "21.75",
"title": "21:45 Uhr"
},
{
"value": "22.0",
"title": "22:00 Uhr"
},
{
"value": "22.25",
"title": "22:15 Uhr"
},
{
"value": "22.5",
"title": "22:30 Uhr"
},
{
"value": "22.75",
"title": "22:45 Uhr"
}
],
"title": "{{item.title}}",
"value": "{{item.value}}"
}
]
}
}
The Code for output is this:
<?php
/**
* #package JReviews
* #author ClickFWD LLC (https://www.jreviews.com)
* #copyright Copyright (C) 2010-2018 ClickFWD LLC
* #license GNU GPL version 3 or later
*/
defined('MVC_FRAMEWORK') or die;
$twentyfourHourFormat = true;
$daysArray = [
1 => __l('Montag',true),
2 => __l('Dienstag',true),
3 => __l('Mittwoch',true),
4 => __l('Donnerstag',true),
5 => __l('Freitag',true),
6 => __l('Samstag',true),
7 => __l('Sonntag',true)
];
$data = json_decode($text, true);
if ( (isset($data['periods']) && empty($data['periods'])) || empty($data) ) return;
$periods = isset($data['periods']) ? $data['periods'] : $data;
$timezone = isset($data['timezone']) ? $data['timezone'] : null;
S2App::import('Helper', 'opening_hours', 'jreviews');
$OpeningHours = (new OpeningHours($timezone))->addPeriods($periods);
$days = $OpeningHours
->twentyfour($twentyfourHourFormat)
->getOrderedPeriods();
$openNowLabel = __t("Open now", true);
$closedNowLabel = __t("Closed now", true);
$closedLabel = __t("Closed", true);
?>
<?php echo $this->renderControllerView('fields_phpformat', 'business_hours_schedule', compact('OpeningHours','days','daysArray'));?>
At the moment the Output is like this:
Monday:
Tuesday: 10:00 - 11:00
Wednesday:
Thursday:
Friday:
Saturday:
Sunday:
But we want this:
Tuesday: 10:00 - 11:00
Hopefully we could explain what we want.
Hopefully we could explain what we want.

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();

Retrieving likes of Facebook Posts in 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
}
}

unable to get json_decode field

I want to get title for each song from the json below in php.I am unable to get all the names .I tried with the code below but its not displaying anything.I really appreciate any help.Thanks in Advance.
code:
$json = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=5/json');
$data=(json_decode($json, true));
foreach ($data as $item)
{
foreach ($item->entry as $asset)
{
echo $asset->title;
}
}
json:
{
"feed": {
"author": {
"name": {
"label": "iTunes Store"
},
"uri": {
"label": "http://www.apple.com/itunes/"
}
},
"entry": [
{
"im:name": {
"label": "Happy (From \"Despicable Me 2\")"
},
"im:image": [
{
"label": "http://a923.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a1645.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a1228.phobos.apple.com/us/r30/Music6/v4/7b/9c/58/7b9c58cb-71b8-44f1-5539-864ea5e505e2/886444495165.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "G I R L"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/g-i-r-l/id823593445?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2014 Columbia Records, a Division of Sony Music Entertainment, 2013 Back Lot Music, under exclusive license to Columbia Records, a Division of Sony Music Entertainment"
},
"title": {
"label": "Happy (From \"Despicable Me 2\") - Pharrell Williams"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/happy-from-despicable-me-2/id823593445?i=823593456&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a1083.phobos.apple.com/us/r1000/014/Music/v4/4e/44/b7/4e44b7dc-aaa2-c63b-fb38-88e1635b5b29/mzaf_1844128138535731917.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/happy-from-despicable-me-2/id823593445?i=823593456&uo=2",
"attributes": {
"im:id": "823593456"
}
},
"im:artist": {
"label": "Pharrell Williams",
"attributes": {
"href": "https://itunes.apple.com/us/artist/pharrell-williams/id14934728?uo=2"
}
},
"category": {
"attributes": {
"im:id": "14",
"term": "Pop",
"scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2",
"label": "Pop"
}
},
"im:releaseDate": {
"label": "2014-03-03T00:00:00-07:00",
"attributes": {
"label": "March 3, 2014"
}
}
},
{
"im:name": {
"label": "All of Me"
},
"im:image": [
{
"label": "http://a1221.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a1943.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a982.phobos.apple.com/us/r30/Features/v4/c6/16/d4/c616d4bc-ae3c-2c3a-9b48-7ffece9e29b3/dj.velovadm.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "Love in the Future (Deluxe Edition)"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/love-in-future-deluxe-edition/id679297685?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2013 Getting Out Our Dreams and Columbia Records, a Division of Sony Music Entertainment"
},
"title": {
"label": "All of Me - John Legend"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/all-of-me/id679297685?i=679297849&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a355.phobos.apple.com/us/r1000/041/Music4/v4/90/cf/04/90cf0482-07c0-fca0-549f-c1ea62c4bdef/mzaf_6715619947923767616.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/all-of-me/id679297685?i=679297849&uo=2",
"attributes": {
"im:id": "679297849"
}
},
"im:artist": {
"label": "John Legend",
"attributes": {
"href": "https://itunes.apple.com/us/artist/john-legend/id16586443?uo=2"
}
},
"category": {
"attributes": {
"im:id": "15",
"term": "R&B/Soul",
"scheme": "https://itunes.apple.com/us/genre/music-r-b-soul/id15?uo=2",
"label": "R&B/Soul"
}
},
"im:releaseDate": {
"label": "2013-08-30T00:00:00-07:00",
"attributes": {
"label": "August 30, 2013"
}
}
},
{
"im:name": {
"label": "Let It Go"
},
"im:image": [
{
"label": "http://a261.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a983.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a262.phobos.apple.com/us/r30/Music/v4/26/be/46/26be4693-e743-f45a-4629-35554af16181/UMG_cvrart_00050087301644_01_RGB72_1500x1500_13DMGIM04438.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "Frozen (Deluxe Edition) [Original Motion Picture Soundtrack]"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/frozen-deluxe-edition-original/id728903889?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2013 Walt Disney Records"
},
"title": {
"label": "Let It Go - Idina Menzel"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/let-it-go/id728903889?i=728904000&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a878.phobos.apple.com/us/r1000/023/Music/v4/0a/eb/c2/0aebc299-44e3-3465-3cdf-5bab336b547a/mzaf_7970981567593232494.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/let-it-go/id728903889?i=728904000&uo=2",
"attributes": {
"im:id": "728904000"
}
},
"im:artist": {
"label": "Idina Menzel",
"attributes": {
"href": "https://itunes.apple.com/us/artist/idina-menzel/id3297504?uo=2"
}
},
"category": {
"attributes": {
"im:id": "16",
"term": "Soundtrack",
"scheme": "https://itunes.apple.com/us/genre/music-soundtrack/id16?uo=2",
"label": "Soundtrack"
}
},
"im:releaseDate": {
"label": "2013-11-25T00:00:00-07:00",
"attributes": {
"label": "November 25, 2013"
}
}
},
{
"im:name": {
"label": "Dark Horse (feat. Juicy J)"
},
"im:image": [
{
"label": "http://a1657.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a731.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a1498.phobos.apple.com/us/r30/Features4/v4/f7/3e/40/f73e4011-5ed3-fc65-9107-2438acd70509/dj.hbxrueel.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "PRISM (Deluxe Version)"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/prism-deluxe-version/id690928033?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2013 Capitol Records, LLC"
},
"title": {
"label": "Dark Horse (feat. Juicy J) - Katy Perry"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/dark-horse-feat.-juicy-j/id690928033?i=690928460&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a1930.phobos.apple.com/us/r1000/022/Music/v4/a4/ac/63/a4ac6341-59e3-bcfe-b7f5-370100eea063/mzaf_4835051646631223146.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/dark-horse-feat.-juicy-j/id690928033?i=690928460&uo=2",
"attributes": {
"im:id": "690928460"
}
},
"im:artist": {
"label": "Katy Perry",
"attributes": {
"href": "https://itunes.apple.com/us/artist/katy-perry/id64387566?uo=2"
}
},
"category": {
"attributes": {
"im:id": "14",
"term": "Pop",
"scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2",
"label": "Pop"
}
},
"im:releaseDate": {
"label": "2013-10-22T00:00:00-07:00",
"attributes": {
"label": "October 22, 2013"
}
}
},
{
"im:name": {
"label": "The Man"
},
"im:image": [
{
"label": "http://a1169.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.55x55-70.jpg",
"attributes": {
"height": "55"
}
},
{
"label": "http://a243.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.60x60-50.jpg",
"attributes": {
"height": "60"
}
},
{
"label": "http://a1234.phobos.apple.com/us/r30/Music4/v4/67/f0/bb/67f0bbfb-9b0f-ee34-3646-b1bfbe704d4f/UMG_cvrart_00602537732579_01_RGB72_1500x1500_13UAEIM00026.170x170-75.jpg",
"attributes": {
"height": "170"
}
}
],
"im:collection": {
"im:name": {
"label": "Lift Your Spirit"
},
"link": {
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/lift-your-spirit/id798928288?uo=2"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Album",
"label": "Album"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
}
},
"im:price": {
"label": "$1.29",
"attributes": {
"amount": "1.29000",
"currency": "USD"
}
},
"im:contentType": {
"im:contentType": {
"attributes": {
"term": "Track",
"label": "Track"
}
},
"attributes": {
"term": "Music",
"label": "Music"
}
},
"rights": {
"label": "℗ 2014 Aloe Blacc Recording, Inc. under exclusive license to XIX Recordings LLC/Interscope Records"
},
"title": {
"label": "The Man - Aloe Blacc"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/us/album/the-man/id798928288?i=798928362&uo=2"
}
},
{
"im:duration": {
"label": "30000"
},
"attributes": {
"title": "Preview",
"rel": "enclosure",
"type": "audio/x-m4a",
"href": "http://a85.phobos.apple.com/us/r1000/033/Music6/v4/8a/09/dc/8a09dcf0-4845-81ae-6936-a94972ad94e7/mzaf_5332424013433716082.plus.aac.p.m4a",
"im:assetType": "preview"
}
}
],
"id": {
"label": "https://itunes.apple.com/us/album/the-man/id798928288?i=798928362&uo=2",
"attributes": {
"im:id": "798928362"
}
},
"im:artist": {
"label": "Aloe Blacc",
"attributes": {
"href": "https://itunes.apple.com/us/artist/aloe-blacc/id4750752?uo=2"
}
},
"category": {
"attributes": {
"im:id": "14",
"term": "Pop",
"scheme": "https://itunes.apple.com/us/genre/music-pop/id14?uo=2",
"label": "Pop"
}
},
"im:releaseDate": {
"label": "2014-03-11T00:00:00-07:00",
"attributes": {
"label": "March 11, 2014"
}
}
}
],
"updated": {
"label": "2014-03-26T14:09:41-07:00"
},
"rights": {
"label": "Copyright 2008 Apple Inc."
},
"title": {
"label": "iTunes Store: Top Songs"
},
"icon": {
"label": "http://itunes.apple.com/favicon.ico"
},
"link": [
{
"attributes": {
"rel": "alternate",
"type": "text/html",
"href": "https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?cc=us&id=38&popId=1"
}
},
{
"attributes": {
"rel": "self",
"href": "http://itunes.apple.com/us/rss/topsongs/limit=5/json"
}
}
],
"id": {
"label": "http://itunes.apple.com/us/rss/topsongs/limit=5/json"
}
}
}
$json = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=5/json');
$data=json_decode($json);
$titles = array();
foreach ($data as $item)
{
foreach ($item->entry as $asset)
{
$titles[] = $asset->title->label;
}
}

Categories