最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Search youtube and get top video id - Stack Overflow

programmeradmin4浏览0评论

I would like to search youtube with a string, and get the id for the top video in the search result, so I can directly play that video on my website. Is this possible?

I only have the url for searching: youtube_url/results?search_query=thevideotitlehere

Should I use javascript/jquery/php/youtube-api?

I would like to search youtube with a string, and get the id for the top video in the search result, so I can directly play that video on my website. Is this possible?

I only have the url for searching: youtube_url/results?search_query=thevideotitlehere

Should I use javascript/jquery/php/youtube-api?

Share Improve this question asked Jun 7, 2014 at 23:23 mowglimowgli 2,8694 gold badges34 silver badges69 bronze badges 2
  • Is this possible? ..did you try it? WHere are your code attampts? What were your findings? – charlietfl Commented Jun 7, 2014 at 23:34
  • 1 Did I try what? I said "I only have the url".. I have been googling for a day, without ing much closer – mowgli Commented Jun 8, 2014 at 9:42
Add a ment  | 

3 Answers 3

Reset to default 3

I'm not formilliar with the Youtube api. But I had to do something simular a while ago so I'm going to use the same approach here.

The idea is simple. First obviously we get the search string from the visitor. Than we build up a correct Youtube link. Using file_get_contents we fetch the source from the link. With a bit of regex we search for the first video id occurence inside this source and save it inside a variable.

Now all we have to do is output a standard Youtube embedding with the video id we fetched earlier.

I'm sure there are better ways to do this though.

<?php

if(isset($_POST['submit']) && $_POST['submit'] != ''){
    $searchString   = $_POST['searchString'];
    $correctString  = str_replace(" ","+",$searchString);
    $youtubeUrl = "https://www.youtube./results?search_query=". $correctString;
    $getHTML        = file_get_contents($youtubeUrl);
    $pattern        = '/<a href="\/watch\?v=(.*?)"/i';

    if(preg_match($pattern, $getHTML, $match)){
            $videoID    = $match[1];
    } else {
            echo "Something went wrong!";
            exit;
    }


    echo '<iframe width="560" height="315" src="//www.youtube./embed/'. $videoID .'" frameborder="0" allowfullscreen></iframe>';
    }

?>


<!DOCTYPE HTML>
<html>
    <head>
            <title>Fetch Youtube first result</title>
    </head>
    <body>
            <form method="post" action="index.php" accept-charset="utf-8">
                    Search string: <input type="text" name="searchString" />
                    <input type="submit" name="submit" value="Search" />
            </form>
    </body>
</html>

After many hours of searching, coding and trying, I have a solution

Use YouTube API, in a very simple way

Enable google youtube API and setup an API key

Then use this to get the id of the top video in a search (I'm using PHP, but I bet you could use something like JavaScript or others too):

<?php
$yt_search = 'Godzilla trailer 2014';
$yt_source = file_get_contents('https://www.googleapis./youtube/v3/search?part=snippet&maxResults=1&order=relevance&q='.urlencode($yt_search).'&key=YOUR_API_KEY_HERE');
$yt_decode = json_decode($yt_source, true);
if ($yt_decode['pageInfo']['totalResults']>0) {
    if (strlen($yt_decode['items'][0]['id']['videoId'])>5) {
        $yt_videoid = trim($yt_decode['items'][0]['id']['videoId']);
        echo $yt_videoid;
    }
}
?>

This works:

youtube_url/embed/?listType=search&list=SEARCHWORD&autoplay=1

But it does not autoplay

发布评论

评论列表(0)

  1. 暂无评论