doujia1988 2014-04-16 15:10
浏览 62
已采纳

使用Twitter API的未定义常量?

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

  • 写回答

1条回答 默认 最新

  • duanjianfu1398 2014-04-16 15:11
    关注

    You have defined as

    CONSUMERKEY
    

    and using as

    new TwitterOAuth(CONSUMER_KEY
    

    Not just the above all of them defined as some constant and using something else

    Hence the issue.

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

    The below should be the correct one.

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

    Also check for other mismatch in the code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 优质github账号直接兑换rmb,感兴趣伙伴可以私信
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)