使用 PHP 解码 Microsoft 翻译 API 的 JSON 输出
这个问题似乎是 microsofttranslator.com 特有的,所以请...任何答案,如果您可以对其进行测试...
使用以下 URL 进行翻译: microsofttranslator.com/V2/Ajax.svc/TranslateArray" rel="nofollow">http://api.microsofttranslator.com/V2/Ajax.svc/TranslateArray ..我通过 cURL 发送一些精彩的参数,并且返回以下内容结果:
[
{
"From":"en",
"OriginalTextSentenceLengths":[13],
"TranslatedText":"我是最好的",
"TranslatedTextSentenceLengths":[5]
},
{
"From":"en",
"OriginalTextSentenceLengths":[16],
"TranslatedText":"你是最好的",
"TranslatedTextSentenceLengths":[5]
}
]
不合适:
json_last_error() == JSON_ERROR_SYNTAX
当我在 cURL 的输出上使用 json_decode($output, true);
时,json_decode 会给出一个错误,说明返回的 JSON 中的语法 与 JSON 返回:
响应标头
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
原始内容:
[{"From":"en","OriginalTextSentenceLengths":[13],"TranslatedText":"我是最好的","TranslatedTextSentenceLengths":[5]},{"From":"en","OriginalTextSentenceLengths":[16],"TranslatedText":"你是最好的","TranslatedTextSentenceLengths":[5]}]
cURL 代码:
$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);
this issue seems specific to microsofttranslator.com so please ... any answers, if you can test against it ...
Using the following URL for translation: http://api.microsofttranslator.com/V2/Ajax.svc/TranslateArray .. I send via cURL some fantastic arguments, and get back the following result:
[
{
"From":"en",
"OriginalTextSentenceLengths":[13],
"TranslatedText":"我是最好的",
"TranslatedTextSentenceLengths":[5]
},
{
"From":"en",
"OriginalTextSentenceLengths":[16],
"TranslatedText":"你是最好的",
"TranslatedTextSentenceLengths":[5]
}
]
When I use json_decode($output, true);
on the output from cURL, json_decode gives an error about the syntax not being appropriate in the returned JSON:
json_last_error() == JSON_ERROR_SYNTAX
The headers being returned with the JSON:
Response 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
API 返回错误的字节顺序标记 (BOM)。
字符串数据本身是 UTF-8,但前面带有
U+FEFF
(UTF-16 BOM)。只需删除前两个字节和 json_decode 即可。这是我的完整测试代码。
这给了我
关于 BOM 的维基百科条目
The API is returning a wrong byte order mark (BOM).
The string data itself is UTF-8 but is prepended with
U+FEFF
which is a UTF-16 BOM. Just strip out the first two bytes andjson_decode
.Here's the entirety of my test code.
Which gives me
Wikipedia entry on BOM
您的 JSON 字符串在语法上没有任何错误。返回的 json 可能包含 UTF-8 字节范围之外的字符,但这会导致 json_decode() 抛出异常,表明这一点。
测试代码:
输出:
There is nothing syntactically wrong with your JSON string. It is possible that the json is coming back with characters outside the UTF-8 byte range, but this would cause json_decode() to throw an exception indicating that.
Test Code:
Output: