title: PHP: checking if YT url is valid and if video exists
body: <p>The function purpose is to validate the URLs of a YouTube video and check if the video exists. This is a snippet of my actual code. I manipulate the string to my desired format and then i proceed to check if it is valid and exists. If it passes the test, then i echo the results.  The problem is that I am not calling the function correctly.  </p>
  
  <p>I am getting this echo even though the video does exist:</p>
  
  <p>The video does not exist or invalid url</p>
  
  <p><strong>Edited: and added isValidURL function</strong>
  *<em>Code for checking if video exist or is invalid:</em>*</p>
  
  <pre><code> 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 &amp;&amp; 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 &gt; 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-&gt;doRequest("/feeds/api/videos/".$videoID, "GET");
                                      //returns Array('headers' =&gt; Array(), 'body' =&gt; 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 ('&lt;iframe src="http://www.youtube.com/embed/'.$v.'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"&gt;&lt;/iframe&gt;');
  
                  //Old way to embed code
                  echo htmlentities ('&lt;embed src="http://www.youtube.com/v/'.$v.'" width="'.$wdth.'" height="'.$hth.'" type="application/x-shockwave-flash"  wmode="transparent" embed="" /&gt;&lt;/embed&gt;');
                  }
              else {
  
              echo ("The video does not exist or invalid url"); 
  
              }
  
  
      }
      ?&gt;
  </code></pre>