php - 如何组合功能以获取多种类型的信息

标签 php json google-maps-api-3 youtube youtube-api

我使用Youtube Api来获取有关单个视频的信息。当我使用类似网址的请求时:

https://www.googleapis.com/youtube/v3/videos?part=status,snippet,contentdetails&id=$videoID&key=$apikey

我得到了我需要的所有信息。问题是我无法找到一种方法来一次性组合获得信息所需的功能。

我到目前为止所得到的:
function getDescription($videoID){
    $apikey = "<MYKEY>";

    $desc = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$videoID&key=$apikey");
    $description =json_decode($desc, true);
    foreach ($description['items'] as $videodesc)
   {
       $description= $videodesc['snippet']['description'];
   }
   return $description;
}

function getPublishedAt($videoID){
    $apikey = "<MYKEY>";

    $pub = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$videoID&key=$apikey");
    $publish =json_decode($pub, true);
    foreach ($publish['items'] as $published)
   {
       $publish= $published['snippet']['publishedAt'];
   }

   $publish = new DateTime($publish);
   $publish = $publish->format('Y-m-d H:i:s');

   return $publish;

}

echo "<br>Description: ";
echo getDescription("<VIDEO-ID>");
echo "<br>PublishedAt: ";
echo getPublishedAt("<VIDEO-ID>");

因此,此代码有效,但是我想向api发出一个请求(使用url中的多个部分)并获取信息。

有谁知道如何仅使用一个功能执行此操作?

最佳答案

好吧,我自己找到了答案:

$videoinfo = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=<video-id>&part=snippet,status,contentDetails,statistics&part=statistics&key=<api-key>");
   $videoinfo =json_decode($videoinfo, true);
   foreach ($videoinfo['items'] as $embed)
   {
       $emb= $embed['status']['embeddable'];
       $license= $embed['status']['license'];
       $duration= $embed['contentDetails']['duration'];
   }
echo $emb;   
echo $license;
echo $duration;

这不是一个函数,但这将执行公制:)

关于php - 如何组合功能以获取多种类型的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39589204/

相关文章:

php - UNION ALL 运算符的意外行为

json - 如何在复杂的JSON对象中只保留一组指定的字段?

google-maps - 将Google Maps MarkerClusterer与infowindow集成

google-maps-api-3 - 通过网络服务访问 Google 的流量数据

php - 如何从mysql表中选择包含至少一个大于搜索值的值的所有行?

php - SQLSTATE[HY000] : General error: 1364 Field 'uID' doesn't have a default value

php - 在 PHP 中生成 excel 输出的最佳方式是什么?

c# - 无法隐式转换类型 'System.Collections.Generic.List

java - 如何在 Java 的 mongodb 中从 BasicDBList 创建 List<String>?

google-maps - 如何通过谷歌地图 API 获取一个国家/地区的州列表?