doubeizhong5178
doubeizhong5178
2019-02-18 21:46

问题处理来自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 doumeng4400 2年前

    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.

    点赞 评论 复制链接分享