title: YouTube API with Google PHP SDK - Response as JSON
body: <p>For my latest Project I would need the result from a YouTube Search as a JSON string. I have tried to modify the code of the official Samples from <a href="https://developers.google.com/youtube/v3/docs/search/list?hl=de" rel="nofollow" title="Search: list YouTube - Google Developers">here</a> so that the whole response array is returned as JSON but the JSON output only includes one result and only two tags of that result </p>
  
  <blockquote>
    <p>{"etag":"\"bvxF-DWHx1toJotsdJBeCm43SLs/Ti9GPWl-tTk2fzo_W4M7px11bPY\"","eventId":null,"kind":"youtube#searchListResponse","nextPageToken":"CBkQAA","prevPageToken":null,"visitorId":null}</p>
  </blockquote>
  
  <p>And thats my code:   </p>
  
  <pre><code>&lt;?php
  header('Cache-Control: no-cache, must-revalidate');
  header('Content-type: application/json');
  if ($_GET['q'] &amp;&amp; $_GET['maxResults']) {
  require_once 'Google/Client.php';
  require_once 'Google/Service/YouTube.php';
  $DEVELOPER_KEY = 'not gonna reveal my key ;) ';
  $client = new Google_Client();
  $client-&gt;setDeveloperKey($DEVELOPER_KEY);
  $youtube = new Google_Service_YouTube($client);
  try 
  {
      $searchResponse = $youtube-&gt;search-&gt;listSearch('id,snippet', array(
          'q' =&gt; $_GET['q'],
          'maxResults' =&gt; $_GET['maxResults'],
      ));
      print_r(json_encode($searchResponse));
  } 
  catch (Google_ServiceException $e) {
      $htmlBody .= sprintf('&lt;p&gt;A service error occurred: &lt;code&gt;%s&lt;/code&gt;&lt;/p&gt;',
      htmlspecialchars($e-&gt;getMessage()));
  } 
  catch (Google_Exception $e) {
      $htmlBody .= sprintf('&lt;p&gt;An client error occurred: &lt;code&gt;%s&lt;/code&gt;&lt;/p&gt;',
      htmlspecialchars($e-&gt;getMessage()));
      }
  }
  ?&gt;
  </code></pre>
  
  <p>In the end I would love to have something like </p>
  
  <pre><code>    {
   "kind": "youtube#searchListResponse",
   "etag": "\"bvxF-DWHx1toJotsdJBeCm43SLs/vL7IQMNuL84nujDqKdOtwOPpBkc\"",
   "nextPageToken": "CAIQAA",
   "pageInfo": {
    "totalResults": 203164,
    "resultsPerPage": 2
   },
   "items": [
    {
     "kind": "youtube#searchResult",
     "etag": "\"bvxF-DWHx1toJotsdJBeCm43SLs/m4lAJ1Qx-hvZpw4Uc9qSo_rqy5o\"",
     "id": {
      "kind": "youtube#video",
      "videoId": "UyI4v5sxT54"
     },
     "snippet": {
      "publishedAt": "2014-04-14T10:21:21.000Z",
      "channelId": "UCpDJl2EmP7Oh90Vylx0dZtA",
      "title": "BORGORE &amp; SIKDOPE - Unicorn Zombie Apocalypse (Original Mix)",
      "description": "BORGORE teams up with SIKDOPE to bring you the main stage rocker that is Unicorn Zombie Apocalypse. Grab your copy NOW : http://btprt.dj/1hFUQhP ...",
      "thumbnails": {
       "default": {
        "url": "https://i.ytimg.com/vi/UyI4v5sxT54/default.jpg"
       },
       "medium": {
        "url": "https://i.ytimg.com/vi/UyI4v5sxT54/mqdefault.jpg"
       },
       "high": {
        "url": "https://i.ytimg.com/vi/UyI4v5sxT54/hqdefault.jpg"
       }
      },
      "channelTitle": "SpinninRec",
      "liveBroadcastContent": "none"
     }
    },
    {
     "kind": "youtube#searchResult",
     "etag": "\"bvxF-DWHx1toJotsdJBeCm43SLs/4LQaEODdVEec6exNA21SnYJAeOU\"",
     "id": {
      "kind": "youtube#video",
      "videoId": "CPXv392pc9k"
     },
     "snippet": {
      "publishedAt": "2014-05-16T23:00:00.000Z",
      "channelId": "UCpDJl2EmP7Oh90Vylx0dZtA",
      "title": "Borgore &amp; Sikdope - Unicorn Zombie Apocalypse (Official Music Video)",
      "description": "Borgore &amp; Sikdope present Unicorn Zombie Apocalypse (Official Music Video). Download your copy on Beatport HERE : http://btprt.dj/1hFUQhP English ...",
      "thumbnails": {
       "default": {
        "url": "https://i.ytimg.com/vi/CPXv392pc9k/default.jpg"
       },
       "medium": {
        "url": "https://i.ytimg.com/vi/CPXv392pc9k/mqdefault.jpg"
       },
       "high": {
        "url": "https://i.ytimg.com/vi/CPXv392pc9k/hqdefault.jpg"
       }
      },
      "channelTitle": "SpinninRec",
      "liveBroadcastContent": "none"
     }
    }
   ]
  }
  </code></pre>