2017-01-11 7 views
0

YouTube API v3を使用して2016年3月から動画の再生時間($ 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がどのようなエラーをスローするかを確認します。 "code":400、 "message": – 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

これが役に立ちます。

関連する問題