<code> $ch = curl_init(); //$keyword='hello'; $url = 'http://apis.baidu.com/apistore/tranlateservice/dictionary?query=hello&from=en&to=zh'; $header = array( 'apikey: 这里是我的apikey,我没有写', ); // 添加apikey到header curl_setopt($ch, CURLOPT_HTTPHEADER , $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 执行HTTP请求 curl_setopt($ch , CURLOPT_URL , $url); $res = curl_exec($ch); curl_close($ch); var_dump(json_decode($res));</code>
<code> $ch = curl_init(); //$keyword='hello'; $url = 'http://apis.baidu.com/apistore/tranlateservice/dictionary?query=hello&from=en&to=zh'; $header = array( 'apikey: 这里是我的apikey,我没有写', ); // 添加apikey到header curl_setopt($ch, CURLOPT_HTTPHEADER , $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 执行HTTP请求 curl_setopt($ch , CURLOPT_URL , $url); $res = curl_exec($ch); curl_close($ch); var_dump(json_decode($res));</code>
因为百度返回给你的结果就是空, 下图为 百度百度提供的调试工具 得到的结果, 和你提供的代码所获得到的结果是一样的.
<code><?php $ch = curl_init(); $url = 'http://apis.baidu.com/baidu_openkg/zici_kg/zici_kg'; $header = array( 'Content-Type:application/x-www-form-urlencoded', 'apikey: a32f233654d52b869c0e96dd6b3e2db7', ); $data = '{"query": "喜欢近义词", "resource": "zici"}'; // 添加apikey到header curl_setopt($ch, CURLOPT_HTTPHEADER , $header); // 添加参数 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 执行HTTP请求 curl_setopt($ch , CURLOPT_URL , $url); $res = curl_exec($ch); $var = json_decode($res, true); print_r($var); ?></code>