2017-01-11 3 views
0

2016 년 3 월부터 YouTube API v3을 사용하여 동영상 길이 ($ vid_dur)를 얻으려면 아래 코드를 사용하여 아무런 문제없이 측면. 어떤 제안?YouTube API v3에서 동영상 길이 가져 오기가 작동하지 않습니다.

<?php 
    $vidkey = "xxxxxxxxx" ; 
    $apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ; 

    $dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$vidkey&key=$apikey"); 
    $VidDuration =json_decode($dur, true); 
    foreach ($VidDuration['items'] as $vidTime) 
    { 
    $VidDuration= $vidTime['contentDetails']['duration']; 
    } 
    // convert duration from ISO to M:S 
    $date = new DateTime('2000-01-01'); 
    $date->add(new DateInterval($VidDuration)); 

    $vid_durH= $date->format('H') ; 
     if ($vid_durH=="00") { 
      $vid_dur= $date->format('i:s') ; 
     } 
     else { 
      $vid_dur= $date->format('H:i:s') ; 
     } 
?> 

가 여기에 오류 메시지가 나는 코드를 테스트 한

Fatal error: Uncaught exception 'Exception' with message 'DateInterval::__construct() [dateinterval.--construct]: Unknown or bad format()'

+0

VidDuration을 반향하여 DateInterval이 어떤 오류를 발생시키는 지 확인하십시오. 메시지 ":"잘못된 요청 "}],"코드 ": 400,"메시지 ": – johnh10

+0

{"오류 ": {"오류 ": [{"도메인 ":"사용 제한 ","이유 ":" "Bad Request"}} –

+0

글쎄, 오류 메시지가 keyInvalid라고 쓰여져 있습니다. – johnh10

답변

2

의 일부입니다, 그것은 노력하고 있습니다. file_get_contents과 관련하여 문제가 발생했다고 생각하면 Google APIs Client Library for PHP을 사용하면 많은 Google API에 간단하고 유연하며 강력한 액세스를 제공 할 수 있습니다. 당신이 사용

는 요청 : 내가 설정 $videokey=UqyT8IEBkvY (24K 매직)

"https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$vidkey&key=$apikey" 

응답 :

"items": [ 
    { 
    "kind": "youtube#video", 
    "etag": "\"GM4ZnRh2gk1X1BLWgHklTm-3cgQ/tokAeZifZMO865fU_ytDkWOyIQ0\"", 
    "id": "UqyT8IEBkvY", 
    "contentDetails": { 
    "duration": "PT3M47S", 
    "dimension": "2d", 
    "definition": "hd", 
    "caption": "false", 
    "licensedContent": true, 
    "projection": "rectangular" 
    } 
    } 
] 

귀하의 코드 :

$VidDuration= "PT3M47S"; 
$date = new DateTime('2000-01-01'); 
$date->add(new DateInterval($VidDuration)); 

$vid_durH= $date->format('H') ; 

if ($vid_durH=="00") { 
      $vid_dur= $date->format('i:s') ; 
     } 
     else { 
      $vid_dur= $date->format('H:i:s') ; 
     } 

echo $vid_dur; 

enter image description here

희망이 도움이됩니다.