2012-03-04 5 views
0

YouTube에서 검색하고 비디오 비디오 ID 및 제목을 가져 오려고합니다. 나는 "유튜브"를 검색하고있는 울부 짖는 예에서 YouTube API PHP json decode

: 여기

<?php 
    $url="http://gdata.youtube.com/feeds/api/videos?q='youtube'&format=5&max-results=2&v=2&alt=jsonc"; 
    $json = file_get_contents($url,0,null,null); 
    $json_output = json_decode($json); 
    echo '<pre>'; 
     print_r("query results:"); 
     print_r($json_output); 
    '</pre>'; 
    foreach ($json_output->data as $data){ 
    echo "{$data->id}"; 
    echo "{$data->title}"; 
    } 
    ?> 

는 위의 질의에 대한 JSON 출력입니다 :

query results:stdClass Object 
(
    [apiVersion] => 2.1 
    [data] => stdClass Object 
     (
      [updated] => 2012-03-04T20:19:06.314Z 
      [totalItems] => 1000000 
      [startIndex] => 1 
      [itemsPerPage] => 2 
      [items] => Array 
       (
        [0] => stdClass Object 
         (
          [id] => IpSYwvzKukI 
          [uploaded] => 2009-03-14T08:17:36.000Z 
          [updated] => 2012-02-29T10:51:35.000Z 
          [uploader] => kenbedict009 
          [category] => Music 
          [title] => one of the funniest kid in youtube!! 
          [description] => he is a 3 years old korean,this kid makes me laugh 
          [tags] => Array 
           (
            [0] => rin on the rox 
            [1] => charice pempengco 
            [2] => joeydiamond 
           ) 

          [thumbnail] => stdClass Object 
           (
            [sqDefault] => http://i.ytimg.com/vi/IpSYwvzKukI/default.jpg 
            [hqDefault] => http://i.ytimg.com/vi/IpSYwvzKukI/hqdefault.jpg 
           ) 

          [player] => stdClass Object 
           (
            [default] => http://www.youtube.com/watch?v=IpSYwvzKukI&feature=youtube_gdata_player 
            [mobile] => http://m.youtube.com/details?v=IpSYwvzKukI 
           ) 

          [content] => stdClass Object 
           (
            [5] => http://www.youtube.com/v/IpSYwvzKukI?version=3&f=videos&app=youtube_gdata 
            [1] => rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQlCusr8wpiUIhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp 
            [6] => rtsp://v8.cache2.c.youtube.com/CiILENy73wIaGQlCusr8wpiUIhMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp 
           ) 

          [duration] => 344 
          [rating] => 4.6304307 
          [likeCount] => 23419 
          [ratingCount] => 25803 
          [viewCount] => 9848083 
          [favoriteCount] => 15390 
          [commentCount] => 16074 
          [accessControl] => stdClass Object 
           (
            [comment] => allowed 
            [commentVote] => allowed 
            [videoRespond] => moderated 
            [rate] => allowed 
            [embed] => allowed 
            [list] => allowed 
            [autoPlay] => allowed 
            [syndicate] => allowed 
           ) 

         ) 

        [1] => stdClass Object 
         (
          [id] => PPNMGYOm1aM 
          [uploaded] => 2011-06-23T16:35:33.000Z 
          [updated] => 2012-02-29T13:15:47.000Z 
          [uploader] => shakeitupvevo 
          [category] => Music 
          [title] => "Watch Me" from Disney Channel's "Shake It Up" 
          [description] => To download Watch Me visit www.smarturl.it Performed by Bella Thorne and Zendaya 
          [tags] => Array 
           (
            [0] => Bella 
            [1] => Thorne 
            [2] => Zendaya 
            [3] => Cast 
            [4] => of 
            [5] => Shake 
            [6] => It 
            [7] => Up: 
            [8] => Break 
            [9] => Down 
            [10] => Watch 
            [11] => Me 
            [12] => Walt 
            [13] => Disney 
            [14] => Soundtrack 
           ) 

          [thumbnail] => stdClass Object 
           (
            [sqDefault] => http://i.ytimg.com/vi/PPNMGYOm1aM/default.jpg 
            [hqDefault] => http://i.ytimg.com/vi/PPNMGYOm1aM/hqdefault.jpg 
           ) 

          [player] => stdClass Object 
           (
            [default] => http://www.youtube.com/watch?v=PPNMGYOm1aM&feature=youtube_gdata_player 
           ) 

          [content] => stdClass Object 
           (
            [5] => http://www.youtube.com/v/PPNMGYOm1aM?version=3&f=videos&app=youtube_gdata 
           ) 

          [duration] => 193 
          [aspectRatio] => widescreen 
          [rating] => 4.7312055 
          [likeCount] => 145059 
          [ratingCount] => 155509 
          [viewCount] => 48201555 
          [favoriteCount] => 69981 
          [commentCount] => 81938 
          [status] => stdClass Object 
           (
            [value] => restricted 
            [reason] => limitedSyndication 
           ) 

          [restrictions] => Array 
           (
            [0] => stdClass Object 
             (
              [type] => country 
              [relationship] => deny 
              [countries] => DE 
             ) 

           ) 

          [accessControl] => stdClass Object 
           (
            [comment] => allowed 
            [commentVote] => allowed 
            [videoRespond] => allowed 
            [rate] => allowed 
            [embed] => allowed 
            [list] => allowed 
            [autoPlay] => allowed 
            [syndicate] => allowed 
           ) 

         ) 

       ) 

     ) 

) 

임 [ID]를 밖으로 반향을 시도하고 [ 표제]. 내가 뭘 잘못하고 있는거야?

감사합니다.

$items = $json -> data -> items; 

그래서 지금 foreach을 시도하십시오 : 첫번째 itemsObjects Array int로서

답변

7

변경 라인 9 :

foreach ($json_output->data->items as $data){ 
4

당신은 가야

foreach ($items as $item){ 
    echo "{$item->id}"; 
    echo "{$item->title}"; 
} 

을 내가 지금 분명 희망한다.