在Youtube Api上达到licensedContent值-PHP
为了在Youtube API上获得一致的音乐搜索回报,我将授权内容与非授权内容分开 调用中的Json如下所示:在Youtube Api上达到licensedContent值-PHP,php,arrays,json,api,youtube,Php,Arrays,Json,Api,Youtube,为了在Youtube API上获得一致的音乐搜索回报,我将授权内容与非授权内容分开 调用中的Json如下所示: { "kind": "youtube#videoListResponse", "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/Y0E2MZ3qwZc8Z7rZDINIYA1uY0I\"", "pageInfo": { "totalResults": 1, "resultsPerPage": 1 }, "items": [ { "
{
"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循环中的值。我的代码有什么问题吗
<?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>";
}
}
?>
您应该会收到一些警告,这些警告将帮助您解决此问题。有几个小问题:
foreach($video['items']作为$data){
-$video
应该是$video2
contentDetails
是一个对象,而不是一个数组。因此,通过对其执行foreach循环,您可以分别遍历每个属性名称。因此,没有任何属性将包含licensedContent
。其中一个属性将是licensedContent
,但由于我们知道需要该属性,因此可以直接访问它licensedContent
(扩展为$licensed
)是一个布尔值,并且设置为false
,echo
实际上不会输出任何可见的内容-这是PHP中echo命令的一个怪癖。我们可以通过检查值,然后回显适当的字符串来修复它foreach ($video2['items'] as $data) {
$licensed = $data['contentDetails']['licensedContent'];
echo ($licensed == false ? "false" : "true")."<br/>";
}
foreach($video2['items']作为$data){
$licensed=$data['contentDetails']['licensedContent'];
echo($licensed==false?“false”:“true”)。“
”;
}
现场演示: