2013-02-12 145 views
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系統說,這看起來像變量包含正確的數據,但「文件撰寫」似乎並不管出來的瀏覽器視圖正確:

<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

打開您的開發人員控制檯以閱讀錯誤。顯示呈現的JavaScript代替PHP。 – 2013-02-12 15:13:46

+1

一個主要缺陷是,你會被那些'echo'語句傾倒到您的JS變量賦值之前,返回值被回波輸出。任何'「'在那裏會引起JS語法錯誤。** ** NEVER直接輸出PHP數據轉換成一個JS代碼上下文而不使其通過json_encode()首先。 – 2013-02-12 15:14:29

回答

3

查看瀏覽器的源代碼視圖。該腳本似乎可能希望(見下文)的工作。

但請注意,在JavaScript 是不可能建立多行字符串這樣

在構建一個多行字符串在JavaScript中你必須這樣做:

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

或使用join函數:

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> 
+0

@ hek2mgl你能提供一個例子然後處理這種多數據? – Mike 2013-02-12 15:18:27

+0

@Mike done !. :) – hek2mgl 2013-02-12 15:20:10

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; 
} 
+0

另一件事是如果要將文檔寫入文檔,請將\ n更改爲
。 – Zach 2013-02-12 15:19:51

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> 
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> 
相關問題