-1

I'm having some trouble retrieving the "title" and "url" fields from this JSON output. I have done this in the past, but never from such a complex hierarchy. Here is what I have tried.

Code block that matters:

$response_raw = curl_exec( $ch );
$response = json_decode( $response_raw );
curl_close( $ch );

print_r ($response);

echo $response->data->title;  // NOT CORRECT
echo $response->data->url;  // NOT CORRECT

Giant JSON block:

(
    [kind] => Listing
    [data] => stdClass Object
        (
            [modhash] => 
            [dist] => 11
            [children] => Array
                (
                    [0] => stdClass Object
                        (
                            [kind] => t3
                            [data] => stdClass Object
                                (
                                    [approved_at_utc] => 
                                    [subreddit] => freebies
                                    [selftext] => 
                                    [author_fullname] => t2_100cy6
                                    [saved] => 
                                    [mod_reason_title] => 
                                    [gilded] => 0
                                    [clicked] => 
                                    [title] => Send a Halloween card to a child at Children's Hospital LA
                                    [link_flair_richtext] => Array
                                        (
                                        )

                                    [subreddit_name_prefixed] => r/freebies
                                    [hidden] => 
                                    [pwls] => 6
                                    [link_flair_css_class] => 
                                    [downs] => 0
                                    [parent_whitelist_status] => all_ads
                                    [hide_score] => 
                                    [name] => t3_9sjqk2
                                    [quarantine] => 
                                    [link_flair_text_color] => dark
                                    [author_flair_background_color] => 
                                    [subreddit_type] => public
                                    [ups] => 456
                                    [domain] => secure1.chla.org
                                    [media_embed] => stdClass Object
                                        (
                                        )

                                    [author_flair_template_id] => 
                                    [is_original_content] => 
                                    [user_reports] => Array
                                        (
                                        )

                                    [secure_media] => 
                                    [is_reddit_media_domain] => 
                                    [is_meta] => 
                                    [category] => 
                                    [secure_media_embed] => stdClass Object
                                        (
                                        )

                                    [link_flair_text] => 
                                    [can_mod_post] => 
                                    [score] => 456
                                    [approved_by] => 
                                    [thumbnail] => 
                                    [edited] => 
                                    [author_flair_css_class] => 
                                    [author_flair_richtext] => Array
                                        (
                                        )

                                    [gildings] => stdClass Object
                                        (
                                            [gid_1] => 0
                                            [gid_2] => 0
                                            [gid_3] => 0
                                        )

                                    [content_categories] => 
                                    [is_self] => 
                                    [mod_note] => 
                                    [created] => 1540894163
                                    [link_flair_type] => text
                                    [wls] => 6
                                    [banned_by] => 
                                    [author_flair_type] => text
                                    [contest_mode] => 
                                    [selftext_html] => 
                                    [likes] => 
                                    [suggested_sort] => confidence
                                    [banned_at_utc] => 
                                    [view_count] => 
                                    [archived] => 
                                    [no_follow] => 
                                    [is_crosspostable] => 1
                                    [pinned] => 
                                    [over_18] => 
                                    [media_only] => 
                                    [link_flair_template_id] => 
                                    [can_gild] => 1
                                    [spoiler] => 
                                    [locked] => 
                                    [author_flair_text] => 
                                    [visited] => 
                                    [num_reports] => 
                                    [distinguished] => 
                                    [subreddit_id] => t5_2qi1v
                                    [mod_reason_by] => 
                                    [removal_reason] => 
                                    [link_flair_background_color] => 
                                    [id] => 9sjqk2
                                    [is_robot_indexable] => 1
                                    [report_reasons] => 
                                    [author] => kushmaster10
                                    [num_crossposts] => 0
                                    [num_comments] => 45
                                    [send_replies] => 1
                                    [mod_reports] => Array
                                        (
                                        )

                                    [author_patreon_flair] => 
                                    [author_flair_text_color] => 
                                    [permalink] => /r/freebies/comments/9sjqk2/send_a_halloween_card_to_a_child_at_childrens/
                                    [whitelist_status] => all_ads
                                    [stickied] => 
                                    [url] => https://secure1.chla.org/site/SPageNavigator/Halloween2018.html;jsessionid=00000000.app205b?utm_source=em3&utm_medium=email&utm_campaign=hall18&s_src=hall18em3&NONCE_TOKEN=8BF78C172D2DE329607E0BE46623F05C
                                    [subreddit_subscribers] => 609487
                                    [created_utc] => 1540865363
                                    [media] => 
                                    [is_video] => 
                                )

                        )

Any help on retrieving these 2 variables is much appreciated.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
Wyatt Jackson
  • 303
  • 1
  • 2
  • 11
  • 1
    After `$response->data` the only valid selections are `->modhash`, `->dist`, or `->children`. If you want the title for the first child, then do `$response->data->children[0]->data->title` – GrumpyCrouton Oct 30 '18 at 20:48
  • 3
    You've learned to pretty print the json so you can actually see the hiarchy right there. To get the URL you just start at the $response and then drill down. $response->data->children[0]->data->url. BUT! You probably get more children so you should iterate over $reponse->data->children and then get $child->data->url for each "row" – JimL Oct 30 '18 at 20:49
  • 1
    Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – mickmackusa Oct 30 '18 at 22:26

1 Answers1

3

Your current code is attempting to retrieve those variable from the first data object, but you need to retrieve them from the data object that is nested further down. Your calls should look something like this:

echo $response->data->children[0]->data->title;
echo $response->data->children[0]->data->url;

As others have stated, it is likely that you will have a scenario where more than one "child" is included in the "children" array. To iterate through multiple children, you could use something like this:

foreach($response->data->children as $child){
    $childTitle = $child->data->title;
    $childURL = $child->data->url;
    //do something with these values
};
Justin T.
  • 826
  • 1
  • 5
  • 13
  • Yes!, you are correct. I also needed to loop through the results. I tried this, and it worked perfectly. Thank you so much for your help. – Wyatt Jackson Oct 30 '18 at 23:23