在Youtube Api上达到licenseContent的价值-PHP [英] Reach licensedContent value on Youtube Api - PHP

查看:150
本文介绍了在Youtube Api上达到licenseContent的价值-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在YouTube网站API上获得一致的音乐搜索回报,我求助于将许可内容与非许可内容分开.

In an effort to get consistent music search returns on Youtube API, I resorted to separate licensed content to non-licensed one.

通话中的Json是这样的:

The Json from the call is this:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/Y0E2MZ3qwZc8Z7rZDINIYA1uY0I\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#video",
   "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/Xup77LEmvulitH-oe1DkTBPumV4\"",
   "id": "plIZho8Nd2g",
   "contentDetails": {
    "duration": "PT4M34S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "true",
    "licensedContent": false,
    "projection": "rectangular"
   }
  }
 ]
}

但是我似乎无法达到PHP foreach循环中的值.我的代码有什么问题吗?

But I can't seem to reach the value in the PHP foreach loop. Is anything wrong with my code?

<?php

$api = "MY API KEY";

$link2 = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=plIZho8Nd2g&key=" . $api;

$video2 = file_get_contents($link2);

$video2 = json_decode($video2, true);


foreach ($video['items'] as $data) {
    foreach ($data['contentDetails'] as $data2) {
        $licensed = $data2['licensedContent'];

        echo $licensed . "<br>";


    }

}

?>

推荐答案

您应该收到一些警告,可以帮助您解决此问题.有一些小问题:

You should be getting some warnings which would help you solve this. There are a few little issues:

  1. 错别字:foreach ($video['items'] as $data) {-$video应该为$video2

contentDetails是一个对象,而不是数组.因此,通过在其上执行foreach循环,您可以分别遍历每个属性名称.因此,所有属性都不会包含 licensedContent.其中一个将成为 licensedContent,但是由于我们知道我们想要那个,因此我们可以直接访问它.

contentDetails is an object, not an array. So by doing a foreach loop on it you're looping through each property name individually. Therefore none of the properties will contain licensedContent. One of them will be licensedContent, but since we know we want that one, we can just access it directly.

由于licensedContent(以及扩展名$licensed)是布尔值,并且设置为false,因此echo实际上不会输出任何可见的内容-这是PHP中echo命令的一个怪癖.我们可以通过检查值然后回显适当的字符串来解决此问题.

Since licensedContent (and by extension $licensed) is a boolean, and is set to false, echo will not actually output anything visible - this is a quirk of the echo command in PHP. We can fix it by checking the value and then echoing an appropriate string.

将所有内容放在一起,这就是您得到的:

Put all that together and this is what you get:

foreach ($video2['items'] as $data) {
    $licensed = $data['contentDetails']['licensedContent'];
    echo ($licensed == false ? "false" : "true")."<br/>";
}

此处的实时演示: http://sandbox.onlinephpfunctions.com/code/7ca099cc3e86ccb4bd49038ad

Live demo here: http://sandbox.onlinephpfunctions.com/code/7ca09960ec3e86ccb4bd49038ad851c3c93ef5de

这篇关于在Youtube Api上达到licenseContent的价值-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆