skip to Main Content

I have a working PHP app running in Bluemix that I want to extend to call a RESTful service (Insights for Twitter). I’ve been able to call the service, retrieve the json body, and use json_decode as follows to create an array:

$insightList = json_decode($guzzleResponse ->getBody(), true);

However, I can’t figure out how to get access to the field I’m interested in. I’ve searched for solution on the web and tried a few approaches that looked promising, but when I tried to integrate them, I couldn’t get them to work. I’m a bit of a PHP novice, so if something doesn’t work I’m not sure how to proceed.

The json structure is quite complex, with three top-level arrays – search, tweets, and next. I’m interested in the second of these, tweets. It’s a pretty complex array – there is one entry per tweet. The field I want right now is tweets.cde.message.body. You can find the full schema here: https://cdeservice.eu-gb.mybluemix.net/rest-api/#!/messages/getTweets

This is the code I have so far:

 foreach($insightList as $cde) {
    foreach($cde as $message) {
    $insight = $message['body'];
    if(strlen($insight) > 60) {
      $posts[] = array(
      'id' => 99999999,
      //Temp; remove links from the text
      'text' => $insight,
      'category' => $insightCategory,
      'image' => 'false'
    );        
  }
 }
}

Here’s a print_r of $insightList:

    [search] => Array (
    [results] => 28
    [current] => 28
    )
[tweets] => Array (
    [0] => Array (
        [cde] => Array (
            [author] => Array (
                [gender] => male
                [parenthood] => Array (
                    [isParent] => unknown
                    )
                [location] => Array (
                    [country] => 
                    )
                [maritalStatus] => Array (
                    [isMarried] => unknown
                    )
                )
            [content] => Array (
                [sentiment] => Array (
                    [evidence] => Array ( )
                    [polarity] => NEUTRAL
                    )
                )
            )
        [message] => Array (
            [postedTime] => 2015-01-13T09:42:16.000Z
            [verb] => share
            [link] => http://twitter.com/zWDOM/statuses/554936456477933569
            [generator] => Array (
                [displayName] => Twitter Web Client
                [link] => http://twitter.com
                )
            [body] => RT @VisualSuccess: "Mainframe & Cloud" Magazine wurde soeben publiziert! http://www.twitter.com  @zWDOM @Rocket @JohnKnutson_IBM
            [favoritesCount] => 0
            [objectType] => activity
            [actor] => Array (
                [summary] => Seit über 25 Jahren im Mainframebereich, derzeit als Senior Consultant und IT Architekt für zEnterprise und Projektmanager beim IBM BP Cancom in Köln
                [image] => https://pbs.twimg.com/profile_images/424202233463308288/XQquUcnh_normal.jpeg
                [statusesCount] => 2309
                [utcOffset] => 3600
                [languages] => Array (
                    [0] => de
                    )
                [preferredUsername] => zWDOM
                [displayName] => Willi Domroese
                [postedTime] => 2009-12-17T01:39:25.000Z
                [link] => http://www.twitter.com/zWDOM
                [verified] => 
                )
            [provider] => Array (
                [displayName] => Twitter
                [link] => http://www.twitter.com
                [objectType] => service
                )
            [twitter_filter_level] => medium
            [twitter_entities] => Array (
                [urls] => Array (
                    [0] => Array (
                        [display_url] => ln.is/paper.li/visua…
                        [indices] => Array (
                            [0] => 77
                            [1] => 99
                            )
                        [expanded_url] => http://ln.is/paper.li/visualsucce/7zYNk
                        [url] => http://www.twitter.com
                        )
                    )
                [hashtags] =>  Array ( )
                [user_mentions] => Array (
                    [0] => Array (
                        [indices] => Array (
                            [0] => 3
                            [1] => 17
                            )
                        [screen_name] => VisualSuccess
                        [id_str] => 213337792
                        [name] => Predrag Gasic
                        [id] => 213337792
                        )
                    [1] => Array (
                        [indices] => Array (
                            [0] => 101
                            [1] => 107
                            )
                        [screen_name] => zWDOM
                        [id_str] => 97334013
                        [name] => Willi Domroese
                        [id] => 97334013
                        )
                    [2] => Array (
                        [indices] => Array (
                            [0] => 108
                            [1] => 115
                            )
                        [screen_name] => Rocket
                        [id_str] => 870584947
                        [name] => Rocket Software
                        [id] => 870584947
                        )
                    [3] => Array (
                        [indices] => Array (
                            [0] => 116
                            [1] => 132
                            )
                        [screen_name] => JohnKnutson_IBM
                        [id_str] => 16452310
                        [name] => John Knutson
                        [id] => 16452310
                        )
                    )
                [trends] =>  Array ( )
                [symbols] =>  Array ( )
                )
            [twitter_lang] => de
            [id] => tag:search.twitter.com,2005:554936456477933569
            [retweetCount] => 1
            [gnip] => Array (
                [urls] => Array (
                    [0] => Array (
                        [expanded_url] => http://linkis.com/paper.li/visualsucce/7zYNk
                        [expanded_status] => 200
                        [url] => http://www.twitter.com
                        )
                    )
                [language] => Array (
                    [value] => de
                    )
                )
            [object] => Array (
                [postedTime] => 2015-01-13T08:04:48.000Z
                [verb] => post
                [link] => http://twitter.com/VisualSuccess/statuses/554911928527888384
                [generator] => Array (
                    [displayName] => Linkis.com
                    [link] => http://linkis.com
                    )
                [body] => "Mainframe & Cloud" Magazine wurde soeben publiziert! http://www.twitter.com ://www.twitter.com
                [objectType] => activity
                [actor] => Array (
                    [summary] => Ù† (N), Wirtschaftsinformatiker | SAP Consultant Logistics #bigdata #appdevelopment #webdesign #eCommerce #SocialMedia #contentmarketing #SmartHome #Journal
                    [image] => https://pbs.twimg.com/profile_images/2841607223/959b0d23646b1f24bd7b70deac160e2f_normal.jpeg
                    [statusesCount] => 14185
                    [utcOffset] => 3600
                    [languages] => Array (
                        [0] => de
                        )
                    [preferredUsername] => VisualSuccess
                    [displayName] => Predrag Gasic
                    [postedTime] => 2010-11-08T17:19:27.000Z
                    [link] => http://www.twitter.com/VisualSuccess
                    [verified] => 
                    )
                [provider] => Array (
                    [displayName] => Twitter
                    [link] => http://www.twitter.com
                    [objectType] => service
                    )
                [twitter_filter_level] => low
                [twitter_entities] => Array (
                    [urls] => Array (
                        [0] => Array (
                            [display_url] => ln.is/paper.li/visua…
                            [indices] => Array (
                                [0] => 58
                                [1] => 80
                                )
                            [expanded_url] => http://ln.is/paper.li/visualsucce/7zYNk
                            [url] => http://www.twitter.com
                            )
                        )
                    [hashtags] =>  Array ( )
                    [user_mentions] => Array (
                        [0] => Array (
                            [indices] => Array (
                                [0] => 82
                                [1] => 88
                                )
                            [screen_name] => zWDOM
                            [id_str] => 97334013
                            [name] => Willi Domroese
                            [id] => 97334013
                            )
                        [1] => Array (
                            [indices] => Array (
                                [0] => 89
                                [1] => 96
                                )
                            [screen_name] => Rocket
                            [id_str] => 870584947
                            [name] => Rocket Software
                            [id] => 870584947
                            )
                        [2] => Array (
                            [indices] => Array (
                                [0] => 97
                                [1] => 113
                                )
                            [screen_name] => JohnKnutson_IBM
                            [id_str] => 16452310
                            [name] => John Knutson
                            [id] => 16452310
                            )
                        )
                    [trends] =>  Array ( )
                    [symbols] =>  Array (
                    )
                )
            )
        )
    )

Answers to this question would be greatly appreciated

2

Answers


  1. Iterating $insightList will not get you to the cde level. And you don’t actually want the cde; you want the message.

    Just iterate the tweets. You don’t need a foreach to get keyed info from a PHP array.

    foreach($insightList['tweets'] as $tweet) {
            $insight = $tweet['message']['body'];
            if(strlen($insight) > 60) {
                $posts[] = array(
                'id' => 99999999,
                //Temp; remove links from the text
                'text' => $insight,
                'category' => $insightCategory,
                'image' => 'false'
            );        
        }
    }
    
    Login or Signup to reply.
  2. It looks to me from your print_r that what you want is at:

    echo $insightList[0]['message']['body'];
    

    When there’s a section like this [some_key], then ‘some_key’ is an associative array key, and you can access those elements be they additional arrays or a value, using the key name, as I illustrated here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search