如何在调用API时打印出PHP中的汉字?
How to print out the Chinese Character in PHP When an API is called?
当我的 API 被调用时,它应该 return 视频的标题、描述、图片等。有时我的视频有中文字符。所以当我打印出 JSON 数组时,汉字显示如下: "\u4e00\u5bb6\u4e0a\u5e02\u4e0d\u4e45\u7684\u5bb6\u5177\u4e1a".
所以我的问题是,当我调用 API 并打印出来时,如何打印出中文单词?
header('Content-Type: text/html; charset=utf-8');
require ("vendor/autoload.php");
use Vimeo\Vimeo;
$client = new Vimeo("{client_id}", "{client_secret}", "{access_token}");
$video_id ="xxxxx";
$response = $client->request("/videos/$video_id");
//var_dump($response['body']);
if($response['status'] === 200){
header('Content-Type: application/json');
echo json_encode($response['body']);
}
else {
echo json_encode($response['body']['error']);
}
使用json_encode
和一些参数来防止中文字符的转换。
header('Content-Type: text/html; charset=utf-8');
require ("vendor/autoload.php");
use Vimeo\Vimeo;
$client = new Vimeo("{client_id}", "{client_secret}", "{access_token}");
$video_id ="xxxxx";
$response = $client->request("/videos/$video_id");
//var_dump($response['body']);
if($response['status'] === 200){
header('Content-Type: application/json');
echo json_encode($response['body'], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
}
else {
echo json_encode($response['body']['error']);
}
当我的 API 被调用时,它应该 return 视频的标题、描述、图片等。有时我的视频有中文字符。所以当我打印出 JSON 数组时,汉字显示如下: "\u4e00\u5bb6\u4e0a\u5e02\u4e0d\u4e45\u7684\u5bb6\u5177\u4e1a".
所以我的问题是,当我调用 API 并打印出来时,如何打印出中文单词?
header('Content-Type: text/html; charset=utf-8');
require ("vendor/autoload.php");
use Vimeo\Vimeo;
$client = new Vimeo("{client_id}", "{client_secret}", "{access_token}");
$video_id ="xxxxx";
$response = $client->request("/videos/$video_id");
//var_dump($response['body']);
if($response['status'] === 200){
header('Content-Type: application/json');
echo json_encode($response['body']);
}
else {
echo json_encode($response['body']['error']);
}
使用json_encode
和一些参数来防止中文字符的转换。
header('Content-Type: text/html; charset=utf-8');
require ("vendor/autoload.php");
use Vimeo\Vimeo;
$client = new Vimeo("{client_id}", "{client_secret}", "{access_token}");
$video_id ="xxxxx";
$response = $client->request("/videos/$video_id");
//var_dump($response['body']);
if($response['status'] === 200){
header('Content-Type: application/json');
echo json_encode($response['body'], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
}
else {
echo json_encode($response['body']['error']);
}