【问题标题】:PHP: fsockopen() expect parameter 1 to be stringPHP:fsockopen() 期望参数 1 是字符串
【发布时间】:2016-05-09 07:37:43
【问题描述】:

我对第 10 行有疑问($f = fsockopen($server, 43, $ae_whois_errno, $ae_whois_errstr, AE_WHOIS_TIMEOUT);)。当我在输入字段中写入 URL 时,会出现警告:“警告:fsockopen() 期望参数 1 为字符串,第 10 行 C:\xampp\htdocs\Client-Server\whoisclient.php 中给出的资源”。这是代码。

 <?php 
    include("preliminari.php");
    function ae_whois($query, $server)
    {

        define('AE_WHOIS_TIMEOUT', 15); // connection timeout
        global $ae_whois_errno, $ae_whois_errstr;

        // connecting 
        $f = fsockopen($server, 43, $ae_whois_errno, $ae_whois_errstr, AE_WHOIS_TIMEOUT);
        if (!$f) 
            return false; // connection failed 

        // sending query    
        fwrite($f, $query."\r\n");

        // receving response 
        $response = '';
        while (!feof($f))
            $response .= fgets($f, 1024);

        // closing connection 
        fclose($f);

        return $response;
    }
    ?>
    <html>
    <head>
    <title>Whois client con i socket del PHP</title>
    <style type="text/css">
    .body {
        font-family: Verdana, Geneva, sans-serif;
    }
    .titolo {
        font-family: Verdana, Geneva, sans-serif;
        font-weight: bold;
        text-decoration: underline;
    }
    </style>
    </head>
    <body>
    <?php
    $indirizzo  = $_GET['dominio'];
    $dom = explode(".",$indirizzo);
    $campi = count($dom)-1;
    $query = "select servername from whois where dominio = '".$dom[$campi]."'";
    $server = mysql_query($query);
    if (isset ($indirizzo)) {
        $reply = ae_whois ($indirizzo, $server);
        $reply = nl2br ($reply);
        echo "<p>$reply</p>";
    }
    ?>
    <h2 align="center" class="titolo">Whois di un dominio</h2>
    <form name="ricerca" method="get" action="whoisclient.php">
    <p align="center" class="body">Nome del dominio</p>
    <p align="center" class="body">
      <input name="dominio" type="text">
    </p>
    <p align="center" class="body"><input name="go" type="submit" value="WHOIS"></p>
    </form>
    </body>
    </html>

【问题讨论】:

  • $server 必须是字符串,而且似乎不是字符串。 $server 是如何初始化的?
  • $server 是查询的结果:$query="select servername from whois where dominio=' ".$dom[$campi]." ' "; $server=mysql_query($query);
  • 你得到正确列的值了吗? $server 必须类似于 www.example.com
  • 停止使用已弃用且自 PHP7 起已移除 mysql_* 函数。迁移到 PDO 并开始使用 Prepared Statements,真的不难。
  • $server 类似于“whois.nic.ac”或“whois.aeda.net.ae”

标签: php html sockets


【解决方案1】:

您有服务器作为查询,但您没有指定结果。

然后你需要 mysql_fetch_assoc 来得到一个值。

 $s =mysql_query($query);
 while ($row = mysql_fetch_assoc($s)) {
     $server = $row['servername'];
 }

您还应该认真考虑改用 mysqli 或 pdo。 php mysql query documentation

【讨论】:

  • 我已经实现了该解决方案,但现在当我编写类似“www.arduino.cc”的内容时,结果输出是“与“WWW.ARDUINO.CC”不匹配
  • 与 www.youtube.com 相同
  • 您正在尝试选择与域名匹配的数据行。如果这些域不在您的表中,则不会返回任何内容。
  • 是的,因为我已经添加了相应服务器名的回显,它出现在页面顶部
  • @danieletedesco 这是意料之中的。 www.arduino.cc 不是注册域名,因此不会出现在 WHOIS 中; arduino.cc 是,不过。同样适用于 Youtube。
猜你喜欢
  • 1970-01-01
  • 2016-05-17
  • 2017-02-24
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
相关资源
最近更新 更多