{"id":31,"date":"2006-06-03T09:59:42","date_gmt":"2006-06-03T17:59:42","guid":{"rendered":"http:\/\/simplepie.org\/blog\/2006\/06\/03\/display-non-english-feeds-correctly-beta-2\/"},"modified":"2006-06-26T19:37:46","modified_gmt":"2006-06-27T03:37:46","slug":"display-non-english-feeds-correctly-beta-2","status":"publish","type":"post","link":"https:\/\/simplepie.org\/blog\/2006\/06\/03\/display-non-english-feeds-correctly-beta-2\/","title":{"rendered":"Display non-english feeds correctly (Beta 2)"},"content":{"rendered":"<p class=\"subscribe\"><b>Requirements:<\/b> SimplePie 1.0 Beta 2 (or newer)<\/p>\n<div class=\"chunk noborder\">\nHave you ever had problems displaying feeds properly on your pages?  I ran into an issue a few times when I was trying to display an iTunes Music Store feed, and Beyonc&#0233; kept coming out as Beyonc[enter-garbled-text-here].  I soon realized that the problem occurred because my pages were being served as ISO-8859-1, and the iTMS feed was being sent as UTF-8.<\/p>\n<p>The solution is really quite simple.<\/p>\n<p>All you have to do is make sure that the page is being served and handled in the same character set that the feed is.  Fortunately, SimplePie has a built-in function that handles this for you: <code>handle_content_type()<\/code>.<\/p>\n<p>Ideally, when you&#8217;re loading a page with SimplePie in it, you&#8217;ll do that part at the very beginning of the page.  Doing so will help you serve the page correctly.<\/p>\n<pre>\r\n&lt;?php\r\n\/\/ Start counting time for loading...\r\n$starttime = explode(' ', microtime());\r\n$starttime = $starttime[1] + $starttime[0];\r\n\r\ninclude('..\/simplepie.inc');\r\n\r\n\/\/ Parse it\r\n$feed = new SimplePie();\r\nif (!empty($_GET['feed'])) {\r\n\t$feed-><a href=\"\/docs\/reference\/config\/feed_url\/\">feed_url<\/a>($_GET['feed']);\r\n\t$feed-><a href=\"\/docs\/reference\/config\/init\/\">init<\/a>();\r\n}\r\n\r\n<em>\/\/ This is the part to pay attention to\r\n$feed-><a href=\"\/docs\/reference\/misc\/handle_content_type\/\">handle_content_type<\/a>();<\/em>\r\n\r\n?>&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"&gt;\r\n\r\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\" xml:lang=\"en-US\" lang=\"en-US\"&gt;\r\n&lt;head&gt;\r\n<\/pre>\n<p>We specifically want to focus in on this chunk:<\/p>\n<pre>\r\n$feed->handle_content_type();\r\n<\/pre>\n<p>What this does is:<\/p>\n<ol>\n<li>Checks to see if any content has been sent to the browser yet.<\/li>\n<li>If not, it checks SimplePie&#8217;s output encoding (which as of this release is always UTF-8).<\/li>\n<li>Creates the proper HTTP Headers that tell the browser to handle this page as UTF-8 and <code>text\/html<\/code>.<\/li>\n<\/ol>\n<\/div>\n<p>You can also set the <code>&lt;meta&gt;<\/code> tag if you&#8217;d like, although the HTTP specification says that the information that gets sent by the server will override any <code>&lt;meta&gt;<\/code> tags that try to do the same thing.<\/p>\n<pre>\r\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html;charset=<b>&lt;?php echo $feed-><a href=\"\/docs\/reference\/misc\/get_encoding\/\">get_encoding<\/a>(); ?&gt;<\/b>\" \/&gt;\r\n<\/pre>\n<p>If you&#8217;re loading a feed into a page dynamically (like in our <a href=\"\/ideas\/demo\/delicious-ajax\/\">Delicious AJAX<\/a> demo), your best bet is to serve the page as UTF-8, since all languages are translated into UTF-8 inside SimplePie anyways.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Requirements: SimplePie 1.0 Beta 2 (or newer) Have you ever had problems displaying feeds properly on your pages? I ran into an issue a few times when I was trying to display an iTunes Music Store feed, and Beyonc&#0233; kept coming out as Beyonc[enter-garbled-text-here]. I soon realized that the problem occurred because my pages were [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-tips-and-tricks"],"_links":{"self":[{"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":0,"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"wp:attachment":[{"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simplepie.org\/blog\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}