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>
Related
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.
The question is about to know if the specific message has been answered or not.
I'm able to get the messages of the inbox of a Facebook page with this
$facebook_graph_get_messages = https://graph.facebook.com/--page--id--/conversations?fields=id,messages{message,to,from,created_time}&access_token=....
$raw_graph = file_get_contents($facebook_graph_get_messages);
$raw_graph_parsed = mb_convert_encoding($raw_graph, "UTF-8",mb_detect_encoding($raw_graph));
$fb_page_conversations = json_decode($raw_graph_parsed, true);
foreach ( $fb_page_convers['data'] as $fb_page_conver_key => $fb_page_conver_arra ) {
foreach ( $fb_page_conver_arra['messages']['data'] as $fbpcmd ) {
$fpms_txt = html_entity_decode($fbpcmd['message']);
echo $fpms_txt;
I get fields message, to, from, created_time.... but I didn't found in the documentation how to know if the message is answered or not ...
Thanks
* EDIT *
Sample
{
"data": [
{
"id": "t_...",
"messages": {
"data": [
{
"message": "test",
"to": {
"data": [
{
"name": "...",
"email": "...\u0040facebook.com",
"id": "..."
}
]
},
"from": {
"name": "...",
"email": "...\u0040facebook.com",
"id": "..."
},
"created_time": "2020-04-05T03:27:49+0000",
"id": "m_dW...."
}
],
"paging": {
"cursors": {
"before": "QVFI....",
"after": "QVFI..."
}
}
}
},
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
i am tring to parse a special json content. I got this as an output file from a cucumber execution. The goal is it to decode some values like the name the status and some other content. How can i encode that.
Another concern would be a transformation of json into a CSV.
It is not important for me to use php. Java or Perl would be an alternative.
This file I am gonna call in php with:
$jsonconntent = file_get_contents("/home/xxx/test1.json");
The json conten looks like this (I posted only the beginning):
[
{
"uri": "features/complete_ski.feature",
"id": "complete_ski_with_time",
"keyword": "Feature",
"name": "Complete_Ski_with_time",
"description": "",
"line": 1,
"elements": [
{
"id": "complete_ski_with_time;time_part_preamble",
"keyword": "Scenario",
"name": "time_part_preamble",
"description": "",
"line": 3,
"type": "scenario",
"before": [
{
"output": [
"Default Timestamp start: 1516024716000"
],
"match": {
"location": "features/support/env.rb:32"
},
"result": {
"status": "passed",
"duration": 191690
}
},
{
"match": {
"location": "capybara-2.17.0/lib/capybara/cucumber.rb:13"
},
"result": {
"status": "passed",
"duration": 52117
}
},
{
"match": {
"location": "capybara-2.17.0/lib/capybara/cucumber.rb:21"
},
"result": {
"status": "passed",
"duration": 25885
}
}
],
"steps": [
{
"keyword": "Given ",
"name": "a Android A-Party",
"line": 4,
"output": [
"Got handset with number unvisable, IMSI: notfor, android-Id: yourfone, VNC: 11111, port: 9981"
],
"match": {
"location": "features/step_definitions/idrp_steps.rb:11"
},
"result": {
"status": "passed",
"duration": 1415024760
},
"after": [
{
"match": {
"location": "features/support/env.rb:24"
},
"result": {
"status": "passed",
"duration": 264339
}
}
]
}
Use:
$data = json_decode($jsonconntent, true);
You will have the data with javascript objects as arrays, not PHP objects.
To save it as CSV, it depends on the structure of the data. In this case, it is complicated:
{
"uri": "features/complete_ski.feature",
"id": "complete_ski_with_time",
"keyword": "Feature",
"name": "Complete_Ski_with_time",
"description": "",
"line": 1,
"elements": [
{
"id": "complete_ski_with_time;time_part_preamble",
"keyword": "Scenario",
"name": "time_part_preamble",
"description": "",
"line": 3,
...
How do you put the data in the column elements? Data is so nested that it cannot be transformed into a tabular format with column headers and one value per column in each row.
As to how to access the data, you can do this:
$first_element_id = $data[0]['elements'][0]['id'];
foreach ( $data as $item ) {
$uri = $item['uri'];
foreach ( $item['elements'] as $element ) {
$name = $element['name'];
}
}
As asked in one of the comments, this is how to access the 'Default Timestamp start':
foreach ($data as $item) {
foreach ($item['elements'] as $element) {
foreach ($element['before'] as $before) {
if (isset($before['output'])) {
foreach ($before['output'] as $output) {
echo sprintf("%s\n", $output);
}
}
}
}
}
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
}
}