Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取视频持续时间在YouTube API v3中不起作用_Api_Youtube_Youtube Api_Youtube Data Api - Fatal编程技术网

获取视频持续时间在YouTube API v3中不起作用

获取视频持续时间在YouTube API v3中不起作用,api,youtube,youtube-api,youtube-data-api,Api,Youtube,Youtube Api,Youtube Data Api,自2016年3月以来,我一直在使用YouTube API v3使用下面的代码获取视频持续时间($vid_dur),并且没有任何问题,直到今天它在我这边没有任何干预的情况下工作为止。有什么建议吗 <?php $vidkey = "xxxxxxxxx" ; $apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ; $dur = file_get_contents("https://www.googleapis.com/youtube/

自2016年3月以来,我一直在使用YouTube API v3使用下面的代码获取视频持续时间($vid_dur),并且没有任何问题,直到今天它在我这边没有任何干预的情况下工作为止。有什么建议吗

<?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') ;  
        } 
?>

下面是错误消息的一部分

致命错误:未捕获的异常“exception”带有消息 'DateInterval::u构造()[DateInterval.-construct]:未知 或错误的格式()'


我已经测试了你的代码,它正在工作。我认为您在
文件获取内容方面遇到了问题,您可能希望使用,因为它提供了对许多Google API的简单、灵活、强大的访问

使用您的请求: 其中我设置了
$videokey=UqyT8IEBkvY
(24K魔术)

答复:

 "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;


希望这有帮助。

回显VidDuration以查看DateInterval抛出的错误。{“错误”:{“错误”:[{“域”:“usageLimits”,“原因”:“keyInvalid”,“message”:“Bad Request”}],“code”:400,“message”:“Bad Request”}}错误消息说keyInvalid,所以请仔细查看。
$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;