Syndicate

Syndicate content

Displaying Random YouTube Video with PHP

HTML, sure...CSS, yup...PHP, not so much.

Since I don't put any energy into learning PHP, I like finding snippets of code that I can easily tweak and post to a website. Below is a PHP script which will randomly choose a YouTube video (from a list of videos you place in a file) and display it on a webpage.

Why would you want to do that? If you have a website within a certain niche, music for example, you could create a list of your favorite music videos or live songs and have one randomly chosen to be displayed on the homepage of your website. This gives users a new experience (if your list is big enough) every time.

Here's how to do it.

1) First, create a txt file which you'll place your videos in, in this example I name it videos.txt.

2) Navigate to a YouTube video. Let's say you want to share with your visitors that there are alien bases on the far side of the moon, and so you want this video to come up periodically: http://www.youtube.com/watch?v=R6QNzH4x1rY

Grab the string of text from the URL after "v=", in the above case "R6QNzH4x1rY" and place it in the videos.txt file. Each of these strings should be placed on a new line.

3) Upload your file to your web server.

4) Where you want the video to display on your page, place the below code. Remember to change the file location to match where you placed your txt.

<?php

$file = '/CHANGE-ME/videos.txt';

$data = file($file);
$line = trim($data[array_rand($data)]);

echo <<<YT
<object width="178" height="135">
<param name="movie" value="http://www.youtube.com/v/$line"></param>
<param name="wmode" value="transparent"></param>
<embed src="http://www.youtube.com/v/$line" type="application/x-shockwave-flash" wmode="transparent" width="178" height="135"></embed>
</object>
YT;

?>

In the above example you can change the size of the YouTube video my simply adjusting the width and height variables. You'll want it to work nicely with your site's design, so do adjust.

Comments

Just what I was looking for!

I was looking for a guide like this.. Thanks!
btw I hid my youtube embed frame.. but I'd like to add the title of the video next the the video ending URL.. would you know how to do this..

for example video.txt

| erFQayhz Cats killing dogs

How can I grab the part for the URL and the other for the title. obviously can't put them in two separate .txt files or else the title and video would be different.. I'm stumped here.. as I know little about php.

Much thanks.
~ 12345

it do not work for me!

it do not work for me! http://hackkings.com/video.html it is white!!

what do i wrong?

php file extension...

Thanks for trying. I think I know what's going on, and it's something I neglected to place in the post.

To get this to work you must have php as the file extension for the page the code is on. So in your example, you'll want the page to be video.php (not video.html).

Give that a shot and report back if you're successful.

Thanks!

i have also a prolem it

i have also a prolem it say:

Warning: file(/videos.txt) [function.file]: failed to open stream: No such file or directory in /home/gkjtvbxc/domains/hackkings.com/public_html/video.php on line 3

Warning: array_rand() [function.array-rand]: First argument has to be an array in /home/gkjtvbxc/domains/hackkings.com/public_html/video.php on line 4

and if i make $file = '/videos.txt'; To $file = 'videos.txt';
thane is it white!

double check file location

Double check to make sure you have the file in the right location. I've seen on some servers you have to put everything up to /public_html, but on the servers I have I do not include any folders before public_html

So, if a file location is:
/home/gkjtvbxc/domains/hackkings.com/public_html/videos/videos.txt
all I would actually place in the file location is:
/videos/videos.txt

Let me know if that's the issue. :)