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?
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 ;