PHP fsockopen无法正常工作

时间:2014-05-27 13:24:55

标签: php fsockopen parse-url

所以,我试图开发一个函数来检查实际的URL是否存在使用fsockopen。我想这样做是为了验证用户输入的URL,但与此同时,我不想因为网站使用的是预定义的404网站而遇到错误 - 或类似的东西。这就是我没有使用file_get_contents的原因。这是我的代码:

<?php
function url_validate($link)
{

  $url_parts = parse_url($link);
  if(empty($url_parts["host"])) return(false);
  if(!empty($url_parts["path"])) {
   $documentpath = $url_parts["path"];
  }else{
   $documentpath = "/";
  }

  if(!empty($url_parts["query"])) {
   $documentpath .= "?" . $url_parts["query"];
  }

  $host = $url_parts["host"];
  $port = $url_parts["port"];
  // Now (HTTP-)GET $documentpath at $host";

  if(empty($port)) $port = "80";
  $socket = fsockopen($host, $port, $errno, $errstr, 30);
  if(!$socket) {
   return(false);
  }else{
   fwrite($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
   $http_response = fgets($socket, 22);

   if(preg_match("/200 OK/i", $http_response, $regs)) {
    return(true);
    fclose($socket);
   }else{
    // echo "HTTP-Response: $http_response<br>";
    return(false);
   }
  }

}

    echo url_validate("http://google.com");
?>

执行脚本时我得到了这个输出:

Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/vhosts/username to domain/httpdocs/test.php on line 22 Warning: fsockopen(): unable to connect to google.com:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /var/www/vhosts/username to domain/httpdocs/test.php on line 22

你们中的任何人都知道可能出现的问题吗?

0 个答案:

没有答案