简体   繁体   English

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

[英]Problem handling JSON response from cURL request

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. 我正在尝试处理来自cURL请求的响应,该请求返回带有一些日期和我不需要的其他数据的JSON。 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. var_dump仅返回“ 1”或true。

What am I doing wrong? 我究竟做错了什么?

Thank you very much in advance. 提前非常感谢您。

You'll want to set CURLOPT_RETURNTRANSFER to true (or 1 ) in your curl_setopt_array call. 您需要在curl_setopt_array调用中将CURLOPT_RETURNTRANSFERtrue (或1 )。

http://php.net/curl_exec http://php.net/curl_exec

Returns TRUE on success or FALSE on failure. 成功返回TRUE ,失败返回FALSE However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure. 但是,如果设置了CURLOPT_RETURNTRANSFER选项,则成功时将返回结果,失败时将返回FALSE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM