【问题标题】:YouTube API with Google PHP SDK - Response as JSON带有 Google PHP SDK 的 YouTube API - 以 JSON 格式响应
【发布时间】:2023-03-21 14:22:01
【问题描述】:

对于我的最新项目,我需要来自 YouTube 搜索的结果作为 JSON 字符串。我尝试从here 修改官方示例的代码,以便整个响应数组以 JSON 形式返回,但 JSON 输出仅包含一个结果和该结果的两个标签

{"etag":"\"bvxF-DWHx1toJotsdJBeCm43SLs/Ti9GPWl-tTk2fzo_W4M7px11bPY\"","eventId":null,"kind":"youtube#searchListResponse","nextPageToken":"CBkQAA","prevPageToken": null,"visitorId":null}

这就是我的代码:

<?php
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
if ($_GET['q'] && $_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->setDeveloperKey($DEVELOPER_KEY);
$youtube = new Google_Service_YouTube($client);
try 
{
    $searchResponse = $youtube->search->listSearch('id,snippet', array(
        'q' => $_GET['q'],
        'maxResults' => $_GET['maxResults'],
    ));
    print_r(json_encode($searchResponse));
} 
catch (Google_ServiceException $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
    htmlspecialchars($e->getMessage()));
} 
catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
    htmlspecialchars($e->getMessage()));
    }
}
?>

最后我很想拥有类似的东西

    {
 "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 & 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 & Sikdope - Unicorn Zombie Apocalypse (Official Music Video)",
    "description": "Borgore & 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"
   }
  }
 ]
}

【问题讨论】:

  • 你能解决这个问题吗?

标签: php json youtube youtube-data-api


【解决方案1】:

问题是您正在尝试对使用 php 客户端库时收到的响应对象进行 JSON 编码。它不是 Youtube API 返回的原始 JSON 对象(以及您要编码的内容)。

如果你var_dump($searchResponse);,你会看到它写着Google_Service_YouTube_SearchListResponse Object

您想对通过在模型上调用 toSimpleObject 获得的“simpleObject”进行 json_encode,例如:

json_encode($searchResponse->toSimpleObject());

【讨论】:

  • 非常感谢。您的解决方案是唯一从响应 api 访问受保护方法的解决方案...
  • 我真的很想知道两件事之一:为什么他们的文档中的这些信息不是搜索谷歌时的第一个结果,或者为什么我在阅读所述文档时如此糟糕。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-04
  • 2013-11-26
  • 2014-01-25
  • 2018-02-26
  • 1970-01-01
  • 2019-03-22
相关资源
最近更新 更多