2017-06-03 19 views
0

我可以訪問3個不同的遠程服務器。他們都託管相同的php文件,它的名字是processing.phpPHP使用file_get_contents將數據發佈到遠程服務器中託管的另一個php文件

而且在不同的服務器上我有3頁:

  1. index.html:包含與POST方法將數據發送到forward.php
  2. forward.php一種形式:即在其他服務器上的表單數據轉發到processing.php
  3. processing.php:顯示發佈數據

問題:processing.php中的代碼未執行,返回的結果是源代碼processing.php的明文!

forward.php:

$field1 = $_POST['field1']; 
$field2 = $_POST['field2']; 
rtrim($_POST['listserver'],"-"); 
$listServer = explode("-",$_POST['listserver']); 

foreach ($listServer as $server){ 
    if(!empty($server)){ 
     $url = 'http://'.$server.'/processing.php'; 
     $data = array('field1' => $field1, 'field2' => $field2); 
     $options = array(
      'http' => array(
       'header' => "Content-type: application/x-www-form-urlencoded", 
       'method' => 'POST', 
       'content' => http_build_query($data) 
      ) 
     ); 
     $context = stream_context_create($options); 
     $result = file_get_contents($url, false, $context); 
     if ($result === FALSE) { 
      echo "You can't "; 
     }else{ 
      echo ('result : '.$result); 
     } 
    } 
} 

Processing.php

$field1 = $_POST['field1']; 
$field2 = $_POST['field2']; 
$result = $field1+$field2; 
$myFile = "files/result.txt"; 
$fh = fopen($myFile, 'w') or die("can't open file"); 
fwrite($fh, $result); 
fclose($fh); 

但在所有的服務器,result.txt是空的!

+0

file_get_contents()執行GET請求。如果你想POST使用捲曲。 http://php.net/manual/de/curl.examples-basic.php –

+1

@MarkusZeller不正確,如果你傳遞正確的流上下文,如上 – Steve

+0

謝謝@Steve。學到了新東西。沒有看完整個代碼。 –

回答

0

感謝@MarkusZeller的建議,我設法讓它工作使用cURL

這個thread是非常有幫助的,謝謝Stack Over Flow社區!

相關問題