作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
我正在尝试创建一个网站并使用 YouTube API 来显示我的 YouTube channel 最近上传的两个视频。视频显示正确,但下方显示以下错误。
Fatal error: Uncaught TypeError: Cannot access offset of type string on string in D:\xampp\htdocs\ImperialSoundsWebsite\index.php:95 Stack trace: #0 {main} thrown in D:\xampp\htdocs\ImperialSoundsWebsite\index.php on line 95
第 95 行代码是:
echo '<iframe width="1280" height="720" src="https://www.youtube.com/embed/' . $video['v_id'] .'" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
全套代码:
<!DOCTYPE html>
<title>Home</title>
<html>
<header>
<meta charset="utf-8" />
<!--Link to style sheet-->
<link rel="stylesheet" href="stylesheet.css">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap" rel="stylesheet">
<div class="box-area">
<div class="wrapper">
<div class="logo">
<a href="#">Imperial Sounds</a>
</div>
<nav>
<a href="index.php">Home</a>
<a href="genres.php">Genres</a>
<a href="submityourmusic.php">Submit Your Music!</a>
<a href="about.php">About Us/Contact</a>
</nav>
</div>
</div>
</header>
<body>
<div class="banner-area">
<h2>Imperial Sounds Music</h2>
<h3 style="color: white; font-family: poppins;">Home for the latest and best copyright free music!</h3>
</div>
<div class="wrapper">
<div class="our-story">
<h2>Our Story</h2>
</div>
</div>
<div class="content-area">
<div class="content-img">
<img src="images/logos/youtubeicon.jpg" alt="icon" />
</div>
<div class="wrapper">
<p style="color: black; font-family: poppins; font-size: 28px; font-weight: bold;">Our Story</p>
<p style="color: black; font-family: poppins; font-size: 20px; display:inline">Imperial Sounds is a Digital Copyright Free Music Promotor. We share a wide range of different genres of music to suit many different needs for music producers and content creators.</p>
</div>
<div id="channel-data" class="col s6">
</div>
<div class="row" id="video-container">
<?php
$API_Url = 'https://www.googleapis.com/youtube/v3/';
$API_Key = 'AIzaSyCZZHuMnTS8q2hXs_-aeEl_lEJcNOX3mlg';
$channelId = 'UCk7cEDiU2CzRAgEmTUciK1A';
$parameter = [
'id'=> $channelId,
'part'=> 'contentDetails',
'key'=> $API_Key
];
$channel_URL = $API_Url . 'channels?' . http_build_query($parameter);
$json_details = json_decode(file_get_contents($channel_URL), true);
$playlist = $json_details['items'][0]['contentDetails']['relatedPlaylists']['uploads'];
$parameter = [
'part'=> 'snippet',
'playlistId' => $playlist,
'maxResults' => '2',
'key'=> $API_Key
];
$channel_URL = $API_Url . 'playlistItems?' . http_build_query($parameter);
$json_details = json_decode(file_get_contents($channel_URL), true);
$my_videos = [];
foreach($json_details['items'] as $video){
//$my_videos[] = $video['snippet']['resourceId']['videoId'];
$my_videos[] = array( 'v_id'=>$video['snippet']['resourceId']['videoId'],
'v_name'=>$video['snippet']['title'] );
}
while(isset($json_details['nextPageToken'])){
$nxt_page_URL = $channel_URL . '&pageToken=' . $json_details['nextPageToken'];
$json_details = json_decode(file_get_contents($nxt_page_URL), true);
foreach($json_details['items'] as $video)
$my_videos[] = $video['snippet']['resourceId']['videoId'];
}
//print_r($my_videos);
foreach($my_videos as $video){
echo '<iframe width="1280" height="720" src="https://www.youtube.com/embed/' . $video['v_id'] .'" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
}
?>
</div>
</body>
</html>
我是一名优秀的程序员,十分优秀!