Posts Tagged ‘Video’
This is just a teaser for a series of articles on dynamically creating webpages on the fly based on keywords. Whether it’s based on a list of related keywords to your target set or simply based on what a user is searching for, the more content that you can provide related to the term, the better off your chances of it being ranked well in the serps (not to mention will provide a better user experience).
Here’s an easy one for you, it will pull a youtube video based on whatever keyword you give it. For use in a wordpress installation, make sure to enable the Exec-PHP plugin before inserting the code.
There are two parts to this, the youtube class and then the actual code to call it and display the video. This was taken long ago from a post on WF, though has been slightly modded. I forget who originally provided it but thanks. :)
Instead of having one huge php file with all the various functions, I’ve found it easier to separate each task into separate class files, that way on the actual page I only need to use the include() function and call up the script using my keyword.
Create a file called youtube_class.php and insert the following code:
<?php
class youtube{
function get_youtubevideo($theproduct){
///do youtube stuff
$theproductencoded=urlencode($theproduct);
$apicall = "http://gdata.youtube.com/feeds/videos?vq=$theproductencoded&max-results=1";
//Initialize the Curl session
$ch = curl_init();
//Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Set the URL
curl_setopt($ch, CURLOPT_URL, $apicall);
//Execute the fetch
$data = curl_exec($ch);
//Close the connection
curl_close($ch);
$xml = new SimpleXMLElement($data);
// Check to see if the XML response was loaded, else print an error
$contentcenter.="<div class=\"leftside\">";
//show the video if a video was found
if (!empty($xml->entry)) {
$media = $xml->entry->children('http://search.yahoo.com/mrss/');
$vidtitle= $media->group->title;
$vid= $media->group->content->attributes()->url;
$contentcenter.="<h1>Video for " . str_replace("-"," ",ucwords($theproduct)). "</h1>";
$results = '';
// If the XML response was loaded, parse it and build links
$vid = str_replace("?v=", "v/", $vid);
// For each SearchResultItem node, build a link and append it to $results
$contentcenter.= "<object width=\"425\" height=\"355\">
<param name=\"movie\" value=\"$vid\"></param>
<param name=\"wmode\" value=\"transparent\"></param>
<embed src=\"$vid\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"355\"></embed>
</object>";
}
// If there was no XML response, print an error
else {
//$results = "Dang! Must not have got the XML response!";
$contentcenter .= "<h2>Error: No video found</h2>";
}
$contentcenter.="</div>";
return $contentcenter;
}
}
?>
Now on whatever page you’d like to display the youtube video, simply insert the following code:
include_once("youtube_class.php");
$theproduct = 'topic';
$videoshow = new youtube();
echo $videoshow->get_youtubevideo($theproduct);
Typically you’ll have the keyword that you’re using to generate the dynamic page already stored as a variable, in which case you can just replace $theproduct = ‘topic’; with your variable(i.e. $searchkeyword).
I can’t stop watching this clip over and over again… it’s a little off-topic but very cool none-the-less. It’s a video named “Inspired Bicycles” and it’s just that, bike riders doing unimaginable stunts set to a killer soundtrack (Band 0f Horses – The Funeral). See for yourself, these guys are incredible!