代码之家  ›  专栏  ›  技术社区  ›  Wyatt Jackson

我该怎么得到这些json变量PHP注意:尝试获取非对象的属性'title'

  •  -1
  • Wyatt Jackson  · 技术社区  · 7 年前

    重要的代码块:

    $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
    

    (
        [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] => 
                                    )
    
                            )
    

    非常感谢您对检索这两个变量的任何帮助。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Justin T.    7 年前

    data 对象,但您需要从 数据

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

    foreach($response->data->children as $child){
        $childTitle = $child->data->title;
        $childURL = $child->data->url;
        //do something with these values
    };