I'm trying to create a Twitter Widget for my Wordpress site, this widget get a last five tweets from my account on Twitter using Twitter API 1.1.
I'm using this code:
<?php # Load Twitter class
require_once ('twitteroauth.php');
# Define constants
define('TWEET_LIMIT', 5);
define('TWITTER_USERNAME', 'myusername');
define('CONSUMERKEY', 'XXXXXXXXXXXXXXXXXXXXXXXXX');
define('CONSUMERSECRET', 'XXXXXXXXXXXXXXXXXXXXXXXXX');
define('ACCESSTOKEN', 'XXXXXXXXXXXXXXXXXXXXXXXXX');
define('ACCESSTOKENSECRET', 'XXXXXXXXXXXXXXXXXXXXXXXXX');
# Create the connection
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
# Migrate over to SSL/TLS
$twitter->ssl_verifypeer = true;
# Load the Tweets
$tweets = $twitter->get('statuses/user_timeline', array('screen_name' => TWITTER_USERNAME, 'exclude_replies' => 'true', 'include_rts' => 'false', 'count' => TWEET_LIMIT));
# Example output
if(!empty($tweets)) {
foreach($tweets as $tweet) {
# Access as an object
$tweetText = $tweet['text'];
# Make links active
$tweetText = preg_replace("/(http://|(www.\))(([^\s<]{4,68})[^\s<]*)/", '<a href="http://$2$3" target="_blank">$1$2$4</a>', $tweetText);
# Linkify user mentions
$tweetText = preg_replace("/@(w+)/", '<a href="http://www.twitter.com/$1" target="_blank">@$1</a>', $tweetText);
# Linkify tags
$tweetText = preg_replace("/#(w+)/", '<a href="http://search.twitter.com/search?q=$1" target="_blank">#$1</a>', $tweetText);
# Output
echo $tweetText;
}
}
# Put this after fetching Tweets
$twitter = '';
# Create the HTML output
if(!empty($tweets)) {
foreach($tweets as $tweet) {
$twitter .= '<article>
<aside class="avatar">
<a href="http://twitter.com/'.$tweet['from_user'].'" target="_blank">
<img alt="'.$tweet['from_user'].'" src="'.$tweet['user']['profile_image_url'].'" />
</a>
</aside>
<p>'.$tweet['created_at'].'</p>
<p>'.$tweet['text'].'</p>
</article>';
}
} ?>
But, PHP returns nothing :(
When i see the log file (/var/apache2/logs) have these lines:
[Wed Apr 16 11:41:17.335808 2014] [:error] [pid 1983] [client 127.0.0.1:XXXX] PHP Notice: Use of undefined constant CONSUMER_KEY - assumed 'CONSUMER_KEY' in /home/user/websites/html/dev_twitterwidget/index.php on line 14
[Wed Apr 16 11:41:17.335870 2014] [:error] [pid 1983] [client 127.0.0.1:XXXX] PHP Notice: Use of undefined constant CONSUMER_SECRET - assumed 'CONSUMER_SECRET' in /home/user/websites/html/dev_twitterwidget/index.php on line 14
[Wed Apr 16 11:41:17.335878 2014] [:error] [pid 1983] [client 127.0.0.1:XXXX] PHP Notice: Use of undefined constant ACCESS_TOKEN - assumed 'ACCESS_TOKEN' in /home/user/websites/html/dev_twitterwidget/index.php on line 14
[Wed Apr 16 11:41:17.335885 2014] [:error] [pid 1983] [client 127.0.0.1:XXXX] PHP Notice: Use of undefined constant ACCESS_TOKEN_SECRET - assumed 'ACCESS_TOKEN_SECRET' in /home/user/websites/html/dev_twitterwidget/index.php on line 14
[Wed Apr 16 11:41:18.637099 2014] [:error] [pid 1983] [client 127.0.0.1:XXXX] PHP Notice: Undefined index: text in /home/user/websites/html/dev_twitterwidget/index.php on line 27
[Wed Apr 16 11:41:18.642361 2014] [:error] [pid 1983] [client 127.0.0.1:XXXX] PHP Warning: preg_replace(): Unknown modifier '/' in /home/user/websites/html/dev_twitterwidget/index.php on line 30
[Wed Apr 16 11:41:18.642834 2014] [:error] [pid 1983] [client 127.0.0.1:XXXX] PHP Notice: Undefined index: from_user in /home/user/websites/html/dev_twitterwidget/index.php on line 52
[Wed Apr 16 11:41:18.642905 2014] [:error] [pid 1983] [client 127.0.0.1:XXXX] PHP Notice: Undefined index: from_user in /home/user/websites/html/dev_twitterwidget/index.php on line 53
[Wed Apr 16 11:41:18.642913 2014] [:error] [pid 1983] [client 127.0.0.1:XXXX] PHP Notice: Undefined index: user in /home/user/websites/html/dev_twitterwidget/index.php on line 53
[Wed Apr 16 11:41:18.642919 2014] [:error] [pid 1983] [client 127.0.0.1:XXXX] PHP Notice: Undefined index: created_at in /home/user/websites/html/dev_twitterwidget/index.php on line 56
[Wed Apr 16 11:41:18.642925 2014] [:error] [pid 1983] [client 127.0.0.1:XXXX] PHP Notice: Undefined index: text in /home/user/websites/html/dev_twitterwidget/index.php on line 57
[Wed Apr 16 11:45:40.954884 2014] [:error] [pid 1985] [client 127.0.0.1:XXXX] PHP Warning: preg_replace(): Unknown modifier '/' in /home/user/websites/html/dev_twitterwidget/index.php on line 33
Ubuntu 13.10 x64 PHP 5.5.3