doubeizhong5178 2019-02-18 21:46
浏览 287
已采纳

问题处理来自cURL请求的JSON响应

I'm trying to handle the response from a cURL request that returns a JSON with some date and other data i don't need. I'm trying to access the data from the dates to show them in a calendar in my app, but I'm not able to access the information at all. I've checked other similar questions but none of the solutions worked for me. This is my code:

<?php
$data = "{}";
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://******",
  CURLOPT_POST => 1,
  CURLOPT_POSTFIELDS => $data,
  CURLOPT_FOLLOWLOCATION => true,
));


$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$decode = json_decode($response, true);
var_dump($decode);
var_dump($response);

This is the response i get:

{
calendar: {
holidays: [
"12/10/2018",
"01/11/2018",
"09/11/2018",
"06/12/2018",
"19/04/2019",
"01/05/2019"
],
nonTeaching: [
"24/12/2018",
"02/11/2018",
"26/12/2018",
"07/12/2018",
"27/12/2018",
"12/04/2019",
"28/12/2018",
"03/05/2019",
"28/04/2019"
],
events: [
{
other info
}

I'm trying to access the dates from holidays and nonTeaching, but so far I haven't been able to. The var_dump just returns "1" or true.

What am I doing wrong?

Thank you very much in advance.

  • 写回答

1条回答 默认 最新

  • doumeng4400 2019-02-18 22:02
    关注

    You'll want to set CURLOPT_RETURNTRANSFER to true (or 1) in your curl_setopt_array call.

    http://php.net/curl_exec

    Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀