dongzhuner6981 2017-04-28 10:39
浏览 74
已采纳

无法使用curl检索JSON以在php中解析

I'm attempting too fetch and parse JSON in PHP via curl. I am unable to parse the response, and see it dumping to my screen instead. Here is sample code.

<?php

$url = 'XXXXXXXXXXXXXXXXXXXXX';

$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);

curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'X-Mashape-Key: XXXXXXXXXXXXXXXXXXXXXXXXX',
'Accept: application/json'
));

$result = curl_exec($cURL); ## this seems to be the line printing the response?

curl_close($cURL);

echo "<br />--------------------------------------------------------<br />";

var_dump($cURL);

echo "<br />--------------------------------------------------------<br />";

var_dump($result);

echo "<br />--------------------------------------------------------<br />";

$json = json_decode($result);

echo "<br />--------------------------------------------------------<br />";

print_r($json);

echo "<br />--------------------------------------------------------<br />";

echo $json->word;
echo $json->definitions[0]->definition;

echo "<br />--------------------------------------------------------<br />";

?>

And the output is:

{"word":"incredible","definitions":[{"definition":"beyond belief or understanding","partOfSpeech":"adjective"}]}
--------------------------------------------------------
resource(2) of type (Unknown) 
--------------------------------------------------------
bool(true) 
--------------------------------------------------------

--------------------------------------------------------
1
--------------------------------------------------------

--------------------------------------------------------

See how my JSON is appearing on screen before my first echo statement? Why is this happening?

  • 写回答

2条回答 默认 最新

  • duanjiaopi8218 2017-04-28 10:50
    关注

    cURL doesn't return transfer but it outputs. You must not output your transfer. Add CURLOPT_RETURNTRANSFER option with true value. Also in the response JSON, definitions is not an object it's array and it will be parsed as array in php too. Also there is no attachments key in response so you can't access it.

    $url = 'XXXXXXXXXXXXXXXXXXXXX';
    
    $cURL = curl_init();
    
    curl_setopt($cURL, CURLOPT_URL, $url);
    curl_setopt($cURL, CURLOPT_HTTPGET, true);
    
    curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    'X-Mashape-Key: XXXXXXXXXXXXXXXXXXXXXXXXX',
    'Accept: application/json'
    ));
    
    curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1); // add this line to catch your response with curl_exec function. 
    
    $result = curl_exec($cURL); 
    
    $json = json_decode($result);
    
    print_r($json); // this will print parsed json.
    
    echo $json->word;
    echo $json->definitions[0]->definition; // Get First Entry of definitions
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝