2014-06-10 1 views
3

최신 프로젝트의 경우 YouTube 검색의 결과가 JSON 문자열이어야합니다. 나는 전체 응답 배열을 JSON으로 반환되지만 JSON 출력은 하나 개의 결과와 그 결과Google PHP SDK가 포함 된 YouTube API - 응답 : JSON

{ "ETAG"의 두 개의 태그를 포함 그래서 here에서 공식 샘플의 코드를 수정하는 것을 시도했다 " \ "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())); 
    } 
} 
?> 

:

그리고 내 코드를 먹으 렴 결국 나는 문제는 당신이 JSON 인코딩에 PHP 클라이언트 라이브러리를 사용하는 동안 수신 된 응답 개체를 시도하고 있다는 점이다

{ 
"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" 
    } 
    } 
] 
} 
+0

당신이 알아낼 수 있습니까 :

당신이 var_dump($searchResponse); 경우 당신은 당신은 당신이 좋아하는 모델에 toSimpleObject를 호출하여 얻을 "simpleObject을"로 json_encode 할 Google_Service_YouTube_SearchListResponse Object

라고 볼 수 있습니다 ? –

답변

4

같은 것을 가지고 싶어요. Youtube API가 반환 한 원시 JSON 객체가 아닙니다 (인코딩하려는 내용).

json_encode($searchResponse->toSimpleObject()); 
+1

정말 고마워요. 귀하의 솔루션은 응답 API에서 보호 된 메서드에 액세스 할 수있는 유일한 사람입니다 ... –

+0

정말 두 가지 중 하나에서 궁금합니다 : 왜 자신의 문서에 대한이 정보가 Google을 검색 할 때 첫 번째 결과가 아니며, 고 말했다. –