-2

When I run an API request, I get the following data back.

How do I use PHP to grab the subscriberCount data from this?

{
 "kind": "youtube#channelListResponse",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "statistics": {
    "subscriberCount": "80021",
   }
  }
 ]
}

I have tried this, but no success:

<?php

$json = '{
 "kind": "youtube#channelListResponse",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "statistics": {
    "subscriberCount": "80021",
   }
  }
 ]
}';

$yummy = json_decode($json);

echo $yummy->subscriberCount;

?>
michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190

1 Answers1

2

I found you need to remove , near 80021:

{
 "kind": "youtube#channelListResponse",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "statistics": {
    "subscriberCount": "80021"
   }
  }
 ]
}

And you may try with this code:

$yummy = json_decode($json);

echo $yummy->items[0]->statistics->subscriberCount;

Then your result will be ok.

Nere
  • 4,097
  • 5
  • 31
  • 71