2014-09-06 66 views
3

我有以下代碼:如何讀取在PHP中的YouTube數據API V3響應

$yt_profiles = $youtube->channels->listChannels('brandingSettings', array(
'mine' => 'true', 
)); 

返回下面的輸出:

Google_Service_YouTube_ChannelListResponse Object 
(
[collection_key:protected] => items 
[etag] => "WFPuK6TsnblcGPcnMex79s42ynQ/sTnuE1bHO-tokx_mFFDt1ybN90g" 
[eventId] => 
[itemsType:protected] => Google_Service_YouTube_Channel 
[itemsDataType:protected] => array 
[kind] => youtube#channelListResponse 
[nextPageToken] => 
[pageInfoType:protected] => Google_Service_YouTube_PageInfo 
[pageInfoDataType:protected] => 
[prevPageToken] => 
[tokenPaginationType:protected] => Google_Service_YouTube_TokenPagination 
[tokenPaginationDataType:protected] => 
[visitorId] => 
[modelData:protected] => Array 
    (
     [pageInfo] => Array 
      (
       [totalResults] => 1 
       [resultsPerPage] => 1 
      ) 

     [items] => Array 
      (
       [0] => Array 
        (
         [kind] => youtube#channel 
         [etag] => "WFPuK6TsnblcGPcnMex79s42ynQ/ecOcHFmWyWQ7ToCD7-B1L36b4L4" 
         [id] => UCQO6uXy5maTpYvSa_yM--Bw 
         [brandingSettings] => Array 
          (
           [channel] => Array 
            (
             [title] => Vasim Padhiyar 
             [showRelatedChannels] => 1 
             [featuredChannelsTitle] => Featured Channels 
             [featuredChannelsUrls] => Array 
              (
               [0] => UCw-TnDmYDQyjnZ5qpVWUsSA 
              ) 

             [profileColor] => #000000 
            ) 

           [image] => Array 
            (
             [bannerImageUrl] => http://s.ytimg.com/yts/img/channels/c4/default_banner-vfl7DRgTn.png 
            ) 

           [hints] => Array 
            (
             [0] => Array 
              (
               [property] => channel.featured_tab.template.string 
               [value] => Everything 
              ) 

             [1] => Array 
              (
               [property] => channel.banner.image_height.int 
               [value] => 0 
              ) 

             [2] => Array 
              (
               [property] => channel.modules.show_comments.bool 
               [value] => True 
              ) 

            ) 

          ) 

        ) 

      ) 

    ) 

[processed:protected] => Array 
    (
    ) 

我通過modelData要循環:受保護的變量來獲取通道及其項目數據的列表。它的json對象如此$ yt_profiles-> modelData:protected在訪問時不工作。請幫我解決這個問題。 在此先感謝

回答

3

可以達到它作爲一個數組:

print_r ($yt_profiles['modelData']); 
0

你的要求:

$titleChannel = $yt_profiles["items"][0]["brandingSettings"]["channel"]["title"]; 

$yt_profiles = $youtube->channels->listChannels('brandingSettings', array(
'mine' => 'true', 
)); 

要存取權限值例如頻道標題

符合值bannerImageUrl例如:

$banner = $yt_profiles["items"][0]["brandingSettings"]["image"]["bannerImageUrl"]; 
+0

我的問題是創建循環。你的代碼不是動態的。如果我有超過1個頻道?我想用動態循環變量$ i替換[0]。例如:for($ i = 0; $ i Kliptu 2014-09-09 05:53:30

0

這是否幫助你嗎?

$yt_profiles = $youtube->channels->listChannels('id, brandingSettings', array('mine' => 'true',)); 

    foreach ($yt_profiles['modelData']['items'] as $searchResult) { 
     switch ($searchResult['kind']) { 
     case 'youtube#channel': 
      $channels[]= array('id'=> $searchResult['id'], 
          'brandingSettings'=> $searchResult['brandingSettings']); 
      break; 
     } 
    }