0

I am using the following code to pull data from an API with JSON results.

// Open connection
$ch = curl_init();

// Make the curl call
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$head = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);


  $data = json_decode($head,true); 
   foreach($data as $item) {
    $name = $item['name'];
    $description = $item['description'];
    echo "NAME: $name DESCRIPTION: $description";

 }

If I use print_r($head) I see all of the results so I know url, username, password is good, however the echo statement at the end of the code is not producing results.

Here is a sample of what the JSON looks like when I print_r($head). Some of it at least.

{"events":[{"eventId":5183191,"name":"The Rhythm Rockets - FREE","description":"Summer Concert Series - The Rhythm Rockets . .\nFREE. Look for a YELLOW balloon. Bring a chair. PLEASE BE ON TIME (6:00 PM!) TO SIT WITH THE GROUP. Allow time for street parking. Expect a crowd. Meetup at 6pm - Showtime is 7pm. Food, wine and beer will be on sale.\nIn case of inclement weather, concerts will be held indoors at the Lincoln Center, 935 Maple Avenue. For weather updates, call[masked] after 5:30 pm. If moved inside, there is no way we can sit together.","primaryImage":{"smallImageUrl":"https://domainnamestorage.blob.core.windows.net/portalimages/portalimages/71021444-d72d-456c-a419-36b61ead5259.small.png","mediumImageUrl":"https://domainnamestorage.blob.core.windows.net/portalimages/portalimages/71021444-d72d-456c-a419-36b61ead5259.medium.png","largeImageUrl":"https://domainnamestorage.blob.core.windows.net/portalimages/portalimages/71021444-d72d-456c-a419-36b61ead5259.large.png"},"categories":[{"categoryId":2,"parentCategoryId":null,"name":"Performing Arts"},{"categoryId":12,"parentCategoryId":null,"name":"Food & Drink"},{"categoryId":17,"parentCategoryId":2,"name":"Music"},{"categoryId":75,"parentCategoryId":12,"name":"Drinks"},{"categoryId":100,"parentCategoryId":17,"name":"Concerts"}],"location":{"latitude":41.792683,"longitude":-88.011765,"city":"Downers Grove","state":"IL","address":"1000 Grove St","country":"United States","locationName":"Fishel Park"},"instances":[{"startDateTime":"2018-08-21T18:00:00Z","endDateTime":null,"hasTime":true,"allDay":false}],"price":{"lowerPrice":null,"upperPrice":null,"free":false,"description":null},"lastUpdatedDate":"2018-07-26T02:20:49.686Z","popularity":null,"interest":50,"links":[{"isSponsor":false,"name":"Meetup","linkUrl":"https://www.meetup.com/Im-Not-Dead-Yet-50-plus/events/252406255/","logoUrl":null},{"isSponsor":true,"name":"I'm Not Dead Yet --- 50+ Meetup","linkUrl":"http://Meetup.com/Im-Not-Dead-Yet-50-plus","logoUrl":null},{"isSponsor":true,"name":"I'm Not Dead Yet --- 50+ Meetup","linkUrl":"http://Meetup.com/Im-Not-Dead-Yet-50-plus","logoUrl":null}],"previousEventIds":[],"seriesStart":"2018-08-21T18:00:00Z","seriesEnd":null,"url":"http://portal.domainname.com/m/FireVite#!/details/THE-RHYTHM-ROCKETS-FREE/5183191/2018-08-21T18","handPicked":false,"customField":null,"contact":{"organization":null,"name":null,"email":null,"phone":null},"media":null}
disruptekz
  • 11
  • 2
  • 3
    The Content-type: header doesn't make sense without payload. An Accept: might be advisable instead. – mario Aug 22 '18 at 03:30

2 Answers2

3

After your decode, $data will have an array called events with each element being the array you are trying to look at. try:

foreach ($data['events'] as $item)...
wordragon
  • 1,297
  • 9
  • 16
0

print_r() does not make sense forstring, but only for array and object.

it might be rather alike this (and maybe you have to use $data['events'] instead)

when using $data = json_decode($response); it would be $data->events:

$data = json_decode($response, true);

// die('<pre>'.print_r($data, true).'</pre>');

foreach($data['events'] as $key => $item) {

    // $event_id = $data['events'][$key]['eventId'];
    $event_id = $item['eventId'];

    // $name = $data['events'][$key]['name'];
    $name = $item['name'];

    // $desc = $data['events'][$key]['description'];
    $desc = $item['description'];

    echo "KEY: $key, ID: $event_id, NAME: $name, DESCRIPTION: $desc";
}

those inline comments just indicate, what is actually being accessed.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Nonsense, `print_r()` works fine for strings. – Brad Aug 22 '18 at 05:33
  • This is great but check the JSON example again above... I added more data that I am retrieving... how do I compensate for arrays such as "primaryImage" and "Location" ... when I use the same code your provided for those all the string variables just return 'array'. – disruptekz Aug 22 '18 at 06:33
  • @Brad and what shall be the concrete purpose of doing so ?? – Martin Zeitler Aug 22 '18 at 10:48
  • @disruptekz such a JSON respopnse may contain nested arrays/objects, which would need to be parsed in nested loops, just alike the top-most level of the structure is being processed. – Martin Zeitler Aug 22 '18 at 10:51