2012-07-12 67 views
2

功能用途是驗證YouTube視頻的網址並檢查視頻是否存在。這是我的實際代碼片段。我操縱字符串到我想要的格式,然後我繼續檢查它是否有效並存在。如果它通過測試,然後我回應結果。問題是我沒有正確調用該函數。PHP:檢查YT網址是否有效以及是否存在視頻

我得到這個回聲即使視頻確實存在:

的視頻不存在或無效的網址

編輯:如果視頻存在並添加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

所以哪來的'isValidURL()'的定義?您至少應該向我們顯示與錯誤消息相關的代碼... – Amber 2012-07-12 03:13:22

+0

u缺少** isValidURL **功能 – Fredy 2012-07-12 03:14:00

+0

對不起,不想讓代碼冗長,但我已添加該功能。 – techAddict82 2012-07-12 03:30:29

回答

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"){