php - 获取数组中的 youtube 频道数据
问题描述
我正在尝试使用该 api 获取 Youtube 频道数据
https://www.googleapis.com/youtube/v3/channels?part=snippet,contentDetails,statistics&id={ Channel Id }&key={ API }
我使用该代码来获取数据
if( $_SERVER[ 'REQUEST_METHOD' ] == 'GET' && !empty( $_GET ) ){
$channel_id = $_GET[ 'channel_id' ];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.googleapis.com/youtube/v3/channels?part=snippet,contentDetails,statistics&id=". $channel_id ."&key=AIzaSyCKAnI41bI4UGDJgYUcwrhrIfnjoD3uirM",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}
?>
<form id="channelIdForm" action="<?php $_SERVER['PHP_SELF']; ?>" method="get">
<input type="text" placeholder="Type channel id" name="channel_id" id="channelId" />
<button type="submit">Get data</button>
</form>
它看起来像那样
{ "kind": "youtube#channelListResponse", "etag": "\"DuHzAJ-eQIiCIp7p4ldoVcVAOeY/pss5QFB6saoevVwzX-8y6f1U75E\"", "pageInfo": { "totalResults": 1, "resultsPerPage": 1 }, "items": [ { "kind": "youtube#channel", "etag": "\"DuHzAJ-eQIiCIp7p4ldoVcVAOeY/gPFze0Hh7eRu9O5tXiTBVyvCwa8\"", "id": "UC4QYTjhWMxiC6UwvKLVkDDA", "snippet": { "title": "حفيد برايز حفيد برايز", "description": "", "publishedAt": "2018-04-01T17:18:31.000Z", "thumbnails": { "default": { "url": "https://yt3.ggpht.com/-wuTRH4WK_UA/AAAAAAAAAAI/AAAAAAAAAAA/ms_Qpu4ekLk/s88-c-k-no-mo-rj-c0xffffff/photo.jpg", "width": 88, "height": 88 }, "medium": { "url": "https://yt3.ggpht.com/-wuTRH4WK_UA/AAAAAAAAAAI/AAAAAAAAAAA/ms_Qpu4ekLk/s240-c-k-no-mo-rj-c0xffffff/photo.jpg", "width": 240, "height": 240 }, "high": { "url": "https://yt3.ggpht.com/-wuTRH4WK_UA/AAAAAAAAAAI/AAAAAAAAAAA/ms_Qpu4ekLk/s800-c-k-no-mo-rj-c0xffffff/photo.jpg", "width": 800, "height": 800 } }, "localized": { "title": "حفيد برايز حفيد برايز", "description": "" } }, "contentDetails": { "relatedPlaylists": { "uploads": "UU4QYTjhWMxiC6UwvKLVkDDA", "watchHistory": "HL", "watchLater": "WL" } }, "statistics": { "viewCount": "830460", "commentCount": "0", "subscriberCount": "3741", "hiddenSubscriberCount": false, "videoCount": "19" } } ] }
然后我尝试将该 json 转换为一个数组以使用索引从中选择我想要的内容
所以我尝试了,而不是 $response
$json = $response;
$arr = json_decode($json, true);
$row = array_reduce($arr['items'], function($c, $v){
$c[ key($v) ] = current($v);
return $c;
}, array());
print_r( $row );
我希望看到项目下面的所有索引,但结果是
Array ( [kind] => youtube#channel )
它显示只是一种我不知道有什么问题所以如果有人可以帮助我将不胜感激!
解决方案
该array_reduce
函数将数组减少为单个值。但您似乎对多个键/值对感兴趣。由于json_decode($json, true);
已经为您提供了关联的数组,您可以通过它们的名称检索特定的键。例子:
$arr = json_decode($response, true);
$newArray = [];
$requiredKeys = ['kind','etag'];
foreach ($requiredKeys as $key){
$newArray[$key] = $arr[$key];
}
推荐阅读
- r - R:具有趋势和约束系数的聚合 Cobb-Douglas 生产函数估计
- c# - 在 UWP 中使用 System.Reflection.Emit(使用 .NET 本机工具链)
- javascript - 为什么即使我点击第二个笔记,它也只编辑第一个笔记?
- android - 我们可以在 kubernetes pod 中运行 android 操作系统吗?
- csv - 从 youtube 链接返回标题
- html - 根 div 反应宽度与视口宽度不同
- javascript - 如何将我的全局状态导入 react-testing-library?
- java - 使用 NoClassDefFoundError 导入外部 JAR - 依赖项
- java - 在 Spring 中操作映射实体中的数据
- laravel - 使用属性在 laravel 中具有 eloquent 的模型字段映射