Form to let users post a youtube video

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

It is working now, I found out that I had another name for the video_submit file :oops: :banghead:

Thank you very much for helping me. I've learned a lot from this :D

And now I'm going to concentrate on my new study, which will contain php in the next year :D We're doing html + css now.

Thanks again, and I hope to see you again if I have future problems!
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

I am very glad you managed to sort it you... Can't believe you had a different name for your file... lol You noob! lol

Glad I could help you out buddy. It took a while but eventually you got there in the end.

I will blogging about this one on my site so if you dont mind I will link to your site if you can provide me with a link that you want.

I found this interesting helping you and it would make an interesting blog post. lol

I assume it is live on your website now?

Best wishes
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

Here I am again :mrgreen: I hope you still follow this topic.

Answer to your previous post: You may use it for your blog, I have no problems with that. I would even provide you a link to my website, but it is in a member section, and not everybody can register.

New question :D : My friend said, that it would be great if the show_video file wouldn't be just a list, but would show only thumbnails of the videos. I don't know if this is possible via a standard youtube script or it has to be written specially.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

The first thing I would need to know is if you are using the exact script that I coded or are you using some altered script on your site.

You can get Youtube to show thumbnails or you can use an API to get the thumbnails and other information like title, description, views etc.

Best wishes,
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

Thanks for looking :) This is the script I currently use. I think it is your original script:

Code: Select all

<?php
// Build query to get results of video Title & Link
$result = mysql_query("SELECT * FROM uservideos") // Select all results from table uservideos
or die ('cannot select table:' .msql_error()); // If error then stop and display mysql error code

// Begin to build table for sql results
echo "<table border=\"0\" width=\"50%\" >"; // initial table width 50% of page
echo "<tr><th width=\"100%\">Titel</th>"; // Title width share is 100%

// Display results in your table
while ($row =mysql_fetch_array($result)){ // Fetch your results and place in an array to call each value
               
        // Make sure you include your css file for your class="example6" to work with the popup function for each video    
        echo "<tr><td>";
        echo "<a class=\"example6\" href=\"".$row['Link']."\" title=\"".$row['Title']."\">".$row['Title']."</a>"; // Includes your CSS class      
        echo "</td></tr>";
        }
                echo "</table>";
?>  
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

I'm connecting with the database with include "connect.php";

connect.php:

Code: Select all

<?
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);

$dbservertype='mysql';

$servername='*************';

$dbusername='***************';
$dbpassword='*************';

$dbname='*************';

connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
?>
And it is working fine. Or is this not what you meant?
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

Hmm, why did the person above me delete it's post? Anyway, I think it should be something like this:
http://www.reelseo.com/youtube-thumbnail-image/

But I don't know how to implement it.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

Its simple really buddy, instead of showing the list format you show the image using an IMG tag and the source would be that link etc. If you remember you asked me to code a script that extracts the video link and changes it to the correct format. So within that script you already have a string value with the video id. So just take that value and save it into the database along with the link but you save the image link format with it.

So you would have the video link saved, the description/title and the image link etc.

Then when you call your list you can call the original link, title and the screenshot you want.

You have the coding there to do it. All you have to do is copy and paste it and change to your requirements.

Have a go and come back to me about it buddy.

Best wishes
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

Hi, sorry it took so long to react. But my friend said (with whom i'm making the new website) that he wanted to code a little piece of code, since I did everything :P Anyway, I made the insert_videos.php file add the stripped link to the database, so that I can easely call it from the database, without the need to strip it again. And now my friend is going to make the script for the thumbnails, where you need the video id. I'll report when the script is finished, or when I have new problems. It is probably going to take a little longer, because I expect that the script that he makes is going to be a disaster :P And I will try to make it work good :)

Here's the new insert_videos.php:

Code: Select all

<?php

					 require_once("connect.php"); // Database connectie bestand

					 // Verkrijgt hetgene wat de gebruiker heeft ingevvuld
					 $Title = $_POST["Title"]; // Pakt de titel
					 $Link = $_POST["Link"]; // Pakt de link

					 // Set error location link values for Title & Link
					 $error1 = "uploadvideos.php?e=1&Title=" . $Title . "&Link=" . $Link; // Sets the string value for error redirect on Title
					 $error2 = "uploadvideos.php?e=2&Title=" . $Title . "&Link=" . $Link; // Sets the string value for error redirect on Link
					 $error3 = "uploadvideos.php?e=3&Title=" . $Title; // Sets the string value for error redirect on Link but leaves the incorrect Link out

					 // Check to see user values have something in them
					 if (empty($Title)) {
                header("Location: $error1"); // Direct them back to your form with error Title missing
								} elseif (empty($Link)) {
            header("Location: $error2"); // Direct them back to your form with error Link missing
						} else {
       
       
        		// ************************************** NEW SECTION - CHANGE LINK *************************************
       
        
				//$Link = "http://www.youtube.com/watch?v=WXy1KhUQMug"; // Link submitted by user

				$Find = "watch?v="; // Value to find in $Link

				$Locate_Find = strpos($Link, $Find); // Find location of $Find and moves starting point to end of $Find

				if ($Locate_Find == TRUE) { // Sets if statement $Find string value found equals TRUE
       
			 $Locate_Find = strpos($Link, $Find) + strlen($Find); // Find location of $Find and moves starting point to end of $Find

			 //echo $Locate_Find . "<br/> "; // Displays value location of $Find

			 $Strip_Link = substr($Link, $Locate_Find); // Strips everything before $Find including $Find value

			 //echo $Strip_Link . "<br />"; // Displays new string value $Link

			 $New_Link = "http://www.youtube.com/embed/" . $Strip_Link; // Create your new link to be save to the database

			 //echo $New_Link; // Displays new value of $Link

			 // Insert the values 'Title', 'Link' in your columns within your database (uservideos) table (????????)
        $insert = "INSERT INTO uservideos (Title, Link, Video_Id) VALUES  ('$Title', '$New_Link', '$Strip_Link')"; // Create string of required actions
                mysql_query($insert) OR DIE (mysql_error()); // Saves values in to database or stops on mysql error

            header("Location: confirmupload.php"); // redirects to confirmation page
						}

			 // Only executes this if the $Find value is not found in your $Link value and redirects the user to your video_submit.php with error code 3

			 else { header("Location: $error3"); } // Direct them back to your form with error Incorrect Link Format
			 }

			 ?>
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

Right, so your still using my original code then yeah. lol Just changed the name of the files etc where needed and just added the $Strip_Link to the insert query so you can call the actual video code link and place that in the simple link that youtube allows you to do.

You see YouTube creates thumbnails for you so all you have to do is code the thumbnail link and add the video code you have in your variable to it. Then when displaying the thumbnail you use IMG tag and there you have it. The main work is deciding what type of layout you would like the thumbnails to appear in. But that is not complicated either as you already have most of your site built. So just build the layout into your CSS file and your good to go.

So are you saying you have a friend who is going to try code the thumbnail section and add to the above code as that would be wrong wouldn't it. The above code is the insert video link to the database. In my original code the file name was display_links.php and that code requires the thumbnails yeah.

So are you having a go yourself or are you getting your friend to do it now and then are you going to post his efforts on this thread for me to check or something? lol

I am sure your friend will code it great for you. But I agree in getting a third person to look over the code.

You might want to take a look at this link: http://code.google.com/apis/youtube/2.0 ... e_php.html as that will guide you through the PHP of what you want using API.

An example would be:

Code: Select all

/* Some stuff goes before here to initialize API connection */

   /* Then get started: */
   $yt = new Zend_Gdata_YouTube()
   $videoEntry = $yt->getVideoEntry('the0KZLEacs'); //Video-ID
   $videoThumbnails = $videoEntry->getVideoThumbnails()

   echo 'Video: ' . $videoEntry->getVideoTitle() . "\n";
   echo 'Video ID: ' . $videoEntry->getVideoId() . "\n";
   echo 'Updated: ' . $videoEntry->getUpdated() . "\n";
   echo 'Description: ' . $videoEntry->getVideoDescription() . "\n";
   echo 'Category: ' . $videoEntry->getVideoCategory() . "\n";
   echo 'Tags: ' . implode(", ", $videoEntry->getVideoTags()) . "\n";
   echo 'Watch page: ' . $videoEntry->getVideoWatchPageUrl() . "\n";
   echo 'Flash Player Url: ' . $videoEntry->getFlashPlayerUrl() . "\n";
   echo 'Duration: ' . $videoEntry->getVideoDuration() . "\n";
   echo 'View count: ' . $videoEntry->getVideoViewCount() . "\n";
   echo 'Rating: ' . $videoEntry->getVideoRatingInfo() . "\n";
   echo 'Geo Location: ' . $videoEntry->getVideoGeoLocation() . "\n";
   echo 'Recorded on: ' . $videoEntry->getVideoRecorded() . "\n";
The link your after is: http://img.youtube.com/vi/VIDEO_ID/default.jpg

You would then add your video id in replace of it like this:

Code: Select all

echo "<a class=\"example6\" href=\"".$row['Link']."\" title=\"".$row['Title']."\"><img src=\"http://img.youtube.com/vi/".$row['Video_Id']."/default.jpg\" alt=\"\"/></a>"; // Includes your CSS class
You should get the idea from that anyway.

Best wishes
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

I tried something nice. But it's not really working out. This is the current code:

Code: Select all

Look down for update.
What I want here is, set the thumbnail to float left, and then the video title and description will be displayed right of the thumbnail. But when I try to open the page it says:

Fatal error: Class 'Zend_Gdata_YouTube' not found in /home/a6121237/public_html/videos.php on line 94

So I looked up the link you gave in the above post, and found this:

"The PHP client library can be used to retrieve public feeds or to execute authenticated operations. All public feeds are read-only and do not require any authentication. Authenticated operations, on the other hand, include the retrieval of private feeds, such as a user's inbox feed, as well as write, upload, update and delete operations. You will need to sign up for a developer key to be able to execute authenticated operations."

I have a developer key. But I can't find anywhere how I can add the class or let the script recognize the class. Maybe I'm looking wrong. I know that in Java, if you want to use something specific, you need to import it first. Maybe it's the same case here?

And I also think that this line does not have to be in the while loop, but I cannot test that now, because of the above error.

Code: Select all

 $yt = new Zend_Gdata_YouTube();
EDIT: now I think I know what it is, I need to include some file. Googling more.... :)

EDIT2: So after some googling, I found out that I had to download this: http://framework.zend.com/download/gdata/
And then include something. This is the current code of display_videos.php:

Code: Select all

<?php	
					 
					    require_once 'Zend/Loader.php';
							Zend_Loader::loadClass('Zend_Gdata_YouTube');

					    $yt = new Zend_Gdata_YouTube();
													
					 		// Build query to get results of video Title & Link
					 		$result = mysql_query("SELECT * FROM uservideos") // Select all results from table uservideos
					 		or die ('cannot select table:' .msql_error()); // If error then stop and display mysql error code

					 		// Begin to build table for sql results
					 		echo "<table border=\"0\" width=\"60%\" >"; // initial table width 60% of page
					 		echo "<tr><th width=\"10%\"></th>"; // Title width share is 100%

					 			// Display results in your table
					 		while ($row =mysql_fetch_array($result)){ // Fetch your results and place in an array to call each value
														 
							 $videoEntry = $yt->getVideoEntry($row['Video_Id']);
               
        			 // Make sure you include your css file for your class="example6" to work with the popup function for each video    
        			 echo "<tr><td>";
        			 echo "<a class=\"example6\" href=\"".$row['Link']."\" title=\"".$row['Title']."\"><img class=\"fleft\" src=\"http://img.youtube.com/vi/".$row['Video_Id']."/default.jpg\" alt=\"\"/></a> '<b>Titel:</b> ' .$videoEntry->getVideoTitle(). <br> '<b>Beschrijving:</b> ' .$videoEntry->getVideoDescription()."; // Includes your CSS class     
        			 echo "</td></tr>";
        			 }
               echo "</table>";
							 ?>
But now I'm getting this error:

Fatal error: Uncaught exception 'Zend_Gdata_App_InvalidArgumentException' with message 'Property getVideoTitle does not exist' in /home/a6121237/public_html/Zend/Gdata/App/Base.php:484 Stack trace: #0 /home/a6121237/public_html/videos.php(104): Zend_Gdata_App_Base->__get('getVideoTitle') #1 {main} thrown in /home/a6121237/public_html/Zend/Gdata/App/Base.php on line 484

EDIT3: Changed the long line into this:

Code: Select all

echo "<a class=\"example6\" href=\"".$row['Link']."\" title=\"".$row['Title']."\"><img class=\"fleft\" src=\"http://img.youtube.com/vi/".$row['Video_Id']."/default.jpg\" alt=\"\"/></a><b>Titel:</b> ".$videoEntry->getVideoTitle()." <br> <b>Beschrijving:</b>  ".$videoEntry->getVideoDescription()." "; // Includes your CSS class  
And now it's working :D It was, of course, an error in the quotes....

It is now fully working. Thanks for your help :) Even if I did this one for a big part on my own :)

Do you want a link to the result?
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

Ok, first of all you need to stop and think what actual data you are after.

All you want is to display a thumbnail of the video. So all the above code is not required to do that as YouTube create thumbnails as standard and you have the option of selecting a thumbnail when uploading the video.

So lets start from the beginning and get your thumbnail working with a simple change in the link code:

Code: Select all

echo "<a class=\"example6\" href=\"".$row['Link']."\" title=\"".$row['Title']."\"><img src=\"http://img.youtube.com/vi/".$row['Video_Id']."/default.jpg\" alt=\"".$row['Title']."\"/></a>"; // Includes your CSS class
That will give you your thumbnail right away and that will be the default thumbnail YouTube has created for you. You already have the title that the user has submitted. So all you would need really is maybe a user name for the 'Who uploaded' text maybe.

Then you could have your thumbnail on the left, then the Title and Username on the right etc. If you want more details just add details to the form when the user first submits the video link. You are giving them an option then to fill in the details if they want. If not you code it where if $string is NULL then enter default text such as Description not supplied. Or you could make it compulsary for the user to enter the details before the uploaded video is inserted into your database.

DONT FORGET! One important thing you need to remain in control of is your website content. Now by grabbing details such as the title, description and other details directly from YouTube you are basically displaying whatever that user has typed as a description etc. So what I personally would want is something entered into my database, then I would have a column which has say a zero assigned as default. All zero's mean admin to check before going live, then when admin has checked the link they can change the 0 to a 1. Then when your display_video.php file shows the thumbnails etc it will only show the ones with a number 1 in that column.

I think for what you need you are going about it a very long way. That simple line of code I just wrote above gives you your thumbnail. Then its just a matter of CSS to get your preferred layout.

Why dont you just send me the link anyway so I can take a look at what your seeing but really, your going the long way around when you can actually see your destination right in front of you buddy.

Best wishes
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

I don't see why you say "your going the long way around when you can actually see your destination right in front of you buddy". Because my solution seems the easiest one to me. And the users who place content, are not some random people (trollers) who will fill in stupid things and think that's funny. But maybe I'm understanding it wrong.

Anyway, you can have the link, please note that it is a dutch website, and currently in development, and also on a test hosting.
www.meukband.comyr.com/videos.php
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: Form to let users post a youtube video

Post by phphelpme »

Hey I meant do offence by my last post buddy. I was merely comparing one line of code which gives you your thumbnail to you using a framework and adding the additional code and server processes to your pages.

So this new website is not actually your gaming website I see. So your working on a couple of sites then and trying to incorporate the display video code on them.

It clearly is working as you stated in your previous post and well done for getting it to work using your chosen framework buddy. I did not meant to take anything away from your success in coding or your hard work completing this.

Is that your band which you play in yourself or is it another project you have been working on buddy?

Best wishes
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Form to let users post a youtube video

Post by stijn22 »

One band member asked me to make it, because they don't have a big budget and thus couldn't afford a professional team of webdesigners. I'm making it for a small profit :) And I'm enjoying it a lot. I don't play in the band myself. And if you go to www.meukband.nl then you can see the reason why they asked an outsider :P

it is indeed, an other site then the clan site. The clansite is now being managed by a clan member, I've learned him the very basics of html :)

The current site is also a project for school :P Where everybody uses extremely simple html, I use php and databases. I hope I get a nice grade for this :)
Post Reply