2012-05-11 48 views
0

我想知道为什么在我的wordpress主题的侧边栏中,此代码不会拉入我的最新推文?Twitter Feed无法在网站上//错误

<?php 
/* 
    PHP Twitter Feed Importer 
     @mradamdavies 
    www.elevatelocal.co.uk 
==-----------------------------*/ 

class pull_tweets { // Create a basic class 

var $twitter_handle; // Twitter username 
var $tweet_limit;  // Max tweets to return 
var $tweet_links;  // Show @profile links 
var $tweet_tags;  // Show #tag links 
var $tweet_avatar;  // Show twitter avatar 
var $tweet_profile;  // Show twitter profile link 

var $hard_max;   // Real max tweets (Used to filter out @replies) 
var $i;     // Tweet counter 

function show_tweet_links($tweet_links_output, $tweet_links) { // Find and replace @replies with links 
     if($tweet_links == true) { 
      $find = '/\@([a-zA-Z]+)/'; 
      $replace = '<a class="hash_link" href="http://twitter.com/'.strtolower('\1').'" rel="nofollow">@\1</a>'; 
      $tweet_links_text = preg_replace($find,$replace,$tweet_links_output); 
      return $tweet_links_text; 
     } 
     else { return $tweet_links_output; } 
} 

function show_hash_tags($tweet_hashtags_output, $tweet_tags) { // Find and replace #tags with links 
    if($tweet_tags == true) { 
     $find_hash_tag = '/\#([a-zA-Z]+)/'; 
     $replace_hash_tag = '<a class="hash_link" href="http://twitter.com/search?q=%23'.strtolower('\1').'" rel="nofollow">#\1</a>'; 
     return 
     preg_replace($find_hash_tag,$replace_hash_tag,$tweet_hashtags_output); 
    } 
    else { return $tweet_hashtags_output; } 
} 

function show_twitter_avatar($tweet_avatar_output, $tweet_avatar) { // Show avatar 
    if($tweet_avatar == true) { 
     return '<img src="'.$tweet_avatar_output.'" alt="" />'; 
    } 
    else { return false; } 
} 

function show_twitter_profile_link($tweet_profile_output, $tweet_profile) { // Insert profile link 
    if($tweet_profile == true) { 
     return '<a class="profile_link" href="http://twitter.com/'.$tweet_profile_output.'">@'.$tweet_profile_output.'</a>'; 
    } 
    else { return false; } 
} 

// Create a basic function to pull latest tweets 
function tweets($twitter_handle, $tweet_limit, $tweet_links, $tweet_tags, $tweet_avatar, $tweet_profile) { 

     /* Store Tweets in a JSON object */ 
     $tweet_feed = json_decode(file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name='. 
         $twitter_handle.'&exclude_replies=false&count='.$hard_max)); 

     $i = 0; // Start the tweet limit counter 
     foreach ($tweet_feed as $tweet) { // Loop through the tweets 

      if ($tweet->in_reply_to_user_id == '' && $i != $tweet_limit) { // Don't show direct @replies 
       ++$i; // Increase tweet counter 

    // Check true/false flags and return output accoringly 
    $tweet_text = $tweet->text; 
    $tweet_text = pull_tweets::show_tweet_links($tweet_text,$tweet_links); // Show @reply links? 
    $tweet_text = pull_tweets::show_hash_tags($tweet_text,$tweet_tags); // Show #tag links? 
    $twitter_avatar = 
pull_tweets::show_twitter_avatar($tweet->user->profile_image_url,$tweet_avatar); // Show avatar? 
    $twitter_profile = 
    pull_tweets::show_twitter_profile_link($twitter_handle,$tweet_profile); // Show main profile link? 

    // Echo our tweet contents 
    echo ' 
    <div class="tweet_bg">',$twitter_avatar,' 
    <p>'.$tweet_text.'<br />'.$twitter_profile.'</p> 
    </div>'; 

      } 

     } // end of foreach 

    } // end of tweets function 

} // end of pull_tweets class 
?> 


<script language="javascript"> 
$my_tweets = new pull_tweets(); 

echo $my_tweets->tweets('mradamdavies', 5, true, true, true, true); 
</script> 

通过本教程中提到: * HTTP://www.elevatelocal.co.uk/blog/php-twitter-feed-importer-05094504*

我想这样做与PHP或最小的JavaScript。目前,由于某些原因,Twitter小工具无法在我的网站上工作。

+0

首先,错误日志中是否有与此相关的项目?可能我可以看到file_get_contents不受URL支持,并且有相当多的主机禁用了allow_url_fopen –

+0

错误控制台中没有任何错误..... –

回答

1

您的<script>中的PHP不会被解析为PHP。你可能想

<?php 
$my_tweets = new pull_tweets(); 

$my_tweets->tweets('mradamdavies', 5, true, true, true, true); 
?> 

杰米指出,通过tweets()产生的HTML不是JavaScript,因此你不需要<script>标签都没有。

+1

而且,echo输出HTML,因此您不需要脚本标记所有。从字面上看,刚刚发现了大约2秒之前你 –

+0

谢谢这两个,但我已经尝试了两个建议 - 并仍然没有看到Twitter的饲料领域... –

+0

@JamieBicknell好抓!更新我的答案以反映这一点。 – jprofitt