我知道我已经有了类似的帖子,但我不能为我的生活找出两个片段之间的不同之处 - 因为我只能从第一个例子中得到返回值 - 但不是第二个例子,示例中的两个方法都返回一个值。更奇怪(或令人沮丧)的是,php不会在第二个例子中抛出任何错误,它只是呈现一个空白屏幕。我在XAMMP上运行,我相信“show errors”等设置为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系统所说的,看起来该变量包含正确的数据,但“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>
答案 0 :(得分: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>
答案 1 :(得分: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;
}
答案 2 :(得分: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>
答案 3 :(得分: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>