/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] cURL: Trouble parsing stdClass objects

I’ve been trying to parse this for a couple of days now. I’m trying to access a series of objects from facebook using the ids URL parameter.

sample of actual url for use in cURL

[code=html]
https://graph.facebook.com/?ids=1234,12345,123456
[/code]

sample_json.php (sample of response):

[code=php]
<?php
header(“Content-type: application/x-javascript”);
echo ‘
{
“63811549237_216358615102004”: {
“id”: “63811549237_216358615102004”,
“from”: {
“name”: “The White House”,
“category”: “Government organization”,
“id”: “63811549237”
},
“message”: “Over the next five years, over a million servicemembers will transition back into civilian life. “We ask these men and women to leave their families and their jobs and risk their lives to fight for our country. The last thing they should have to do is fight for a job when they get home.” The Returning Heroes Tax Credit was passed in the Senate on Thursday, and the House should pass this bill as soon as possible so President Obama can sign it into law.”,
“picture”: “http://external.ak.fbcdn.net/safe_image.php?d=AQBtZWghSj-h_PnL&w=130&h=130&url=httpu00253Au00252Fu00252Fi2.ytimg.comu00252Fviu00252F–QxAtXZJpQu00252Fhqdefault.jpg”,
“link”: “http://youtu.be/–QxAtXZJpQ”,
“source”: “http://www.youtube.com/v/–QxAtXZJpQ?version=3&autohide=1&autoplay=1”,
“name”: “Weekly Address: Honoring Our Veterans for Their Service and Sacrifice”,
“caption”: “www.youtube.com”,
“icon”: “http://static.ak.fbcdn.net/rsrc.php/v1/yj/r/v2OnaTyTQZE.gif”,
“actions”: [
{
“name”: “Comment”,
“link”: “http://www.facebook.com/63811549237/posts/216358615102004”
},
{
“name”: “Like”,
“link”: “http://www.facebook.com/63811549237/posts/216358615102004”
}
],
“type”: “video”,
“created_time”: “2011-11-13T15:01:30+0000”,
“updated_time”: “2011-11-13T21:07:11+0000”,
“likes”: {
“data”: [
{
“name”: “J Robert Clm”,
“id”: “1661901712”
}
],
“count”: 1220
},
“comments”: {
“data”: [
{
“id”: “63811549237_216358615102004_1886046”,
“from”: {
“name”: “Patricia Kanan”,
“id”: “100003141099706”
},
“message”: “Random Poll…..rnHow many of you had 10 friends in High School?”,
“created_time”: “2011-11-13T20:00:43+0000”,
“likes”: 2
},
{
“id”: “63811549237_216358615102004_1886328”,
“from”: {
“name”: “Patricia Kanan”,
“id”: “100003141099706”
},
“message”: “Bioterrorism, get it? Warfare.rnWe have to stockpile vaccines in case someone uses it to spread an epidemic.”,
“created_time”: “2011-11-13T21:06:33+0000”
},
{
“id”: “63811549237_216358615102004_1886330”,
“from”: {
“name”: “Henya Friedman”,
“id”: “100000476074760”
},
“message”: “God bless our Pres. And our military, Thank you for the service:) BBL”,
“created_time”: “2011-11-13T21:07:11+0000”,
“likes”: 1
}
],
“count”: 1221
}
},
“76625396025_10150379083971026”: {
“id”: “76625396025_10150379083971026”,
“from”: {
“name”: “Anthony Celi”,
“id”: “100002396146406”
},
“to”: {
“data”: [
{
“name”: “CDC”,
“category”: “Government organization”,
“id”: “76625396025”
}
]
},
“message”: “Was it real that you quarantined a building because of an infection?”,
“actions”: [
{
“name”: “Comment”,
“link”: “http://www.facebook.com/76625396025/posts/10150379083971026”
},
{
“name”: “Like”,
“link”: “http://www.facebook.com/76625396025/posts/10150379083971026”
}
],
“type”: “status”,
“created_time”: “2011-11-12T21:02:21+0000”,
“updated_time”: “2011-11-12T21:02:21+0000”,
“comments”: {
“count”: 0
}
},
“76625396025_10150377012826026”: {
“id”: “76625396025_10150377012826026”,
“from”: {
“name”: “Sheila Martin”,
“id”: “100000104130795”
},
“to”: {
“data”: [
{
“name”: “CDC”,
“category”: “Government organization”,
“id”: “76625396025”
}
]
},
“message”: “WHY would the Director of the CDC say that J&J’s patches and gums work when they don’t? Follow the money.”,
“actions”: [
{
“name”: “Comment”,
“link”: “http://www.facebook.com/76625396025/posts/10150377012826026”
},
{
“name”: “Like”,
“link”: “http://www.facebook.com/76625396025/posts/10150377012826026”
}
],
“type”: “status”,
“created_time”: “2011-11-11T15:08:31+0000”,
“updated_time”: “2011-11-11T15:08:31+0000”,
“likes”: {
“data”: [
{
“name”: “Sheila Martin”,
“id”: “100000104130795”
}
],
“count”: 1
},
“comments”: {
“count”: 0
}
},
“20531316728_263325323713845”: {
“id”: “20531316728_263325323713845”,
“from”: {
“name”: “Facebook”,
“category”: “Product/service”,
“id”: “20531316728”
},
“message”: “Weu2019d like to take the opportunity to thank the millions of active duty service members, veterans and military families who sacrifice so much in service of their country. Today we are announcing a series of programs we are launching to help service members and their families stay connected though Facebook. Take a minute to thank military personnel and families at http://on.fb.me/nationgivesthanks.”,
“picture”: “http://external.ak.fbcdn.net/safe_image.php?d=AQDDb3boOkEq3r1c&w=90&h=90&url=httpu00253Au00252Fu00252Fprofile.ak.fbcdn.netu00252Fhprofile-ak-snc4u00252F373589_175817209171249_189152508_q.jpg”,
“link”: “http://on.fb.me/nationgivesthanks”,
“name”: “U.S. Military on Facebook”,
“caption”: “www.facebook.com”,
“description”: “Please check out our tabs on the left for more information about how you can thank the U.S. military, find job resources, and use CNNu2019s iReport to share messages with veterans and their families.”,
“icon”: “http://static.ak.fbcdn.net/rsrc.php/v1/yD/r/aS8ecmYRys0.gif”,
“actions”: [
{
“name”: “Comment”,
“link”: “http://www.facebook.com/20531316728/posts/263325323713845”
},
{
“name”: “Like”,
“link”: “http://www.facebook.com/20531316728/posts/263325323713845”
}
],
“type”: “link”,
“created_time”: “2011-11-10T01:38:48+0000”,
“updated_time”: “2011-11-13T20:57:48+0000”,
“likes”: {
“data”: [
{
“name”: “Cee Stockton”,
“id”: “1500138924”
}
],
“count”: 24840
},
“comments”: {
“data”: [
{
“id”: “20531316728_263325323713845_3433211”,
“from”: {
“name”: “Sandra Dopler”,
“id”: “100001451268376”
},
“message”: “Today at the Memorial Methodist Church (in Lake Placid Florida) we said THANK YOU to our veterans. Three of the men in our Choir sang all of the Military anthems and the men and women stood up when their’s was sang. Beautiful and very emotional!!!”,
“created_time”: “2011-11-13T15:24:55+0000”
},
{
“id”: “20531316728_263325323713845_3434707”,
“from”: {
“name”: “Julia Perez”,
“id”: “100000018036692”
},
“message”: “DO SOMETHING ABOUT ALL THE PORN PLEASE!!!!!!!!!!!!!!!!!!”,
“created_time”: “2011-11-13T20:57:48+0000”
}
],
“count”: 2005
}
}
}

‘;
?>
[/code]

parse_it.php

[code=php]
<?php
$fbwallURL = ‘http://localhost/fullpath/sample_json.php’;
$ch = curl_init($fbwallURL);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resultsJson = curl_exec($ch);
curl_close($ch);

$results = json_decode($resultsJson);

print_r($results);
?>
[/code]

this function doesn’t help much…

[code=php]
function printObject($obj) {
foreach (get_object_vars($obj) as $field => $value) {
if (is_object($value)) {
printObject($value);
} else {
print $field . ‘ – ‘ . $value . ‘<br />’;

if ($field == ‘message’){echo ‘Message: ‘.$value;}

}
}
}
[/code]

Any help appreciated.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 14.2011 — My first recommendation would be to add the optional 2nd parameter to json_decode() and set it to true, so that everything is an array, and then you don't have to mess with objects. ?
Copy linkTweet thisAlerts:
@bustyaauthorNov 14.2011 — Thank you sir, I wish I would have asked yesterday...

Now I can access each object like so:

[code=php]
$fbwallURL = 'http://localhost/fullpath/sample_json.php';
$ch = curl_init($fbwallURL);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$resultsJson = curl_exec($ch);
curl_close($ch);

$results = json_decode($resultsJson, true);
foreach ($results as $result)
{
echo 'user name: '.$result['from']['name'].'<br />';
echo 'user id: '.$result['from']['id'].'<br />';
echo 'user category: '.$result['from']['category'].'<br />';
}

[/code]
×

Success!

Help @bustya spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...