I'm working with the twitter API to retrieve all of my tweets. However, I can't seem to get the "expanded_url" and "hashtag" properties. The documentation for this particular API can be found at https://dev.twitter.开发者_如何转开发com/docs/api/1/get/statuses/user_timeline. My code is as follows:
$retweets = 'http://api.twitter.com/1/statuses/user_timeline.json? include_entities=true&include_rts=true&screen_name=callmedan';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $retweets);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlout = curl_exec($ch);
curl_close($ch);
$response = json_decode($curlout, true);
$tweet_number = count($response);
for($i = 0;$i < $tweet_number;$i++)
{
$url = $response['entities']['urls'];
$hashtag = $response['entities']['hashtags'];
$text = $response[$i]['text'];
echo "$url <br />";
echo "$hashtag <br />";
echo "$text <br />";
echo "<br /><br />";
}
I get an error message reading "Notice: Undefined index: entities."
Any suggestions?
You should do (if $response is an array you must access the proper index):
$url = $response[$i]['entities']['urls'];
$hashtag = $response[$i]['entities']['hashtags'];
$text = $response[$i]['text'];
Otherwise use foreach:
foreach ($response as $r){
$url = $r['entities']['urls'];
$hashtag = $r['entities']['hashtags'];
$text = $r['text'];
You're using an integer-incremented for loop, but not utilizing the $i
index. Instead, use a foreach
:
foreach($response as $tweet)
{
$url = $tweet['entities']['urls'];
$hashtag = $tweet['entities']['hashtags'];
$text = $tweet['text'];
echo "$url <br />";
echo "$hashtag <br />";
echo "$text <br />";
echo "<br /><br />";
}
精彩评论