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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里