使用Twitter API的未定义常量?

时间:2014-04-16 15:10:34

标签: php twitter

我正在尝试为我的Wordpress网站创建一个Twitter小部件,这个小部件使用Twitter API 1.1在Twitter上从我的帐户获取最后五条推文。

我正在使用此代码:

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

但是,PHP什么都不返回:(

当我看到日志文件(/ var / apache2 / logs)有这些行时:

[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

1 个答案:

答案 0 :(得分:0)

您已定义为

CONSUMERKEY

并使用

new TwitterOAuth(CONSUMER_KEY

不仅仅是上面所有的定义为一些常量并使用其他东西

因此问题。

define('TWITTER_USERNAME', 'myusername');
define('CONSUMERKEY', 'XXXXXXXXXXXXXXXXXXXXXXXXX');
define('CONSUMERSECRET', 'XXXXXXXXXXXXXXXXXXXXXXXXX');
define('ACCESSTOKEN', 'XXXXXXXXXXXXXXXXXXXXXXXXX');
define('ACCESSTOKENSECRET', 'XXXXXXXXXXXXXXXXXXXXXXXXX');

以下应该是正确的。

$twitter = new TwitterOAuth(CONSUMERKEY, CONSUMERSECRET, ACCESSTOKEN, ACCESSTOKENSECRET);

同时检查代码中的其他不匹配。