Search code examples
phpapicharactervimeocjk

How to print out the Chinese Character in PHP When an API is called?


When my API is called, it shall return the video's title, description, pictures, and so on. And sometimes my video has Chinese characters. So when I printed out the JSON array, the Chinese character is showing something like this: "\u4e00\u5bb6\u4e0a\u5e02\u4e0d\u4e45\u7684\u5bb6\u5177\u4e1a".

So my question is, how can i print out the Chinese word when I called the API and print out?

  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']);
  }

Solution

  • use json_encode with some parameters to prevent the transformation of the chinese caracteres.

      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']);
      }