0

This is my code:

$latesttweets = $twitterconn>get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitter_id."&count=".$limit);


foreach ( $latesttweets as $tweet ){
   ...


   $text_url = $tweet->entities->urls[0]->url; // this line gives an error

   // test
   echo $text_url; // this line doesn't give an error



   ... code for display tweet ...
}

I'm trying to retrieve the url from the object:

"urls": [{
  "url": "https:\/\/t.co\/XdXRudPXH5",
  "expanded_url": "https:\/\/blog.twitter.com\/2013\/rich-photo-experience-now-in-embedded-tweets-3",
  "display_url": "blog.twitter.com\/2013\/rich-phot\u2026",
  "indices": [80, 103]
}],

as shown here.

The funny thing is, I've tried echoing and it works, except that the error Notice: Undefined offset: 0 and Notice: Trying to get property of non-object which follows on the next line keeps appearing.

(Note: The same goes for all the other arrays in the urls array. I'm kind of sure it's something to do with the ..->urls[0]->.., but I see no other way.)

Am I doing something wrong?

Bobby
  • 242
  • 3
  • 12
  • is `$tweet` is json decoded, what does `var_dump($tweet)` shows – Abhik Chakraborty May 02 '14 at 11:59
  • Returns the twitter object with no problems. As shown in the [docs](https://dev.twitter.com/docs/platform-objects/tweets). The problem is the error messages are showing **even though** I am able to retrieve the values. – Bobby May 02 '14 at 12:33

1 Answers1

1

Ok the issue is urls[0]-> not a valid object in PHP and you should use

$text_url = $tweet->entities->urls{'0'}->url;

instead.

However in PHP 5+ the urls[0]-> should work

Here is the example.

$str = '{
    "text": "Four more years. http://t.co/bAJE6Vom",
    "entities": {
        "hashtags": [],
        "symbols": [],
        "urls": [
            {
                "url": "https://t.co/XdXRudPXH5",
                "expanded_url": "https://blog.twitter.com/2013/rich-photo-experience-now-in-embedded-tweets-3",
                "display_url": "blog.twitter.com/2013/rich-phot…",
                "indices": [
                    80,
                    103
                ]
            }
        ],
        "user_mentions": [],
        "media": [
            {
                "id": 266031293949698050,
                "id_str": "266031293949698048",
                "indices": [
                    17,
                    37
                ],
                "media_url": "http://pbs.twimg.com/media/A7EiDWcCYAAZT1D.jpg",
                "media_url_https": "https://pbs.twimg.com/media/A7EiDWcCYAAZT1D.jpg",
                "url": "http://t.co/bAJE6Vom",
                "display_url": "pic.twitter.com/bAJE6Vom",
                "expanded_url": "http://twitter.com/BarackObama/status/266031293945503744/photo/1",
                "type": "photo",
                "sizes": {
                    "medium": {
                        "w": 600,
                        "h": 399,
                        "resize": "fit"
                    },
                    "thumb": {
                        "w": 150,
                        "h": 150,
                        "resize": "crop"
                    },
                    "small": {
                        "w": 340,
                        "h": 226,
                        "resize": "fit"
                    },
                    "large": {
                        "w": 800,
                        "h": 532,
                        "resize": "fit"
                    }
                }
            }
        ]
    }
}

';

$tweet = json_decode($str);
$text_url = $tweet->entities->urls{0}->url;
echo $text_url ;
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
  • I did some searching and found [this](http://stackoverflow.com/questions/9477297/get-image-url-from-twitter-api-entity-parameter-php). But it seems he is having no problems. Weird. I'm not sure how you would loop the tweets stored within the twitter object using your way. – Bobby May 02 '14 at 13:00
  • well looping would be easy `foreach($tweet->entities->urls as $key=>$val){ echo $val->url ; }` – Abhik Chakraborty May 02 '14 at 13:04
  • Sorry if I wasn't clear. what I meant was retrieving Tweets from Twitter. Not looping the entity array. I've edited my question with the code in detail. – Bobby May 02 '14 at 13:05
  • yes you can use secondary loop as above if there are multiple urls. But if its only one then just can use as in the answer !! – Abhik Chakraborty May 02 '14 at 13:10
  • Would you mind explaining a little more on the JSON bit? The `$latesttweets` I got using the twitter API apparently isn't a `str`. I am unable to use `json_decode()`. Also, what about the part where `echoing` doesn't show an error but shows errors when defining the variable? It doesn't make sense. – Bobby May 02 '14 at 13:18
  • ok so here it is , since you are using the Twitter API the returned data is already in json decoded. So your `$latesttweets` will hold json_decoded data indexed from `0,1....` for each tweet. So when u loop through `$latesttweets` you are reading one by one tweets and they are again structured as json object and you can get the property of each of them. if some of the inner element is again having multiple emtry you need to loop through that inner element to get each data. – Abhik Chakraborty May 02 '14 at 13:22
  • Exactly my point (which I thought I was wrong). So why is this showing errors when its working? I am getting what its expected to give (something like this: http://t.co/dgv3b0wV6O), but the errors are there, which bugs me a lot! – Bobby May 02 '14 at 13:25
  • thats a notice !! as I explained below PHP 5 `urls[0]->` is considered as bad object property. Not sure which PHP version you are running at. – Abhik Chakraborty May 02 '14 at 13:27
  • Is there any way I can get rid of that? PHP 5.4, to answer your question. – Bobby May 02 '14 at 13:27
  • only thing I can see is to use `$tweet->entities->urls{'0'}->url` or do a check `if(array_key_exists(0,$tweet->entities->urls)){ $text_url = $tweet->entities->urls{'0'}->url; }` – Abhik Chakraborty May 02 '14 at 13:31
  • This is just getting more confusing. Doing a check of `array_key_exists` works (as in no **Notice**) ONLY if I `echo` it right away. If defined in a variable like your suggestion, another notice will show when `echoed` which shows :`Notice: Undefined variable: text_url`. One more note, even though the notice says `Undefined variable`, the `text` is `echo`ed without a problem. – Bobby May 02 '14 at 13:36
  • ok I would suggest just leave it how it is for now and use as `$tweet->entities->urls{'0'}->url` and also check the PHP version. in 5+ it should work the way you are doing – Abhik Chakraborty May 02 '14 at 13:39
  • Okay I managed to remove all **Notices** by adding another check `isset()` when echoing: `if ( isset($text_url) ) echo $text_url;` This is really mind boggling. I guess I'll be using this solultion for now. Thank you Abhik, you have been very patient. Greatly appreciate your help. – Bobby May 02 '14 at 13:41