我想在我的博客中显示我的Twitter信息。所以我写了一些代码来实现它。
我得到的问题是中文字符显示为未知代码。
这是测试代码。有人可以看看并帮忙吗?感谢
<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 :(得分:1)
PHP UTF-8 cheatsheet是展示不同语言内容的好教程。
答案 1 :(得分:0)
将此行添加到文件的标题中:
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=charset_name">
charset_name可以是gb2312
,big5
,utf-8
答案 2 :(得分:0)
我使用Magpie + Snoopy遇到了类似的问题。在获取RSS之前我必须添加这样的一行:
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
否则,我得到的是问号而不是智能引号,而且在这一点上,没有任何关于charset的烦恼会解决它。
不确定这是否可行,但您可能想尝试在Snoopy中的$ rawheaders数组中添加“Aceept-Charset”标头。