2012-03-19 48 views
0

我正在使用fsockopen連接並將數據發送到腳本。問題是,在接收端沒有收到數據,就像你在下面的輸出中看到的那樣,只打印空數組。我在Tamlyns的回答中提供了我的解決方案:PHP Post data with Fsockopen。我做了'嘗試創建後期參數的方式,輸出沒有差異。使用fsockopen發佈數據,在接收端沒有收到數據

我的主要腳本:

<?php 
session_start(); 

    $fp = fsockopen("192.168.1.107",  
      80, 
      $errno, $errstr, 10);  

    $params = "smtp=posteddata\r\n"; 
    $params = urlencode($params); 

    $auth = base64_encode("kaand:kaand123"); 

    if (!$fp) { 
      return false; 
     } else { 
      error_log("4"); 
      $out = "POST /smic/testarea/fsockopen_print_post.php HTTP/1.1\r\n";  
      $out.= "Host: 192.168.1.107\r\n";  
      $out.= "Content-Type: application/x-www-form-urlencoded\r\n"; 
      $out.= "Authorization: Basic ".$auth; 
      $out.= 'Content-Length: '.strlen($params).'\r\n'; 
      $out.= "Connection: Close\r\n\r\n"; 
      $out .= $params; 

      fwrite($fp, $out); 
      fflush($fp); 

      header('Content-type: text/plain'); 
      while (!feof($fp)) { 
       echo fgets($fp, 1024); 
      } 

      fclose($fp); 
     }   
?> 

fsockopen_print_post.php:

<?php 
session_start(); 

print_r($_POST); 

$raw_data = $GLOBALS['HTTP_RAW_POST_DATA']; 
parse_str($raw_data, $_POST); 
print_r($_POST); 
?> 

輸出:

HTTP/1.1 200 OK 
Date: Mon, 19 Mar 2012 09:21:06 GMT 
Server: Apache/2.2.15 (CentOS) 
X-Powered-By: PHP/5.3.10 
Set-Cookie: PHPSESSID=i4lcj5mn1ablqgekb1g24ckbg5; path=/ 
Expires: Thu, 19 Nov 1981 08:52:00 GMT 
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Pragma: no-cache 
Content-Length: 20 
Connection: close 
Content-Type: text/html; charset=UTF-8 

Array 
(
) 
Array 
(
) 

有什麼問題和我如何解決它?

回答

1

有一個在你的代碼一個錯字:

$out.= 'Content-Length: '.strlen($params).'\r\n'; 
$out.= "Connection: Close\r\n\r\n"; 

應該是:

$out .= "Content-Length: ".strlen($params)."\r\n"; 
$out .= "Connection: close\r\n\r\n"; 

見您如何使用身邊\r\n單引號?這意味着直接發送'\r\n'而不是將它們解釋爲Windows crlf。雙引號糾正了這一點。

+0

感謝您的回答。這真的有必要嗎?檢查fsockopen的例子,他們不會像這樣([php.net](http://se2.php.net/manual/en/function.fsockopen.php))和所有其他我找到的例子,這不會在任何地方使用。我只想發佈數據,然後斷開連接(應該採用異步方式)。 – Nicsoft 2012-03-19 10:19:51

+0

如果您希望能夠在不阻塞的情況下處理多個客戶端,這是必要的。如果你「知道」,你只需要一個,這通常意味着你現在做,但將來需要更多,但有一個更簡單的方法。但我建議你以這種方式實施解決方案,以免你未來頭痛。此外,當你弄清楚這是如何工作的時候,你會失去一切,並獲得更好的知識。 ;) – deed02392 2012-03-19 10:24:04

+0

反正現在的學習曲線非常陡峭;)也許這有助於處理多個客戶端,但是這有助於我在接收端獲取發佈的數據,這是我所要求的嗎? – Nicsoft 2012-03-19 10:26:44

0

該行

$out.= "Authorization: Basic ".$auth; 

應該已經

$out.= "Authorization: Basic ".$auth."\r\n"; 

還有別的東西我還沒有確定,但此代碼(測試時,周圍的變量可能只是一些混淆) :

<?php 
$fp = fsockopen('192.168.1.107', 80); 
$vars = array(
'hello' => 'world' 
); 
$content = http_build_query($vars); 
$auth = base64_encode("kaand:kaand123"); 
$out = "POST /smic/testarea/fsockopen_print_post.php HTTP/1.1\r\n";  
$out .= "Host: 192.168.1.107\r\n";  
$out .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
$out .= "Authorization: Basic ".$auth."\r\n"; 
$out .= "Content-Length: ".strlen($content)."\r\n"; 
$out .= "Connection: close\r\n\r\n"; 
$out .= $content; 

fwrite($fp,$out); 

header("Content-type: text/plain"); 
while (!feof($fp)) { 
echo fgets($fp, 1024); 
} 
fclose($fp);  
?> 
相關問題