0
我可以訪問3個不同的遠程服務器。他們都託管相同的php文件,它的名字是processing.php
。PHP使用file_get_contents將數據發佈到遠程服務器中託管的另一個php文件
而且在不同的服務器上我有3頁:
index.html
:包含與POST方法將數據發送到forward.php
forward.php
一種形式:即在其他服務器上的表單數據轉發到processing.php
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
是空的!
file_get_contents()執行GET請求。如果你想POST使用捲曲。 http://php.net/manual/de/curl.examples-basic.php –
@MarkusZeller不正確,如果你傳遞正確的流上下文,如上 – Steve
謝謝@Steve。學到了新東西。沒有看完整個代碼。 –