Php 谷歌字典API的json_解码
我试图用PHP解码Google Dictionary API JSON响应,但遇到了一个问题,我似乎无法解析响应(Php 谷歌字典API的json_解码,php,json,Php,Json,我试图用PHP解码Google Dictionary API JSON响应,但遇到了一个问题,我似乎无法解析响应(JSON\u decode返回NULL) 找到了一个示例JSON响应(搜索单词“oracle”) JSON API要求您提交一个回调函数,因此我选择了一个非常简短的(a),因为它不需要处理 这是我的密码: <?php $query = 'oracle'; $file = file_get_contents('http://www.google.com/dictionary/js
JSON\u decode
返回NULL
)
找到了一个示例JSON响应(搜索单词“oracle”)
JSON API要求您提交一个回调函数,因此我选择了一个非常简短的(a
),因为它不需要处理
这是我的密码:
<?php
$query = 'oracle';
$file = file_get_contents('http://www.google.com/dictionary/json?callback=a&q='.$query.'&sl=en&tl=en&restrict=pr,de&client=te');
var_dump($file);
$json = json_decode($file);
var_dump($json);
?>
谷歌以函数调用的形式返回结果。如果你想摆脱这种状况,你可以: 所以你必须把那部分去掉:
$file = substr($file, 2, -10);
此外,所有十六进制字符都会造成混乱。我不知道处理这些问题的最佳方法(他们应该转变为他们的性格,但如何轻松地做到这一点需要思考)。出于测试目的,您可以将其剥离(以确保其正常工作)。
尝试:
因此,最后你有:
<?php
$query = 'oracle';
$file = file_get_contents('http://www.google.com/dictionary/json?callback=a&q='.$query.'&sl=en&tl=en&restrict=pr,de&client=te');
// var_dump($file);
$file = substr($file, 2, -10);
$file = preg_replace("/\\\x[0-9a-f]{2}/", "", $file);
echo $file;
$json = json_decode($file);
var_dump($json);
?>
<?php
$query = 'oracle';
$file = file_get_contents('http://www.google.com/dictionary/json?callback=a&q='.$query.'&sl=en&tl=en&restrict=pr,de&client=te');
// var_dump($file);
$file = substr($file, 2, -10);
$file = preg_replace("/\\\x[0-9a-f]{2}/", "", $file);
echo $file;
$json = json_decode($file);
var_dump($json);
?>
$from = array("\x3d", "\x22", ...);
$to = array("=", "\"", ...);
$file = str_replace($from, $to, $file);