PHP:检查YT url是否有效以及视频是否存在

时间:2012-07-12 03:08:35

标签: php

功能目的是验证YouTube视频的网址并检查视频是否存在。这是我实际代码的片段。我操纵字符串到我想要的格式然后我继续检查它是否有效和存在。如果它通过测试,那么我回应结果。问题是我没有正确调用该函数。

即使视频确实存在,我也会收到此回音:

视频不存在或网址无效

已编辑:并添加了isValidURL功能 * 用于检查视频是否存在或无效的代码: *

 if($_POST)
    {

            // After applying url manipulation and getting the url in a proper format result = $formatted_url


function isValidURL($formatted_url) {

            $formatted_url = trim($formatted_url);
            $isValid = true;

            if (strpos($formatted_url, 'http://') === false && strpos($formatted_url, 'https://') === false) {
                $formatted_url = 'http://'.$formatted_url;
            }

            //first check with php's FILTER_VALIDATE_URL
            if (filter_var($formatted_url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === false) {
                $isValid = false;
            } else {
                //not all invalid URLs are caught by FILTER_VALIDATE_URL
                //use our own mechanism

                $host = parse_url($formatted_url, PHP_URL_HOST);
                $dotcount = substr_count($host, '.');

                //the host should contain at least one dot
                if ($dotcount > 0) {
                    //if the host contains one dot
                    if ($dotcount == 1) {
                        //and it start with www.
                        if (strpos($host, 'www.') === 0) {
                            //there is no top level domain, so it is invalid
                            $isValid = false;
                        }
                    } else {
                        //the host contains multiple dots
                        if (strpos($host, '..') !== false) {
                            //dots can't be next to each other, so it is invalid
                            $isValid = false;
                        }
                    }
                } else {
                    //no dots, so it is invalid
                    $isValid = false;
                }
            }

            //return false if host is invalid
            //otherwise return true
            return $isValid;
        }

        $isValid = getYoutubeVideoID($formatted_url);


            function isYoutubeVideo($formatted_url) {
                $isValid = false;
                    //validate the url, see: http://snipplr.com/view/50618/
                if (isValidURL($formatted_url)) {
                            //code adapted from Moridin: http://snipplr.com/view/19232/                
                    $idLength = 11;
                    $idOffset = 3;
                    $idStarts = strpos($formatted_url, "?v=");
                    if ($idStarts !== FALSE) {
                                    //there is a videoID present, now validate it
                        $videoID = substr($formatted_url, $idStarts + $idOffset, $idLength);
                        $http = new HTTP("http://gdata.youtube.com");
                        $result = $http->doRequest("/feeds/api/videos/".$videoID, "GET");
                                    //returns Array('headers' => Array(), 'body' => String);
                        $code = $result['headers']['http_code'];
                                    //did the request return a http code of 2xx?
                        if (substr($code, 0, 1) == 2) {
                            $isValid = true;
                        }
                    }
                }
                return $isValid;
            }

            $isValid = isYoutubeVideo($formatted_url);
            parse_str($parsed_url['query'], $parsed_query_string);
            $v = $parsed_query_string['v'];

            if ( $isValid == true ) {
                //Iframe code 
                echo htmlentities ('<iframe src="http://www.youtube.com/embed/'.$v.'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');

                //Old way to embed code
                echo htmlentities ('<embed src="http://www.youtube.com/v/'.$v.'" width="'.$wdth.'" height="'.$hth.'" type="application/x-shockwave-flash"  wmode="transparent" embed="" /></embed>');
                }
            else {

            echo ("The video does not exist or invalid url"); 

            }


    }
    ?>

1 个答案:

答案 0 :(得分:0)

您缺少isValidURL()功能。尝试更改此行:

if (isValidURL($formatted_url)) {   

if(preg_match('/http:\/\/www\.youtube\.com\/watch\?v=[^&]+/', $formatted_url, $result)) {

$test = parse_url($formatted_url);
if($test['host']=="www.youtube.com"){