致命错误:Uncaught TypeError:不能访问字符串上的字符串类型偏移量



我正在尝试创建一个网站,并利用YouTube API来显示最近的两个上传从我的YouTube频道。视频显示正确,但下面显示以下错误。

Fatal error: Uncaught TypeError: Cannot access offset of type string on string in D:xampphtdocsImperialSoundsWebsiteindex.php:95 Stack trace: #0 {main} thrown in D:xampphtdocsImperialSoundsWebsiteindex.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>

问题在于向$my_videos添加元素的两个循环添加的元素类型不同。foreach循环添加了一个关联数组:

$my_videos[] = array( 'v_id'=>$video['snippet']['resourceId']['videoId'], 
'v_name'=>$video['snippet']['title'] );

while循环只是添加了一个字符串:

$my_videos[] = $video['snippet']['resourceId']['videoId'];

出现错误的代码期望使用第一种元素,而不是第二种。你应该把第二个改成:

$my_videos[] = array('v_id' => $video['snippet']['resourceId']['videoId'],
'v_name' => $video['snippet']['title']);

最新更新