2

我有一个像这样的 cURL 请求

$ch = curl_init();
$data = 'filter=year(StartTime)' . urlencode(' eq 2013 and ') .'month(StartTime)'. urlencode(' eq 06') ;
curl_setopt($ch, CURLOPT_URL, "http://url.com/id()/events?$".$data);
$headers = array(
    'Accept: application/json',
    'Content-type: application/json',
    'X-ApiKey : XXXXXXXXXX'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, false);

$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);

?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>


</head>

<body>

<br><br>
<?php 
 echo "the name". $result['Name'];
?>

</body>
</html>

这就是它打印的内容。

HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 218 Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET- WEBSRV X-Powered-By: ARR/2.5 X-Powered-By: ASP.NET - ARR02 Date: Mon, 01 Jul 2013 02:52:31 GMT [{"Id":1079,"Name":"Test ","Status":1,"MemberId":1308,"Description":"This is a Test Event","SponsoredBy":null,"StartTime":"2013-06-30T12:00:00","EndTime":"2013-06-30T23:59:00","SearchDescription":null,"Types":[1,4,6]}] 

the name

如何将其放入关联数组中?

我试过这个

json_decode($response,true));

和这个

ksort($response);

和这个

var_dump($response);

似乎没有任何效果..

我希望能够像这样输出

echo $reponse['Name'];

有什么帮助吗?谢谢

4

4 回答 4

14

json_decode当您将“true”作为第二个参数传递时,会为您提供一个关联数组:

  $json = '[{"Id":1079,"Name":"Test ","Status":1,"MemberId":1308,"Description":"This is a Test Event","SponsoredBy":null,"StartTime":"2013-06-30T12:00:00","EndTime":"2013-06-30T23:59:00","SearchDescription":null,"Types":[1,4,6]}]';

  $response = json_decode($json, true);

  echo $response[0]["Name"];

给出:

Test

编辑:

json_decode()给你一个数组数组,所以你需要引用[0]响应中位置的数组,如果你得到我的话。

我在上面的示例中使用 $response[0] 完成了该操作,但请看一下这个示例,希望它更清楚!

  $result = json_decode($json, true);
  var_dump($result);

给出:

array(1) {
  [0]=>
  array(10) {
    ["Id"]=>
    int(1079)
    ["Name"]=>
    string(5) "Test "
    ["Status"]=>
    int(1)
    ["MemberId"]=>
    int(1308)
    ["Description"]=>
    string(20) "This is a Test Event"
    ["SponsoredBy"]=>
    NULL
    ["StartTime"]=>
    string(19) "2013-06-30T12:00:00"
    ["EndTime"]=>
    string(19) "2013-06-30T23:59:00"
    ["SearchDescription"]=>
    NULL
    ["Types"]=>
    array(3) {
      [0]=>
      int(1)
      [1]=>
      int(4)
      [2]=>
      int(6)
    }
  }
}

然后..访问数组本身:

  $result = json_decode($json, true);

  $result = $result[0]; // let's just reassign this to get the array we want      
  var_dump($result);

给出:

array(10) {
  ["Id"]=>
  int(1079)
  ["Name"]=>
  string(5) "Test "
  ["Status"]=>
  int(1)
  ["MemberId"]=>
  int(1308)
  ["Description"]=>
  string(20) "This is a Test Event"
  ["SponsoredBy"]=>
  NULL
  ["StartTime"]=>
  string(19) "2013-06-30T12:00:00"
  ["EndTime"]=>
  string(19) "2013-06-30T23:59:00"
  ["SearchDescription"]=>
  NULL
  ["Types"]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(4)
    [2]=>
    int(6)
  }
}

现在您可以直接访问数组的各种元素:

  $result = json_decode($json, true);
  $result = $result[0];

  echo "Name: ". $result["Name"] . "\nID:   " . $result["Id"] . "\nDescription: " . $result["Description"] . "\n";

现在我们回来了:

Name: Test 
ID:   1079
Description: This is a Test Event

希望这是有道理的!

于 2013-07-01T01:49:15.397 回答
6

默认情况下,curl_exec 将从服务器获取的数据输出到标准输出,因此您的 $response 变量实际上并没有实际的响应数据。如果要获取变量中的数据,请在调用 curl_exec 之前设置 CURLOPT_RETURNTRANSFER 选项。

curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE)
于 2013-07-01T10:05:33.827 回答
0

所以我有一个像这样的 cURL 结果。

{
  "object": "transaction",
  "status": "paid",
}

所以我做了这个。

$result = json_decode($response, true);
$result['status'];

结果将是:

paid
于 2022-01-10T05:39:35.210 回答
0

我发现的方法是删除响应的所有标题部分,在您的情况下找到json响应的第一个字符的位置是'['但也可能是'{',然后执行以下操作:

$json = 'HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 218 Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET- WEBSRV X-Powered-By: ARR/2.5 X-Powered-By: ASP.NET - ARR02 Date: Mon, 01 Jul 2013 02:52:31 GMT [{"Id":1079,"Name":"Test ","Status":1,"MemberId":1308,"Description":"This is a Test Event","SponsoredBy":null,"StartTime":"2013-06-30T12:00:00","EndTime":"2013-06-30T23:59:00","SearchDescription":null,"Types":[1,4,6]}] ';
$pos = strpos($json,'[');
$json = substr($json,$pos);
$php_array = json_decode($json, true);
echo "Name= ".$php_array[0]['Name']; //because it starts with [ you need to add [0]

你得到 Name=Test

于 2022-01-29T13:45:51.267 回答