2010-10-10 32 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數組中。