Home >Backend Development >PHP Tutorial >php调用百度翻译api后,调用成功后为什么结果为空?

php调用百度翻译api后,调用成功后为什么结果为空?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:25:381845browse

<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>

因为百度返回给你的结果就是空, 下图为 百度百度提供的调试工具 得到的结果, 和你提供的代码所获得到的结果是一样的.

php调用百度翻译api后,调用成功后为什么结果为空?

<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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn