Using PHP and the new Twitter API

白昼怎懂夜的黑 提交于 2020-01-12 06:17:26

问题


Because of the new Twitter API, I am using PHP to display 1 latest tweet on my webpage using PHP.

At the moment I have got it working so that the tweet is just outputted as a simple text string. My question is how do I control the HTML outputted? I want to be able to display the links as links if a hashtag or web address is stated within the tweet. How do I do this?

Here's my code so far that outputs the string as a tweet in my page:

function get_tweet() {

require 'tmhOAuth.php';
require 'tmhUtilities.php';

$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'secret',
'consumer_secret' => 'secret',
'user_token' => 'secret',
'user_secret' => 'secret',
'curl_ssl_verifypeer' => false
));

$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
'screen_name' => 'evanrichards',
'count' => '1'));

$response = $tmhOAuth->response['response'];
$tweets = json_decode($response, true);
echo($tweets[0]['text']);

}

回答1:


Here is some example code for replacing links, hashtags and attags with links in php

$tweet = "@george check out http://www.google.co.uk #google";

//Convert urls to <a> links
$tweet = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet);

//Convert hashtags to twitter searches in <a> links
$tweet = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet);

//Convert attags to twitter profiles in <a> links
$tweet = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet);

echo $tweet;

This gives the output

<a href="http://www.twitter.com/george">@george</a> check out <a target="_blank" href="http://www.google.co.uk">http://www.google.co.uk</a> <a target="_new" href="http://twitter.com/search?q=google">#google</a>

So you could change your code to

function get_tweet() {

  require 'tmhOAuth.php';
  require 'tmhUtilities.php';

  $tmhOAuth = new tmhOAuth(array(
  'consumer_key' => 'secret',
  'consumer_secret' => 'secret',
  'user_token' => 'secret',
  'user_secret' => 'secret',
  'curl_ssl_verifypeer' => false
  ));

  $code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
  'screen_name' => 'evanrichards',
  'count' => '1'));

  $response = $tmhOAuth->response['response'];
  $tweets = json_decode($response, true);

  $tweet = $tweets[0]['text'];
  $tweet = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet);
  $tweet = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet);
  $tweet = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet);
  echo($tweet);

}

I'm sure the regex's could be improved though.

Or even better you can then split it out into it's own function.

function linkify_tweet($tweet) {

  $tweet = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet);
  $tweet = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet);
  $tweet = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet);

  return $tweet;

}

function get_tweet() {

  require 'tmhOAuth.php';
  require 'tmhUtilities.php';

  $tmhOAuth = new tmhOAuth(array(
  'consumer_key' => 'secret',
  'consumer_secret' => 'secret',
  'user_token' => 'secret',
  'user_secret' => 'secret',
  'curl_ssl_verifypeer' => false
  ));

  $code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array(
  'screen_name' => 'evanrichards',
  'count' => '1'));

  $response = $tmhOAuth->response['response'];
  $tweets = json_decode($response, true);

  echo(linkify_tweet($tweets[0]['text']));

}



回答2:


A simple regular expression will work in most cases. But Twitter entities solve the parsing problem for you, plus give you more information about links, usernames, etc.

See The full documentation here: https://dev.twitter.com/overview/api/entities-in-twitter-objects

To format the text, get a list of all replacements you need to make, then do the replacements in reverse order (so the offsets stay correct after the text is expanded).

<?php
function twitter_format($tweet) {
    $text = $tweet['text'];
    $entities = isset($tweet['entities']) ? $tweet['entities'] : array();

    $replacements = array();
    if (isset($entities['hashtags'])) {
        foreach ($entities['hashtags'] as $hashtag) {
            list ($start, $end) = $hashtag['indices'];
            $replacements[$start] = array($start, $end,
                "<a href=\"https://twitter.com/search?q={$hashtag['text']}\">#{$hashtag['text']}</a>");
        }
    }
    if (isset($entities['urls'])) {
        foreach ($entities['urls'] as $url) {
            list ($start, $end) = $url['indices'];
            // you can also use $url['expanded_url'] in place of $url['url']
            $replacements[$start] = array($start, $end,
                "<a href=\"{$url['url']}\">{$url['display_url']}</a>");
        }
    }
    if (isset($entities['user_mentions'])) {
        foreach ($entities['user_mentions'] as $mention) {
            list ($start, $end) = $mention['indices'];
            $replacements[$start] = array($start, $end,
                "<a href=\"https://twitter.com/{$mention['screen_name']}\">@{$mention['screen_name']}</a>");
        }
    }
    if (isset($entities['media'])) {
        foreach ($entities['media'] as $media) {
            list ($start, $end) = $media['indices'];
            $replacements[$start] = array($start, $end,
                "<a href=\"{$media['url']}\">{$media['display_url']}</a>");
        }
    }

    // sort in reverse order by start location
    krsort($replacements);

    foreach ($replacements as $replace_data) {
        list ($start, $end, $replace_text) = $replace_data;
        $text = mb_substr($text, 0, $start, 'UTF-8').$replace_text.mb_substr($text, $end, NULL, 'UTF-8');
    }
    return "<p>$text</p>";
}

echo twitter_format($tweets[0]);


来源:https://stackoverflow.com/questions/15387161/using-php-and-the-new-twitter-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!