just tried several $result->key ($customer->transaction->status) but was not successful. i need the "status" value of this object in php. tried some combinations of array shift, ->object ->["status"] etc.
object(stdClass)#2 (3) {
["customer"]=>
object(stdClass)#3 (1) {
["link"]=>
object(stdClass)#4 (3) {
["url"]=>
string(78) "https://demo1.com"
["rel"]=>
string(8) "customer"
["method"]=>
string(3) "GET"
}
}
["transaction"]=>
object(stdClass)#5 (9) {
["merchantRefId"]=>
string(19) "46532156465456"
["amount"]=>
int(200)
["currency"]=>
string(3) "EUR"
["id"]=>
string(15) "646544564564"
["transactionType"]=>
string(27) "Transfer"
["createDate"]=>
string(19) "2016-01-26 08:33:09"
["updateDate"]=>
string(19) "2016-01-26 08:33:09"
["status"]=>
string(8) "accepted"
["fees"]=>
array(1) {
[0]=>
object(stdClass)#6 (3) {
["feeType"]=>
string(11) "service_fee"
["feeAmount"]=>
int(119)
["feeCurrency"]=>
string(3) "EUR"
}
}
}
["links"]=>
array(1) {
[0]=>
object(stdClass)#7 (3) {
["url"]=>
string(78) "https://demo.com"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
}
Since you didn't provide the code that creates your object, I've created JSON string and converted it to object, which gives similar var_dump result to yours.
<?php
$jsonStr = '{
"customer": {
"link": {
"url": "https://demo1.com",
"rel": "customer",
"method": "GET"
}
},
"transaction": {
"merchantRefId": "46532156465456",
"amount": 200,
"currency": "EUR",
"id": "646544564564",
"transactionType": "Transfer",
"createDate": "2016-01-26 08:33:09",
"updateDate": "2016-01-26 08:33:09",
"status": "accepted",
"fees": [
{
"feeType": "service_fee",
"feeAmount": 119,
"feeCurrency": "EUR"
}
]
},
"links": [
{
"url": "https://demo.com",
"rel": "self",
"method": "GET"
}
]
}';
$stdObj = json_decode($jsonStr);
var_dump($stdObj);
var_dump($stdObj->transaction->status);
I am able, and you should be too, to simply get status with simple:
$customer->transaction->status
Related
I'm having trouble sorting alphabetically an object returned by the json_decode function in PHP.
The object I've got is as follows:
object(stdClass)#9263 (1) {
["Activities"]=>
array(91) {
[0]=>
object(stdClass)#6116 (3) {
["Code"]=>
string(1) "5"
["NameFr"]=>
string(16) "Droit ju"
["NameNl"]=>
string(18) "Ger recht"
}
[1]=>
object(stdClass)#8582 (3) {
["Code"]=>
string(1) "1"
["NameFr"]=>
string(19) "Droit per"
["NameNl"]=>
string(13) "Pers recht"
}
[2]=>
object(stdClass)#8598 (3) {
["Code"]=>
string(5) "2"
["NameFr"]=>
string(11) "Droit ca"
["NameNl"]=>
string(14) "Ca recht"
}
...
I need to sort it using the "NameFr" property, alphabetically.
try usort
<?php
$data = '
{
"Activities": [
{
"Code": "5",
"NameFr": "Droit ju",
"NameNl": "Ger recht"
},
{
"Code": "1",
"NameFr": "Droit per",
"NameNl": "Pers recht"
},
{
"Code": "2",
"NameFr": "Droit ca",
"NameNl": "Ca recht"
}
]
}';
$data = json_decode($data, true);
usort($data["Activities"], function($a, $b) {
return $a['NameFr'] <=> $b['NameFr'];
});
print_r($data);
I used stripe to return a Customer Object. I want to extract the bank account ID and save it in a database for later use. What is the proper syntax to access the array properly?
Here is what I have tried:
$bank_account=$customer->lastResponse['json']['sources']['data'][0]['id'];
I was told to try:
$bank_account=$customer->bank_accounts->data[0]['id'];
But that came up null as well...
Im not sure how to work with the object or what the right way to do this is.
I need the bank_account_id
This is what the var_dump($customer) looks like after some formatting and removing information that is not pertinent. I think I can either use a JSON decode or access the array called 'json' i dont know the proper syntax for either of those solutions. Help please (:
object(Stripe\Customer)#3572 (6)
{
["_opts":protected]=> object(Stripe\Util\RequestOptions)#3576 (2) { ["headers"]=> array(1) { ["Stripe-Account"]=> string(21) "acct_1BNn74AprSj6yALS" } ["apiKey"]=> string(32) "secretkey" }
["_values":protected]=> array(15) {//Removed For brevity}
["_unsavedValues":protected]=> object(Stripe\Util\Set)#3590 (1) { ["_elts":"Stripe\Util\Set":private]=> array(0) { } } ["_transientValues":protected]=> object(Stripe\Util\Set)#3602 (1) { ["_elts":"Stripe\Util\Set":private]=> array(0) { } } ["_retrieveOptions":protected]=> array(0) { }
["_lastResponse":protected]=>
object(Stripe\ApiResponse)#3714 (4)
{
["headers"]=> array(15) { //Removed for brevity}
// Json
["body"]=> string(1148) "{
"id": "cus_BoJOjFghfV7mmq", "object": "customer", "account_balance": 0, "created": 1511290036, "currency": null, "default_source": "bank_account_id", "delinquent": false, "description": "Name", "discount": null, "email": null, "livemode": true, "metadata": {}, "shipping": null, "sources": { "object": "list", "data": [ { "id": "bank_account_id", "object": "bank_account", "account_holder_name": "Daniel Taylor", "account_holder_type": "individual", "bank_name": "Bank Name", "country": "US", "currency": "usd", "customer": "cus_id", "fingerprint": "info", "last4": "last four of account", "metadata": {}, "routing_number": "routing number", "status": "new" } ], "has_more": false, "total_count": 1, "url": "/v1/customers/cus_BoJOjFghfV7mmq/sources" }, "subscriptions": { "object": "list", "data": [], "has_more": false, "total_count": 0, "url": "/v1/customers/cus_BoJOjFghfV7mmq/subscriptions" }
} "
["json"]=> array(15)
{
["id"]=> string(18) "cus_id"
["object"]=> string(8) "customer"
["account_balance"]=> int(0)
["created"]=> int(1511290036)
["currency"]=> NULL
["default_source"]=> string(27) "bank_account_id"
["delinquent"]=> bool(false)
["description"]=> string(13) "Name"
["discount"]=> NULL
["email"]=> NULL ["livemode"]=> bool(true)
["metadata"]=> array(0) { }
["shipping"]=> NULL
["sources"]=> array(5)
{
["object"]=> string(4) "list"
["data"]=> array(1)
{
[0]=> array(13)
{
["id"]=> string(27) "bank_account_id" ["object"]=> string(12) "bank_account"
["account_holder_name"]=> string(13) "name"
["account_holder_type"]=> string(10) "individual"
["bank_name"]=> string(26) "Bank Name"
["country"]=> string(2) "US" ["currency"]=> string(3) "usd"
["customer"]=> string(18) "cus_ID"
["fingerprint"]=> string(16) "fingerprint"
["last4"]=> string(4) "lastfour" ["metadata"]=> array(0) { }
["routing_number"]=> string(9) "routenumber"
["status"]=> string(3) "new"
}
}
["has_more"]=> bool(false)
["total_count"]=> int(1)
["url"]=> string(40) "/v1/customers/cus_BoJOjFghfV7mmq/sources"
}
["subscriptions"]=> array(5) { ["object"]=> string(4) "list" ["data"]=> array(0) { } ["has_more"]=> bool(false) ["total_count"]=> int(0) ["url"]=> string(46) "/v1/customers/cus_id/subscriptions" }
}
["code"]=> int(200)
}
}
I think it should be:
$bank_account = $customer->sources->data[0]->id
If the customer has multiple fund sources, you may need to loop through the data array to find the one you want. $customer->sources->data[$i]->object will be "bank_account" for the source you want.
Pretty sure there is a getter in there. You can just use the properties you would expect on the object.
Example:
$customer_id = $customer->id;
$bank_account_id = $customer->sources->data[0]->id;
Below is the result and I want to remove duplicate from the array
I tried using this code: $login_data1['items'] = array_values(array_map("unserialize", array_unique(array_map("serialize", $login_data1['items']))));
{
"items": [
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual",
},
{
"id": "1",
"tags": [
{
"name": "Snow Leopard"
}
],
"type": "faq"
},
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
}
],
}
I tried using $login_data1['items'] = array_unique($login_data1['items'] ,SORT_REGULAR); but this adds serial numbers at the each json response
Try as using array_unique
$json = '{
"items": [
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
},
{
"id": "1",
"tags": [
{
"name": "Snow Leopard"
}
],
"type": "faq"
},
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
}
]
}';
foreach(json_decode($json, true) as $key => $value){
$input = array_unique($value,SORT_REGULAR);
}
If its an array then simply use
array_unique($login_data['items'],SORT_REGULAR);
Fiddle
array_unique works perfectly if you pass a multidimensional array.
$login_data1['items'] = array_unique($login_data1['items'], SORT_REGULAR);
It doesn't work with your json because it's an array of object. Infact:
$array = json_decode($json);
var_dump($array);
returns:
object(stdClass)#1 (1) {
["items"]=>
array(3) {
[0]=>
object(stdClass)#2 (3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#3 (1) {
["name"]=>
string(9) "Microsoft"
}
}
["type"]=>
string(6) "manual"
}
[1]=>
object(stdClass)#4 (3) {
["id"]=>
string(1) "1"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#5 (1) {
["name"]=>
string(12) "Snow Leopard"
}
}
["type"]=>
string(3) "faq"
}
[2]=>
object(stdClass)#6 (3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#7 (1) {
["name"]=>
string(9) "Microsoft"
}
}
["type"]=>
string(6) "manual"
}
}
}
Your json should look like this:
{
"items": [
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
},
{
"id": "1",
"tags": {
"name": "Snow Leopard"
},
"type": "faq"
},
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
},
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
}
]
}
And now:
$array = json_decode($json);
var_dump($array);
returns:
array(1) {
["items"]=>
array(4) {
[0]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
[1]=>
array(3) {
["id"]=>
string(1) "1"
["tags"]=>
array(1) {
["name"]=>
string(12) "Snow Leopard"
}
["type"]=>
string(3) "faq"
}
[2]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
[3]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
}
}
And array_unique works.
I got the solution for this.
$login_data1['items'] =array_values(array_unique($login_data1['items'] ,SORT_REGULAR));
Try array_value and array_unique together and serial number will be removed!
$login_data1['items'] =array_values(array_unique($login_data1['items'] ,SORT_REGULAR));
My Array;
{
"bio": "Testing Facebook Bio -> Stupidly forgot to set whilst trying to fetch this information testing my latest Facebook App!",
"movies": {
"data": [
{
"name": "Night At The Museum",
"id": "251922028320619"
},
{
"name": "Little Nicky",
"id": "112378985439799"
},
{
"genre": "Action / Adventure / Comedy / Family / Sci-Fi",
"name": "Back to the Future Trilogy",
"id": "141545972523915"
},
{
"genre": "Stupid",
"name": "Jackass",
"id": "21295715752"
},
{
"genre": "Comedy",
"name": "Mrs. Brown's Boys D'Movie",
"id": "217475368404328"
},
{
"genre": "Animation, Family",
"name": "Madagascar",
"id": "149800431712088"
},
{
"genre": "Animation",
"name": "Frozen",
"id": "482368755113431"
},
{
"genre": "Animation",
"name": "Toy Story",
"id": "10498014129"
},
{
"genre": "This is a gritty drama that develops into an uplifting story of triumph over adversity. It should appeal to a wide audience, both male and female.",
"name": "Breaking Free Film",
"id": "135991503231501"
},
{
"genre": "Animation",
"name": "The Lion King",
"id": "12393266550"
},
{
"genre": "Animation | Comedy | Family ",
"name": "Despicable Me",
"id": "117067844993952"
},
{
"genre": "Action / Adventure",
"name": "Thor",
"id": "113589202010624"
},
{
"name": "Scooby-Doo: The Movie",
"id": "306089742863765"
},
{
"genre": "Action / fairy tale",
"name": "Hansel & Gretel: Witch Hunters",
"id": "271965656164363"
},
{
"name": "Harry Potter",
"id": "156794164312"
},
{
"name": "Star Wars",
"id": "216676368377759"
},
{
"name": "Minion",
"id": "136787429687873"
},
{
"name": "Night at the Museum 2",
"id": "115126478502712"
},
{
"genre": "Animation, Holiday, Family",
"name": "The Nightmare Before Christmas",
"id": "173587329354820"
},
{
"name": "Pacific Rim",
"id": "439835889373123"
},
{
"genre": "Action Adventure",
"name": "Oblivion UK",
"id": "235958443193536"
},
{
"genre": "Animation, Family",
"name": "Shrek",
"id": "355374000182"
},
{
"name": "Scooby Doo: The Movie",
"id": "106352129401640"
},
{
"genre": "Epic Action-Adventure",
"name": "Dracula Untold",
"id": "332230740134829"
},
{
"name": "Simba",
"id": "27665751322"
}
],
"paging": {
"next": "https://graph.facebook.com/v2.2/663878750359892/movies?fields=genre,name&limit=25&offset=25&__after_id=enc_AezyAtvaN1UtcaRwF9kgU5Z5PIv07LU_3Wli-CRCkYEol9BoJJtn86fNGT4v-XsnG-o"
}
},
"id": "663878750359892"
}
I know to get the "Bio" from my array, however this is pretty much out there onthe internet how to get the first level information from the Facebook API Array passed on;
$FB_About_Bio = $graph->getProperty('bio');
However when it comes to calling for "movies";
$FB_About_Movies = $graph->getProperty('movies');
This produces an array of itself I believe of which I do not know how to do a loop to list all the movie titles for example
How would I get all the movie names?
Full Script:
<?php
/* INCLUSION OF LIBRARY FILEs*/
require_once( 'lib/Facebook/FacebookSession.php');
require_once( 'lib/Facebook/FacebookRequest.php' );
require_once( 'lib/Facebook/FacebookResponse.php' );
require_once( 'lib/Facebook/FacebookSDKException.php' );
require_once( 'lib/Facebook/FacebookRequestException.php' );
require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php');
require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
require_once( 'lib/Facebook/GraphObject.php' );
require_once( 'lib/Facebook/GraphUser.php' );
require_once( 'lib/Facebook/GraphSessionInfo.php' );
require_once( 'lib/Facebook/Entities/AccessToken.php');
require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php');
require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php');
/* USE NAMESPACES */
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\FacebookHttpable;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookCurl;
/*PROCESS*/
//1.Stat Session
session_start();
//2.Use app id,secret and redirect url
$app_id = '000000000000000';
$app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$redirect_url='http://rafflebananza.com/Desktop/facebook.php';
//3.Initialize application, create helper object and get fb sess
FacebookSession::setDefaultApplication($app_id,$app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_url);
$sess = $helper->getSessionFromRedirect();
//4. if fb sess exists echo name
if(isset($sess)){
//store the token in the php session
$_SESSION['fb_token']=$sess->getToken();
//create request object,execute and capture response
$request = new FacebookRequest($sess,'GET','/me');
// from response get graph object
$response = $request->execute();
$graph = $response->getGraphObject(GraphUser::classname());
// use graph object methods to get user details
$FB_id = $graph->getId();
$FB_First_Name = $graph->getProperty('first_name');
$FB_Middle_Name = $graph->getProperty('middle_name');
$FB_Last_Name = $graph->getProperty('last_name');
$FB_About_Bio = $graph->getProperty('bio');
$FB_image = 'https://graph.facebook.com/'.$id.'/picture?width=300';
$FB_email = $graph->getProperty('email');
$graphArray = $graph->asArray();
$movies = $graphArray['movies']['data'];
foreach ($movies as $movie) {
$name = $movie['name'];
}
// Echo Info To Page:
echo '<h1>'.$name.'</h1>';
echo '<table><tr><td><strong>First Name:</strong></td><td><input type="text" value="'.$FB_First_Name.'"></input></td></tr>';
echo "<tr><td><strong>Middle Name: </strong></td><td>$FB_Middle_Name</td></tr>";
echo "<tr><td><strong>Last Name: </strong></td><td>$FB_Last_Name <br</td></tr>";
echo "<tr><td><strong>E-Mail Address:</strong></td><td>$FB_email</td></tr>";
echo "<tr><td><strong>About You:</strong></td><td>$FB_About_Bio</td></tr></table>";
echo "<img src='FB_$image' /><br><br>";
// Logout Button
echo "<a href='".$logout."'><button>Logout</button></a>";
}else{
//else echo login
echo '<a href="'.$helper->getLoginUrl(array('email')).'" >Login with facebook</a>';
}
Update
user9418 asked on Stackoverflow "How to parse a facebook graph api response" and bhushya answer has given somewhat of an insight to how it should be done. Below is my latest progress whereas previously I was getting no data whatsoever;
New Snippet;
$user_profile = (new FacebookRequest(
$sess, 'GET', '/me/movies'
))->execute()->getGraphObject();
$movies = $user_profile->getProperty('data');
$movies_data = $movies->asArray();//this will do all job for you..
foreach($movies_data as $row){
var_dump($row);
}
Result:
object(stdClass)#28 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(19) "Night At The Museum" ["created_time"]=> string(24)
"2014-12-16T23:05:57+0000" ["id"]=> string(15) "251922028320619" }
object(stdClass)#29 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(12) "Little Nicky" ["created_time"]=> string(24)
"2014-12-14T02:35:10+0000" ["id"]=> string(15) "112378985439799" }
object(stdClass)#30 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(26) "Back to the Future Trilogy" ["created_time"]=> string(24)
"2014-10-06T04:12:32+0000" ["id"]=> string(15) "141545972523915" }
object(stdClass)#31 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(7) "Jackass" ["created_time"]=> string(24)
"2014-08-23T21:51:24+0000" ["id"]=> string(11) "21295715752" }
object(stdClass)#32 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(25) "Mrs. Brown's Boys D'Movie" ["created_time"]=> string(24)
"2014-08-06T10:16:10+0000" ["id"]=> string(15) "217475368404328" }
object(stdClass)#33 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(10) "Madagascar" ["created_time"]=> string(24)
"2014-07-09T22:16:05+0000" ["id"]=> string(15) "149800431712088" }
object(stdClass)#34 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(6) "Frozen" ["created_time"]=> string(24)
"2014-05-27T20:47:31+0000" ["id"]=> string(15) "482368755113431" }
object(stdClass)#35 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(9) "Toy Story" ["created_time"]=> string(24)
"2014-04-15T01:39:17+0000" ["id"]=> string(11) "10498014129" }
object(stdClass)#36 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(19) "Breaking Free Film" ["created_time"]=> string(24)
"2014-02-20T14:16:06+0000" ["id"]=> string(15) "135991503231501" }
object(stdClass)#37 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(13) "The Lion King" ["created_time"]=> string(24)
"2014-02-19T08:32:34+0000" ["id"]=> string(11) "12393266550" }
object(stdClass)#38 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(13) "Despicable Me" ["created_time"]=> string(24)
"2014-02-19T08:32:29+0000" ["id"]=> string(15) "117067844993952" }
object(stdClass)#39 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(4) "Thor" ["created_time"]=> string(24)
"2013-11-29T02:22:14+0000" ["id"]=> string(15) "113589202010624" }
object(stdClass)#40 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(21) "Scooby-Doo: The Movie" ["created_time"]=> string(24)
"2013-10-17T13:41:51+0000" ["id"]=> string(15) "306089742863765" }
object(stdClass)#41 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(30) "Hansel & Gretel: Witch Hunters" ["created_time"]=>
string(24) "2013-10-15T21:22:05+0000" ["id"]=> string(15)
"271965656164363" } object(stdClass)#42 (4) { ["category"]=> string(5)
"Movie" ["name"]=> string(12) "Harry Potter" ["created_time"]=>
string(24) "2013-10-09T21:29:08+0000" ["id"]=> string(12)
"156794164312" } object(stdClass)#43 (4) { ["category"]=> string(5)
"Movie" ["name"]=> string(9) "Star Wars" ["created_time"]=> string(24)
"2013-08-30T20:12:14+0000" ["id"]=> string(15) "216676368377759" }
object(stdClass)#44 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(6) "Minion" ["created_time"]=> string(24)
"2013-07-22T12:25:27+0000" ["id"]=> string(15) "136787429687873" }
object(stdClass)#45 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(21) "Night at the Museum 2" ["created_time"]=> string(24)
"2013-06-14T01:18:02+0000" ["id"]=> string(15) "115126478502712" }
object(stdClass)#46 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(30) "The Nightmare Before Christmas" ["created_time"]=>
string(24) "2013-06-08T11:11:36+0000" ["id"]=> string(15)
"173587329354820" } object(stdClass)#47 (4) { ["category"]=> string(5)
"Movie" ["name"]=> string(11) "Pacific Rim" ["created_time"]=>
string(24) "2013-05-16T19:30:24+0000" ["id"]=> string(15)
"439835889373123" } object(stdClass)#48 (4) { ["category"]=> string(5)
"Movie" ["name"]=> string(11) "Oblivion UK" ["created_time"]=>
string(24) "2013-02-14T23:02:31+0000" ["id"]=> string(15)
"235958443193536" } object(stdClass)#49 (4) { ["category"]=> string(5)
"Movie" ["name"]=> string(5) "Shrek" ["created_time"]=> string(24)
"2012-10-03T07:01:57+0000" ["id"]=> string(12) "355374000182" }
object(stdClass)#50 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(21) "Scooby Doo: The Movie" ["created_time"]=> string(24)
"2012-09-20T11:55:28+0000" ["id"]=> string(15) "106352129401640" }
object(stdClass)#51 (4) { ["category"]=> string(5) "Movie" ["name"]=>
string(14) "Dracula Untold" ["created_time"]=> string(24)
"2012-09-17T16:15:38+0000" ["id"]=> string(15) "332230740134829" }
object(stdClass)#52 (4) { ["category"]=> string(15) "Movie character"
["name"]=> string(5) "Simba" ["created_time"]=> string(24)
"2012-09-06T22:23:22+0000" ["id"]=> string(11) "27665751322" }
My full code now being the same with this implemented above my echoing to the page. I just need to learn now how to loop through and list only the name of each movie wrapping each one in a tag!
Option 1: Convert the graph object to an array using the asArray() method and parse as a normal array.
$graphArray = $graph->asArray();
$movies = $graphArray['movies']['data'];
foreach ($movies as $movie) {
$name = $movie['name'];
}
Option 2: get the data property and loop through that array, then get the genre, name, and id properties using the getProperty method.
$movies = $graph->getProperty('movies');
foreach ($movies as $movie) {
$name = $movie->getProperty('name');
}
Resolved Snippet
$FB_User_Interests_Movies = (new FacebookRequest(
$sess, 'GET', '/me/movies'
))->execute()->getGraphObject()->asArray();
foreach($FB_User_Interests_Movies['data'] as $key) {
echo $key->name.'<br />';
}
How I resolved
Many things are in the Facebook Graph API alike this so I expanded research to ask how to get user friends and whatnot. This lead me to a YouTube video titled "Facebook PHP SDK v4: Get List of Pages Liked by User | Part 10" which was enough to explain how to achieve my question!
I make a JSON call using an API and get the following, which seems to be correctly formatted JSON:
{
"pagination": {},
"meta": {
"code": 200
},
"data": [
{
"tags": [],
"location": {
"latitude": 37.42833,
"name": "Stanford University",
"longitude": -122.1668,
"id": 10138861
},
"comments": {
"count": 0,
"data": []
},
"filter": "Rise",
"created_time": "1331327429",
"link": "http://instagr.am/p/H91ykZpqUW/",
"likes": {
"count": 3,
"data": [
{
"username": "razzles39",
"profile_picture": "http://images.instagram.com/profiles/profile_14316422_75sq_1322705511.jpg",
"id": "14316422",
"full_name": "razzles39"
},
{
"username": "mscaliti",
"profile_picture": "http://images.instagram.com/profiles/profile_10827166_75sq_1330704753.jpg",
"id": "10827166",
"full_name": "mscaliti"
},
{
"username": "mariecox",
"profile_picture": "http://images.instagram.com/profiles/profile_3987147_75sq_1324863102.jpg",
"id": "3987147",
"full_name": "Marie Cox"
}
]
},
"images": {
"low_resolution": {
"url": "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_7.jpg",
"width": 612,
"height": 612
}
},
"caption": {
"created_time": "1331327500",
"text": "Chillin with Brady at Stanford",
"from": {
"username": "nicolelainefox",
"profile_picture": "http://images.instagram.com/profiles/anonymousUser.jpg",
"id": "17982472",
"full_name": "nicolelainefox"
},
"id": "143507936292283648"
},
"type": "image",
"id": "143507334669706518_17982472",
"user": {
"username": "nicolelainefox",
"website": "",
"bio": "",
"profile_picture": "http://images.instagram.com/profiles/anonymousUser.jpg",
"full_name": "nicolelainefox",
"id": "17982472"
}
}
]
}
I then use json_decode in my PHP script to make it something I can manipulate, and this is the output of var_dump
object(stdClass)#1 (3) { ["pagination"]=> object(stdClass)#2 (0) { } ["meta"]=> object(stdClass)#3 (1) { ["code"]=> int(200) } ["data"]=> array(1) { [0]=> object(stdClass)#4 (12) { ["tags"]=> array(0) { } ["location"]=> object(stdClass)#5 (4) { ["latitude"]=> float(37.42833) ["name"]=> string(19) "Stanford University" ["longitude"]=> float(-122.1668) ["id"]=> int(10138861) } ["comments"]=> object(stdClass)#6 (2) { ["count"]=> int(0) ["data"]=> array(0) { } } ["filter"]=> string(4) "Rise" ["created_time"]=> string(10) "1331327429" ["link"]=> string(31) "http://instagr.am/p/H91ykZpqUW/" ["likes"]=> object(stdClass)#7 (2) { ["count"]=> int(3) ["data"]=> array(3) { [0]=> object(stdClass)#8 (4) { ["username"]=> string(9) "razzles39" ["profile_picture"]=> string(73) "http://images.instagram.com/profiles/profile_14316422_75sq_1322705511.jpg" ["id"]=> string(8) "14316422" ["full_name"]=> string(9) "razzles39" } [1]=> object(stdClass)#9 (4) { ["username"]=> string(8) "mscaliti" ["profile_picture"]=> string(73) "http://images.instagram.com/profiles/profile_10827166_75sq_1330704753.jpg" ["id"]=> string(8) "10827166" ["full_name"]=> string(8) "mscaliti" } [2]=> object(stdClass)#10 (4) { ["username"]=> string(8) "mariecox" ["profile_picture"]=> string(72) "http://images.instagram.com/profiles/profile_3987147_75sq_1324863102.jpg" ["id"]=> string(7) "3987147" ["full_name"]=> string(9) "Marie Cox" } } } ["images"]=> object(stdClass)#11 (3) { ["low_resolution"]=> object(stdClass)#12 (3) { ["url"]=> string(79) "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_6.jpg" ["width"]=> int(306) ["height"]=> int(306) } ["thumbnail"]=> object(stdClass)#13 (3) { ["url"]=> string(79) "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_5.jpg" ["width"]=> int(150) ["height"]=> int(150) } ["standard_resolution"]=> object(stdClass)#14 (3) { ["url"]=> string(79) "http://distilleryimage6.s3.amazonaws.com/4c6f97c46a2c11e180c9123138016265_7.jpg" ["width"]=> int(612) ["height"]=> int(612) } } ["caption"]=> object(stdClass)#15 (4) { ["created_time"]=> string(10) "1331327500" ["text"]=> string(30) "Chillin with Brady at Stanford" ["from"]=> object(stdClass)#16 (4) { ["username"]=> string(14) "nicolelainefox" ["profile_picture"]=> string(54) "http://images.instagram.com/profiles/anonymousUser.jpg" ["id"]=> string(8) "17982472" ["full_name"]=> string(14) "nicolelainefox" } ["id"]=> string(18) "143507936292283648" } ["type"]=> string(5) "image" ["id"]=> string(27) "143507334669706518_17982472" ["user"]=> object(stdClass)#17 (6) { ["username"]=> string(14) "nicolelainefox" ["website"]=> string(0) "" ["bio"]=> string(0) "" ["profile_picture"]=> string(54) "http://images.instagram.com/profiles/anonymousUser.jpg" ["full_name"]=> string(14) "nicolelainefox" ["id"]=> string(8) "17982472" } } } }
However, when I use echo($instagram_data["data"]); everything crashes. How do I access the 'data' array in this associative array? Here's the whole code:
$instagram_handler = fopen("https://api.instagram.com/v1/locations/10138861/media/recent/?client_id=MY_ID", "r");
$instagram_json = stream_get_contents($instagram_handler);
fclose($instagram_handler);
$instagram_data = json_decode($instagram_json);
echo($instagram_data["data"]); //Breaks page
the JSON is read as an object, not an associative array. load it like so and you'll be good:
$instagram_data = json_decode($instagram_json, TRUE);
alternatively, loading it like you do currently, run:
echo $instagram_data->data
Note the assoc param of json_decode function. If you omit it decode treats JSON as an object rather than an array. So you should be able access it with:
$instagram_data->data;
Other option is to decode as array as following:
$instagram_data = json_decode($instagram_json, TRUE);