简体   繁体   中英

Why is Twitter's 1.1 API not including entities in my tweets?

Forgive the ignorance if I'm missing something fundamental here but I'm trying to print the last tweet to a client's homepage after updating their twitter API call to 1.1 and I'm able to do that, but it's not including entities by default it seems. It spits out plain text without links around hashtags and URLs. Am I missing something here?

require_once 'twitteroauth.php';
 $twitterConnection = new TwitterOAuth(
                'XXX',  // Consumer Key
                'XXX',      // Consumer secret
                'XXX',       // Access token
                'XXX'       // Access token secret
                );
 $twitterData = $twitterConnection->get(
                  'statuses/user_timeline',
                  array(
                    'screen_name'     => 'XXX',
                    'count'           => 1,
                    'exclude_replies' => true
                  )
                );
 if($twitterConnection->http_code != 200)
 {
      $twitterData = get_transient($transName);
 }
 // Save our new transient.
 set_transient($transName, $twitterData, 60 * $cacheTime);

 foreach($twitterData as $tweets)
{
    return $tweets->text;
}

Mostly because that's what you tell it to do at the line you write return $tweets->text;

text is pretty much just the string value of the tweet. If you wanted the hashtags linkified, you'd have to iterate over the values of $tweets->hashtags . It's an array of objects, where $tweets->hastags[0]->text is the text value of the first hashtag present in the tweet and $tweets->hashtags[0]->indices returns an array of the form [9,15] that specifies the position(start, stop) of the hashtag and it's text value in the tweet.

Here's some pseudocode to show how your request could be handled:

foreach($twitterData as $tweet) {
    $text = $tweet->text;
    foreach($tweet->hashtag as $hashtag) {
        $searchString = $hashtag->text;
        $text = linkify($text,$searchString,$hashtag->indices);
    }
}

Where linkify simply adds something similar to <a href="http://twitter.com/search?q='$searchString&src=hash'"> and </a> , depending on how you want to handle links, at positions indices[0] and indices[1] respectively.

Of course, you could argue that it'd be much simpler to use a regex to filter using #(a-z_)\\w+ as described here https://stackoverflow.com/a/15540967/1882885 , replacing it with a url of similar form with what I wrote above.

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM