YouTube Data API v3を利用する(PHPでリクエストを送信してみる)

CGI'sトップページ > Youtube API メモ > YouTube Data API v3を利用する(PHPでリクエストを送信してみる)

PHPでリクエストを送信してみる

YouTube Data API: PHP コード サンプル でライブラリが公開されていますが、まずは自前でリクエストを送信してみます。


以下のサンプルでは、YouTube Data API の概要 にあるリクエスト例を PHPで送信し結果を出力しています。

※$API_KEY(API キー)はGoogle Developers Consoleで取得・設定しますが、 「サーバーキー」「ブラウザキー」「Androidキー」「iOSキー」の4種があります。 ここではサーバーキーを取得してIPアドレス制限を設定したものを利用しています。


サンプルコード



結果

{
  "kind": "youtube#videoListResponse",
  "etag": "3EuQLwR0hlwQi70kabhU5O-t23I",
  "items": [
    {
      "kind": "youtube#video",
      "etag": "P4sAkoxc-8We9UuErEtc1MEziWM",
      "id": "bHQqvYy5KYo",
      "snippet": {
        "publishedAt": "2011-05-13T16:54:56Z",
        "channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
        "title": "Google I/O 2011: YouTube's iframe Player: The Future of Embedding",
        "description": "Jeffrey Posnick,  Jarek Wilkiewicz,  Greg Schechter\r\n\r\nYouTube players allow for video playback in web applications. The latest YouTube's embedded iframe player supports both Flash and HTML5 video and exposes a rich API which lets you control the YouTube playback experience. We'll give you the details on how the API was developed, and show you how it can power the videos on your own website.",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/bHQqvYy5KYo/default.jpg",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/bHQqvYy5KYo/mqdefault.jpg",
            "width": 320,
            "height": 180
          },
          "high": {
            "url": "https://i.ytimg.com/vi/bHQqvYy5KYo/hqdefault.jpg",
            "width": 480,
            "height": 360
          },
          "standard": {
            "url": "https://i.ytimg.com/vi/bHQqvYy5KYo/sddefault.jpg",
            "width": 640,
            "height": 480
          },
          "maxres": {
            "url": "https://i.ytimg.com/vi/bHQqvYy5KYo/maxresdefault.jpg",
            "width": 1280,
            "height": 720
          }
        },
        "channelTitle": "Google Developers",
        "tags": [
          "YouTube",
          "HTML5",
          "JavaScript",
          "Flash",
          "video"
        ],
        "categoryId": "28",
        "liveBroadcastContent": "none",
        "localized": {
          "title": "Google I/O 2011: YouTube's iframe Player: The Future of Embedding",
          "description": "Jeffrey Posnick,  Jarek Wilkiewicz,  Greg Schechter\r\n\r\nYouTube players allow for video playback in web applications. The latest YouTube's embedded iframe player supports both Flash and HTML5 video and exposes a rich API which lets you control the YouTube playback experience. We'll give you the details on how the API was developed, and show you how it can power the videos on your own website."
        }
      },
      "contentDetails": {
        "duration": "PT54M37S",
        "dimension": "2d",
        "definition": "hd",
        "caption": "true",
        "licensedContent": true,
        "contentRating": {},
        "projection": "rectangular"
      },
      "status": {
        "uploadStatus": "processed",
        "privacyStatus": "public",
        "license": "youtube",
        "embeddable": true,
        "publicStatsViewable": true,
        "madeForKids": false
      },
      "statistics": {
        "viewCount": "368518",
        "likeCount": "636",
        "dislikeCount": "196",
        "favoriteCount": "0",
        "commentCount": "67"
      }
    }
  ],
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 1
  }
}


Youtube API メモ Indexへ戻る