2010-10-10 162 views
1

我想在我的博客中显示我的Twitter信息。所以我写了一些代码来获取它。无法在我的PHP代码中显示中文字符

我得到的问题是汉字显示为未知代码。

这里是测试代码。任何人都可以看看和帮助?谢谢

<html> 
    <title>Twitter Test</title> 
<body> 
<?php 

    function mystique_objectToArray($object){ 
     if(!is_object($object) && !is_array($object)) return $object; 
     if(is_object($object)) $object = get_object_vars($object); 
     return array_map('mystique_objectToArray', $object); 
    } 

define('ABSPATH', dirname(dirname(__FILE__)) . '/'); 

    require_once('/home/jun1st/jun1stfeng.com/wp-includes/class-snoopy.php'); 

    $snoopy = new Snoopy; 
    $response = @$snoopy->fetch("http://twitter.com/users/show/jun1st.json"); 
    if ($response) $userdata = json_decode($snoopy->results, true); else $error = true; 
    $response = @$snoopy->fetch("http://twitter.com/statuses/user_timeline/jun1st.json"); 
    if ($response) $tweets = json_decode($snoopy->results, true); else $error = true; 
    if(!$error): 
     // for php < 5 (included JSON returns object) 
     $userdata = mystique_objectToArray($userdata); 
     $tweets = mystique_objectToArray($tweets); 

     $twitdata = array(); 

     $twitdata['user']['profile_image_url'] = $userdata['profile_image_url']; 
     $twitdata['user']['name'] = $userdata['name']; 
     $twitdata['user']['screen_name'] = $userdata['screen_name']; 
     $twitdata['user']['followers_count'] = $userdata['followers_count']; 
     $i = 0; 
     foreach($tweets as $tweet): 
     $twitdata['tweets'][$i]['text'] = $tweet['text']; 
     $twitdata['tweets'][$i]['created_at'] = $tweet['created_at']; 
     $twitdata['tweets'][$i]['id'] = $tweet['id']; 
     $i++; 
     endforeach; 

    endif; 

    // only show if the twitter data from the database is newer than 6 hours 
     if(is_array($twitdata['tweets'])): ?> 
     <div class="clear-block"> 
     <div class="avatar"><img src="<?php echo $twitdata['user']['profile_image_url']; ?>" alt="<?php echo $twitdata['user']['name']; ?>" /></div> 
     <div class="info"><a href="http://www.twitter.com/jun1st"><?php echo $twitdata['user']['name']; ?> </a><br /><span class="followers"> <?php printf(__("%s followers","mystique"),$twitdata['user']['followers_count']); ?></span></div> 
     </div> 

     <ul> 
     <?php 
      $i = 0; 
      foreach($twitdata['tweets'] as $tweet): 
      $pattern = '/\@(\w+)/'; 
      $replace = '<a rel="nofollow" href="http://twitter.com/$1">@$1</a>'; 
      $tweet['text'] = preg_replace($pattern, $replace , $tweet['text']); 
      $tweet['text'] = make_clickable($tweet['text']); 

      // remove +XXXX 
      $tweettime = substr_replace($tweet['created_at'],'',strpos($tweet['created_at'],"+"),5); 

      $link = "http://twitter.com/".$twitdata['user']['screen_name']."/statuses/".$tweet['id']; 
      echo '<li><span class="entry">' . $tweet['text'] .'<a class="date" href="'.$link.'"  rel="nofollow">'.$tweettime.'</a></span></li>'; 
      $i++; 
      if ($i == $twitcount) break; 
      endforeach; 
     ?> 
     </ul> 
    <? endif?> 
?> 
</body> 
</html> 

回答

0

此行添加到文件的标题:

<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=charset_name"> 

CHARSET_NAME在哪里可以gb2312big5utf-8

+0

我已经这样做了。它不起作用 – fengd 2010-10-10 15:03:48

+0

检查您的文件的编码是否是utf-8。 – 2010-10-10 15:08:41

+0

这是utf-8。我已经检查过:) – fengd 2010-10-11 02:10:00

0

我跑进使用喜鹊+史努比类似的问题。我取了RSS之前添加这样一行:

define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); 

否则,我得到了问号,而不是智能引号,并且在这一点上,没有与字符集大惊小怪的量将修复它。

不知道这是否可行,但你可能想尝试添加一个“Aceept-Charset”头到Snoopy中的$ rawheaders数组中。