2

我知道我已经有一个类似的帖子,但是我一生都无法弄清楚这两个片段之间有什么不同-因为我只能从第一个示例中获取返回值-但不能从第二个示例中获取这两种方法在示例中返回一个值。更奇怪(或令人沮丧)的是 php 在第二个示例中没有抛出任何错误,它只是呈现一个空白屏幕。我在 XAMMP 上运行,我相信“显示错误”等设置为 true。

第一个示例(完美运行):

<html>
    <?php 
        function myMethod() {
            return "testing";
        }
    ?>

    <script type="text/javascript">
        var val= "<?php echo myMethod(); ?>";
        document.write(val);
    </script>

</html>

第二个例子:(注意只有“myMethod”函数发生了变化)

<html>
    <?php 
        function myMethod() {
            error_reporting(E_ALL);

            echo "<h2>Querying...</h2>\n";

            /* Get the port for the WWW service. */
            $service_port = getservbyname('www', 'tcp');

            /* Get the IP address for the target host. */
            $address = gethostbyname('www.example.com');

            /* Create a TCP/IP socket. */
            $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            if ($socket === false) {
                echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
            } else {
                echo "OK.\n";
            }

            echo "Attempting to connect to '$address' on port '$service_port'...";
            $result = socket_connect($socket, $address, $service_port);
            if ($result === false) {
                echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
            } else {
                echo "OK.\n";
            }

            $in = "HEAD / HTTP/1.1\r\n";
            $in .= "Host: www.example.com\r\n";
            $in .= "Connection: Close\r\n\r\n";
            $out = '';

            echo "Sending HTTP HEAD request...";
            socket_write($socket, $in, strlen($in));
            echo "OK.\n";

            echo "Reading response:\n\n";
            while ($out = socket_read($socket, 2048)) {
                echo $out;
                return 'test';
            }

            echo "Closing socket...";
            socket_close($socket);
            echo "OK.\n\n";
        }
    ?>

    <script type="text/javascript">
        var val= "<?php echo myMethod(); ?>";
        document.write(val);
    </script>
</html>

更新:根据@the system 所说,看起来该变量包含正确的数据,但“document.write”似乎没有正确地将其输出到浏览器视图:

<html>
<script type="text/javascript">
    var val= "<h2>Querying...</h2>
    OK.
    Attempting to connect to '192.0.43.10' on port '80'...OK.
    Sending HTTP HEAD request...OK.
    Reading response:

    HTTP/1.0 302 Found
    Location: http://www.iana.org/domains/example/
    Server: BigIP
    Connection: close
    Content-Length: 0

    test";

    document.write(val);
</script>
</html>
4

4 回答 4

3

查看浏览器的源代码视图。该脚本似乎可以按的预期工作(见下文)。

但请注意,在 javascript 中,无法以这种方式构建多行字符串

在 javascript 中构建多行字符串时,您必须这样做:

var str  = "first line\n";
    str += "secon line\n";

或使用连接功能:

var str = [
  "first line",
  "second line"
].join("\n");

这是你的输出:

<html>

<script type="text/javascript">
var val= "<h2>Querying...</h2>
OK.
Attempting to connect to '192.0.43.10' on port '80'...OK.
Sending HTTP HEAD request...OK.
Reading response:

HTTP/1.0 302 Found
Location: http://www.iana.org/domains/example/
Server: BigIP
Connection: close
Content-Length: 0

test";

document.write(val);
</script>

</html>
于 2013-02-12T15:16:06.660 回答
1

将所有回声更改为本地存储在变量中:

function myMethod()
{
error_reporting(E_ALL);

$temp = "<h2>Querying...</h2>\n";

/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');

/* Get the IP address for the target host. */
$address = gethostbyname('www.example.com');

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
    echo "OK.\n";
}

$temp .= "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
    $temp .= "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
    $temp .= "OK.\n";
}

$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';

$temp .= "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
$temp .="OK.\n";

$temp .="Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
    $temp .= $out;
    return 'test';

}

$temp .= "Closing socket...";
socket_close($socket);
$temp .= "OK.\n\n";
return $temp;
}
于 2013-02-12T15:18:22.523 回答
1

因为 Javascript 不支持多行文本,所以您可以将输出作为数组返回,并json_encode在 javascript 中将其用作数组:

<?php

    function myMethod()
    {
        error_reporting(E_ALL);
        $return = array();

        $return[] = "<h2>Querying...</h2>\n";

        /* Get the port for the WWW service. */
        $service_port = getservbyname('www', 'tcp');

        /* Get the IP address for the target host. */
        $address = gethostbyname('www.example.com');

        /* Create a TCP/IP socket. */
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($socket === false) {
            $return[] = "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
        } else {
            $return[] = "OK.\n";
        }

        $return[] = "Attempting to connect to '$address' on port '$service_port'...";
        $result = socket_connect($socket, $address, $service_port);
        if ($result === false) {
            $return[] = "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
        } else {
            $return[] = "OK.\n";
        }

        $in = "HEAD / HTTP/1.1\r\n";
        $in .= "Host: www.example.com\r\n";
        $in .= "Connection: Close\r\n\r\n";
        $out = '';

        $return[] = "Sending HTTP HEAD request...";
        socket_write($socket, $in, strlen($in));
        $return[] = "OK.\n";

        $return[] = "Reading response:\n\n";
        while ($out = socket_read($socket, 2048)) {
            $return[] = $out;
        }

        $return[] = "Closing socket...";
        socket_close($socket);
        $return[] = "OK.\n\n";

        return json_encode($return);
    }

?>

<script type="text/javascript">
var val= <?php echo myMethod(); ?>;

for(i in val){
    document.write(val[i] + '<br \/>');
}
</script>
于 2013-02-12T15:19:24.470 回答
0

我编辑了你的代码问题是在 html 和 js 代码上而不是在 php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php 

function myMethod()
{
error_reporting(E_ALL);

echo "<h2>Querying...</h2>\n";

/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');

/* Get the IP address for the target host. */
$address = gethostbyname('www.example.com');

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
    echo "OK.\n";
}

echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
    echo "OK.\n";
}

$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';

echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";

echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
    echo $out;
    return 'test';

}

echo "Closing socket...";
socket_close($socket);
echo "OK.\n\n";
}

?>
<?php echo myMethod(); ?>
</body>
</html>
于 2013-02-12T15:20:20.037 回答