2016-02-28 58 views
0

我试图检索描述或只是任何使用YouTube API的数据,它似乎并没有为我工作>>的YouTube API JSON PHP

我在做什么。

<?php 

    $unparsed_json = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=AIzaSyB-3Ayh1i6MlXXqWCr-NdJ07tepfalklfs&part=snippet,contentDetails,statistics,status"); 

    $json_object = json_decode($unparsed_json, true); 

    ?> 

    <html> 
     <head> 
      <title>Music Player</title> 
      <link rel="stylesheet" href="/resources/app.css"> 
     </head> 

     <body> 
      <p><?php echo $json_object['items']['description']; ?></p> 
     </body> 
    </html> 

什么API输出:

{ 
"kind": "youtube#videoListResponse", 
"etag": "\"q5k97EMVGxODeKcDgp8gnMu79wM/HsO5dOfnOcgAuvefMmBwP_N0QX0\"", 
"pageInfo": { 
    "totalResults": 1, 
    "resultsPerPage": 1 
}, 
"items": [ 
    { 
    "kind": "youtube#video", 
    "etag": "\"q5k97EMVGxODeKcDgp8gnMu79wM/zt9_oZje8_2zsC7FjoJEVT0leuw\"", 
    "id": "7lCDEYXw3mM", 
    "snippet": { 
    "publishedAt": "2012-06-20T23:12:38.000Z", 
    "channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw", 
    "title": "Google I/O 101: Q&A On Using Google APIs", 
    "description": "Antonio Fuentes speaks to us and takes questions on working with Google APIs and OAuth 2.0.", 
    "thumbnails": { 
    "default": { 
     "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/default.jpg", 
     "width": 120, 
     "height": 90 
    }, 
    "medium": { 
     "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/mqdefault.jpg", 
     "width": 320, 
     "height": 180 
    }, 
    "high": { 
     "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/hqdefault.jpg", 
     "width": 480, 
     "height": 360 
    } 
    }, 
    "channelTitle": "Google Developers", 
    "tags": [ 
    "api", 
    "gdl", 
    "i-o" 
    ], 
    "categoryId": "28", 
    "liveBroadcastContent": "none", 
    "localized": { 
    "title": "Google I/O 101: Q&A On Using Google APIs", 
    "description": "Antonio Fuentes speaks to us and takes questions on working with Google APIs and OAuth 2.0." 
    } 
    }, 
    "contentDetails": { 
    "duration": "PT15M51S", 
    "dimension": "2d", 
    "definition": "hd", 
    "caption": "true", 
    "licensedContent": false 
    }, 
    "status": { 
    "uploadStatus": "processed", 
    "privacyStatus": "public", 
    "license": "youtube", 
    "embeddable": true, 
    "publicStatsViewable": true 
    }, 
    "statistics": { 
    "viewCount": "6785", 
    "likeCount": "39", 
    "dislikeCount": "2", 
    "favoriteCount": "0", 
    "commentCount": "11" 
    } 
    } 
] 
} 

而我得到的错误是,说明是一个未定义的指标。

+0

这是因为'$ JSON_OBJECT [ '项']'是一个数组,而不是一个结构。尝试遍历它,或者只是看到第一个条目:'$ json_object ['items'] [0]' - 同样'description'位于'snippet'内,而不是位于项目根目录。 – wiesion

回答

0

尝试以下代码:

<?php 
$unparsed_json = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=7lcdeyxw3mm&key=aizasyb-3ayh1i6mlxxqwcr-ndj07tepfalklfs&part=snippet,contentdetails,statistics,status"); 
$json_object = json_decode($unparsed_json, true); 
?> 

<html> 
    <head> 
     <title>music player</title> 
     <link rel="stylesheet" href="/resources/app.css"> 
    </head> 
    <body> 
     <p><?php echo $json_object['items'][0]['snippet']['localized']['description']; ?></p> 
    </body> 
</html>