title: PHP - Get Website Title From User Site Input
body: <p>I'm trying to get the title of a website that is entered by the user.</p>
  
  <p>Text input: website link, entered by user is sent to the server via AJAX.
  The user can input anything: an actual existing link, or just single word, or something weird like 'po392#*@8'<br>
  <br> 
  Here is a <strong>part</strong> of my PHP script:</p>
  
  <pre><code>         // Make sure the url is on another host
          if(substr($url, 0, 7) !== "http://" AND substr($url, 0, 8) !== "https://") {
              $url = "http://".$url;
          }
  
          // Extra confirmation for security
          if (filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
              $urlIsValid = "1";
          } else {
              $urlIsValid = "0";
          }
  
          // Make sure there is a dot in the url
          if (strpos($url, '.') !== false) {
              $urlIsValid = "1";
          } else {
              $urlIsValid = "0";
          }
  
          // Retrieve title if no title is entered
          if($title == "" AND $urlIsValid == "1") {
  
              function get_http_response_code($theURL) {
                  $headers = get_headers($theURL);
                  if($headers) {
                      return substr($headers[0], 9, 3);
                  } else {
                      return 'error';
                  }
              }
  
              if(get_http_response_code($url) != "200") {
  
                  $urlIsValid = "0";
  
              } else {
  
                  $file = file_get_contents($url);
  
                  $res = preg_match("/&lt;title&gt;(.*)&lt;\/title&gt;/siU", $file, $title_matches);
  
                  if($res === 1) {
                      $title = preg_replace('/\s+/', ' ', $title_matches[1]);
                      $title = trim($title);
  
                      $title = addslashes($title);
                  }
  
                  // If title is still empty, make title the url
                  if($title == "") {
                      $title = $url;
                  }
  
              }
          }
  </code></pre>
  
  <p>However, there are still errors occuring in this script.</p>
  
  <p>It <strong>works</strong> perfectly if an existing url as '<a href="https://www.youtube.com/watch?v=eB1HfI-nIRg" rel="nofollow noreferrer">https://www.youtube.com/watch?v=eB1HfI-nIRg</a>' is entered and when a non-existing page is entered as '<a href="https://www.youtube.com/watch?v=NON-EXISTING" rel="nofollow noreferrer">https://www.youtube.com/watch?v=NON-EXISTING</a>', but it <strong>doesn't work</strong> when the users enters something like 'twitter.com' (without http) or something like 'yikes'.</p>
  
  <p>I tried literally everthing: cUrl, DomDocument...</p>
  
  <p>The problem is that when an invalid link is entered, the ajax call never completes (it keeps loading), while it should $urlIsValid = "0" whenever an error occurs.</p>
  
  <p>I hope someone can help you - it's appreciated.</p>
  
  <p>Nathan</p>