-1

下面的 JSON 结构显示了搜索结果的格式。您可以在此处获取更多详细信息

{
  "kind": "youtube#searchResult",
  "etag": etag,
  "id": {
    "kind": string,
    "videoId": string,
    "channelId": string,
    "playlistId": string
  },
  "snippet": {
    "publishedAt": datetime,
    "channelId": string,
    "title": string,
    "description": string,
    "thumbnails": {
      (key): {
        "url": string,
        "width": unsigned integer,
        "height": unsigned integer
      }
    },
    "channelTitle": string,
    "liveBroadcastContent": string
  }
}

下面是我为获取 PHP 文件中的值而遵循的代码。

 $searchResponse = $youtube->search->listSearch('id,snippet', array(
      'q' => $_GET['q'],
      'maxResults' => $_GET['maxResults'],
    ));

    foreach ($searchResponse['items'] as $searchResult) {
      switch ($searchResult['id']['kind']) {
        case 'youtube#video':
          $videos[] = $searchResult;
          break;
      }

   foreach ($videos as $video) {
      echo $video['snippet']['title'];
   }

我可以从这里获取标题标签,但如何获取缩略图 url 字符串值。我尝试使用$video['snippet']['thumbnails']['key']['url'].

编辑:我只是直接执行了代码并将其作为响应。现在,我尝试将 url 值访问为$video['snippet']['thumbnails']['high']['url']. 还是没有运气!!它说 Warning: Illegal string offset 'high'

{
 "kind": "youtube#searchListResponse",
 "etag": "\"X98aQHqGvPBJLZLOiSGUHCM9jnE/iTnovD87h4Y7nwPlTFtrcd7IEPY\"",
 "nextPageToken": "CAEQAA",
 "pageInfo": {
  "totalResults": 118094,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"X98aQHqGvPBJLZLOiSGUHCM9jnE/d7ArhLqOVP8ys-9qfM5Mk0UTbH4\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "ixnBb9cyTkg"
   },
   "snippet": {
    "publishedAt": "2014-04-09T16:58:02.000Z",
    "channelId": "UC7rvhg8JyqjmD_D2BV5lzNQ",
    "title": "Mahabharat 9 April 2014 Full Episode (Krishna Save Draupadi Maha Episode)",
    "description": "Mahabharat 10 April Promo ( Pandavas Anger ) https://www.youtube.com/watch?v=pGWMzWf-h2s Mahabharat 9 April 2014 Full Episode.",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/ixnBb9cyTkg/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/ixnBb9cyTkg/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/ixnBb9cyTkg/hqdefault.jpg"
     }
    },
    "channelTitle": "1ColorsTV",
    "liveBroadcastContent": "none"
   }
  }
 ]
}
4

2 回答 2

0

您正在正确访问它..但是您的 JSON 格式有点错误..

下面是固定的 JSON 和代码...

编码..

<?php
$json='{
  "kind": "youtube#searchResult",
  "etag": "etag",
  "id": {
    "kind": "string",
    "videoId": "string",
    "channelId": "string",
    "playlistId": "string"
  },
  "snippet": {
    "publishedAt": "datetime",
    "channelId": "string",
    "title": "string",
    "description": "string",
    "thumbnails": {
      "key": {
        "url": "string",
        "width": "unsigned integer",
        "height": "unsigned integer"
      }
    },
    "channelTitle": "string",
    "liveBroadcastContent": "string"
  }
}';

$yourarr = json_decode($json,true);
echo $yourarr['snippet']['thumbnails']['key']['url'];

Demo

于 2014-04-12T01:08:59.580 回答
0

它不是一个多维数组,它是一个对象......

$j=$youtube->search->listSearch('id,snippet', array(
      'q' => $_GET['q'],
      'maxResults' => $_GET['maxResults'],
    ));

现在您可以使用以下方法获取显示的对象:

var_dump($j);

然后很容易找到获取片段标题的方法:

echo $j->items[0]->snippet->title;
于 2014-04-12T01:48:08.433 回答