Decode JSON Output from Microsoft Translator API with PHP

this problem seems specific to microsofttranslator.com , so please ... any answers if you can test it ...

Using the following URL to translate: http://api.microsofttranslator.com/V2/Ajax.svc/TranslateArray .. I send some fantastic arguments through cURL and return the following result:

[ { "From":"en", "OriginalTextSentenceLengths":[13], "TranslatedText":"我是最好的", "TranslatedTextSentenceLengths":[5] }, { "From":"en", "OriginalTextSentenceLengths":[16], "TranslatedText":"你是最好的", "TranslatedTextSentenceLengths":[5] } ] 

When I use json_decode($output, true); upon exiting cURL, json_decode gives an error message when the syntax is not suitable for the returned JSON :

  json_last_error() == JSON_ERROR_SYNTAX 

Headers are returned using JSON:

Answer Headers

  Cache-Control:no-cache Content-Length:244 Content-Type:application/x-javascript; charset=utf-8 Date:Sat, 06 Aug 2011 13:35:08 GMT Expires:-1 Pragma:no-cache X-MS-Trans-Info:s=63644 

Raw Content:

  [{"From":"en","OriginalTextSentenceLengths":[13],"TranslatedText":"我是最好的","TranslatedTextSentenceLengths":[5]},{"From":"en","OriginalTextSentenceLengths":[16],"TranslatedText":"你是最好的","TranslatedTextSentenceLengths":[5]}] 

CURL code:

  $texts = array("i am the best" => 0, "you are the best" => 0); $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = array( 'appId' => $bing_appId, 'from' => 'en', 'to' => 'zh-CHS', 'texts' => json_encode(array_keys($texts)) ); curl_setopt($ch, CURLOPT_URL, $bingArrayUrl . '?' . http_build_query($data)); $output = curl_exec($ch); 
+4
source share
2 answers

The API returns an incorrect byte order sign (BOM).
The string data itself is UTF-8, but is added using U+FEFF , which is the UTF-16 specification. Just separate the first two bytes and json_decode .

 ... $output = curl_exec($ch); // Insert some sanity checks here... then, $output = substr($output, 3); ... $decoded = json_decode($output, true); 

Here is all my test code.

 $texts = array("i am the best" => 0, "you are the best" => 0); $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = array( 'appId' => $bing_appId, 'from' => 'en', 'to' => 'zh-CHS', 'texts' => json_encode(array_keys($texts)) ); curl_setopt($ch, CURLOPT_URL, $bingArrayUrl . '?' . http_build_query($data)); $output = curl_exec($ch); $output = substr($output, 3); print_r(json_decode($output, true)); 

What gives me

 Array ( [0] => Array ( [From] => en [OriginalTextSentenceLengths] => Array ( [0] => 13 ) [TranslatedText] => 我是最好的[TranslatedTextSentenceLengths] => Array ( [0] => 5 ) ) [1] => Array ( [From] => en [OriginalTextSentenceLengths] => Array ( [0] => 16 ) [TranslatedText] => 你是最好的[TranslatedTextSentenceLengths] => Array ( [0] => 5 ) ) ) 

Wikipedia Entry Specification

+5
source

There is nothing syntactically incorrect in your JSON string. It is possible that json returns with characters outside the UTF-8 byte range, but this will cause json_decode () to throw an exception indicating this.

Security Code:

 ini_set("track_errors", 1); $json = ' [ { "From":"en", "OriginalTextSentenceLengths":[13], "TranslatedText":"我是最好的", "TranslatedTextSentenceLengths":[5] }, { "From":"en", "OriginalTextSentenceLengths":[16], "TranslatedText":"你是最好的", "TranslatedTextSentenceLengths":[5] } ] '; $out = @json_decode($json, TRUE); if(!$out) { throw new Exception("$php_errormsg\n"); } else { print_r($out); } ?> 

Output:

 $ php -f jsontest.php Array ( [0] => Array ( [From] => en [OriginalTextSentenceLengths] => Array ( [0] => 13 ) [TranslatedText] => 我是最好的[TranslatedTextSentenceLengths] => Array ( [0] => 5 ) ) [1] => Array ( [From] => en [OriginalTextSentenceLengths] => Array ( [0] => 16 ) [TranslatedText] => 你是最好的[TranslatedTextSentenceLengths] => Array ( [0] => 5 ) ) ) 
+1
source

All Articles