Reputation: 7569
I want to display my Twitter Info in my blog. So I write some code to get it.
the issue I got is that Chinese characters displayed as unknown code.
Here is the test code. Could anyone take a look and help? Thanks
<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>
Upvotes: 1
Views: 5306
Reputation: 70075
I ran into a similar problem using Magpie + Snoopy. I had to add a line like this before fetching the RSS:
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
Otherwise, I was getting question marks instead of smart quotes, and at that point, no amount of fussing with the charset will fix it.
Not sure if this will work, but you might want to try adding an "Aceept-Charset" header to the $rawheaders array in Snoopy.
Upvotes: 0
Reputation: 12437
Add this line to the header of your files:
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=charset_name">
Where charset_name can be gb2312
, big5
, utf-8
Upvotes: 0
Reputation: 382696
The PHP UTF-8 cheatsheet is good tutorial when displaying content different languages.
Upvotes: 1