简体   繁体   中英

display utf-8 char from json in php

I am using a public api, YQL, in particular, it returns a json, and in this josn there is a field that is Chinese character. By calling this api url directly, I am able to view the json in browser, and it does show the chinese character in the browser in json form (meaning it is beding decoded correctly and readable i suppose)

now, in php, i call this same api url via curl (it should return the same json as i were calling it directly in browser)

$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$yql_json = curl_exec($session);

i print the returned json in the follow format via php

print_r($yql_json . "\n\n");
print_r(utf8_decode($yql_json) . "\n\n");

and it returns the following respectively,

{"query":{"count":2,"created":"2012-08-19T07:24:51Z","lang":"en-US","results":{"h1":{"class":"entry-title","content":"康熙來了 2012-06-21 女å­ç†è²¡ä¾¿æ˜¯å¾·?!"}}}}


{"query":{"count":2,"created":"2012-08-19T07:24:51Z","lang":"en-US","results":{"h1":{"class":"entry-title","content":"???? 2012-06-21 ????????!"}}}}

as you can see, the chinese char does not show up (after "content")

my goal is to eventually take the content field (the chinese chars) and insert to database, but clearly example above doesn't seem to be in correct chinese value to insert or in utf-8 encoding

basically what i am also asking is that how can i have the chinese char displayed in the above print statements, becuase if i can do that, it means i have the correct utf-8 encoding to insert to database.

please help

i guess my question is: 1. which of the print statement has the correct information for me to insert to database?

edit:

suggested by one of the answers that i should insert the utf8_decode version (the one with ?? and !! above)

i did just that with the following code

$sql = "UPDATE myTable SET title='" . utf8_decode($yql_json_php["query"]["results"["h1"["content"]) . "' WHERE id='4'";
mysql_query("set names 'utf8'");
$result = mysql_query($sql);

i refresh my page, but it still showing up as ?? and !!

i pasted chinese character directly into the table via sequel pro application, i am able to display the data on my webpage

so it has to do with the UPDATE sql i have...the db doesn't seem to know it is utf8

Try using

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

OR

<?php header("Content-type: text/html; charset=utf-8");?>

May be this will help you.

I faced the similar issue. basically you are getting the chinese characters. But while you are printing it , or storing it in database they might show up as "?????" . and again when you retrieve it and display it in browser, you can see the chinese character. To get rid of "????" in database, you will have to define the characterEncoding in database url and set character_set_result and few others to utf8. Meanwhile, if you debug your application and see the value of content on watch , you would actually see the chinese characters. Let me know if this helps.

  1. $yql_json = iconv(in_charset, 'utf-8', $yql_json);

  2. header("Content-type: text/html; charset=utf-8");

  3. web page view coding -> utf-8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM