Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Uncaught TypeError: Cannot access offset of type string on string [closed]

Tags:

html

css

php

I am trying to create a website and making use of the YouTube API to display the most recent two uploads from my YouTube Channel. The videos are displaying correctly but the following error is displayed below them.

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

Line 95 code is:

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>';

The full set of code:

<!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>
like image 882
Imperial Sounds Avatar asked Mar 14 '26 22:03

Imperial Sounds


1 Answers

The problem is that the two loops that add elements to $my_videos are adding different types of elements. The foreach loop adds an associative array with:

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

The while loop just adds a string with:

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

The code with the error expects the first kind of element, not the second. You should change the second to:

$my_videos[] = array('v_id' => $video['snippet']['resourceId']['videoId'],
                     'v_name' => $video['snippet']['title']);
like image 134
Barmar Avatar answered Mar 16 '26 10:03

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!