4

I have a cURL request like so

$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>

This is what it prints.

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

How can I put this into a Associative array?

I've tried this

json_decode($response,true));

and this

ksort($response);

and this

var_dump($response);

and nothing seems to work..

I want to be able to output like this

echo $reponse['Name'];

Any help? Thanks

4
  • You mention that var_dump($response); "doesn't work" - what does it give you? Are you struggling to get the JSON string from the CURL call, or struggling to decode it into an array? Commented Jul 1, 2013 at 2:36
  • If I do this ` $response = var_dump($response); echo $response['Name'];` in outputs this bool(true) It seems to get the json string from CURL fine.. just can't get it into a assoc array Commented Jul 1, 2013 at 2:39
  • @IMSoP I think it has something to do with my return string. Some of the values are ints and arrays that don't have quotes around them. Do you know how I could handle this? Could that be the problem? Commented Jul 1, 2013 at 3:39
  • json_decode($response,true)); worked for me Commented Apr 19, 2018 at 7:12

4 Answers 4

16

json_decode gives you an associative array when you pass "true" as the second argument:

  $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"];

gives:

Test

Edit:

json_decode() is giving you back an array of arrays, so you need to reference the array that is at position [0] in the response, if you get me.

I've done that above in my example with $response[0], but have a look at this example, hope it makes it clearer!

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

gives:

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)
    }
  }
}

then.. to access the array itself:

  $result = json_decode($json, true);

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

gives:

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)
  }
}

And now you can access the various elements of the array directly:

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

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

now we get back:

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

hope that makes sense!

Sign up to request clarification or add additional context in comments.

Comments

6

By default curl_exec outputs the data it gets from the server to the standard output so your $response variable doesn't really have the actual response data in it. If you want to get the data in a variable set the CURLOPT_RETURNTRANSFER option before calling curl_exec.

curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE)

Comments

3

So I have a cURL result like this.

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

So I did this.

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

The result will be:

paid

Comments

0

The way I've found is to remove all the header part of the response, find the position of the first character of the json response in your case is '[' but could also be '{', and do this:

$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]

And you get Name= Test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.