/**
* This method searches YouTube and potentially eventually other video services for a video
* clip featuring the passed in subject. Then it returns the HTML for an embeddable player
* to play that video.
*
* @param string
*
* @return string
*/
public function embeddableVideoClipFor($searchString)
{
// Previous experience revealed that video search is not perfect, in that for given
// keywords the top result isn't always accurate.
$embeddableVideoClipHTML = NULL;
// Further details on searching YouTube http://www.ibm.com/developerworks/xml/library/x-youtubeapi/
// This was working well for over two years but had to be revised to use version 2 of API
// May switch to Zend or version 3.0 of Google/YouTube API...
$vq = $searchString;
$vq = preg_replace('/[[:space:]]+/', ' ', trim($vq));
$vq = urlencode($vq);
$feedURL = 'http://gdata.youtube.com/feeds/api/videos?q=' . $vq . '&safeSearch=none&orderby=relevance&v=2';
// read feed into SimpleXML object
try
{
$youTubeXML = simplexml_load_file($feedURL);
}
catch(Exception $e)
{
// This rarely throws an error, but when it does, I just pretend I can't find a video clip
$youTubeXML = NULL;
}
if(($youTubeXML != NULL) && ( ! empty($youTubeXML->entry->link[0]['href'])))
{
$videoLink = $youTubeXML->entry->link[0]['href'];
// This is not enough, I need to trim the beginning and end off this to just get the video code
$trimedURL = str_replace('http://www.youtube.com/watch?v=', '' , $videoLink);
$videoCode = str_replace('&feature=youtube_gdata', '', $trimedURL);
$embeddableVideoClipHTML = '<iframe id="ytplayer" type="text/html" width="640" height="360" src="https://www.youtube.com/embed/' . $videoCode . '"frameborder="0" allowfullscreen>';
}
return $embeddableVideoClipHTML;
}