[英]Curl not posting data to remote server , using php-curl
我已經在虛擬機上設置了本地服務器,這是在Virtualbox上作為內部網絡和橋接適配器連接的兩個Ubuntu VM。 我已經將站點托管在地址為10.0.0.3和10.0.0.4的本地dhcp服務器(內部網絡)上。 我的網頁都托管在兩台機器上的apache2服務器上。 我已經驗證它們在相互ping通彼此的VM時已連接。 我必須將表單數據從一台服務器發送到另一台服務器,並且為此使用curl。
我在VM1(10.0.0.4)上的update.php文件是:
<?php
if($_POST['time']){
$time = $_POST['time'];
$data = array('time'=>$time);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '10.0.0.3/index.php');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_exec($ch);
curl_close($ch);
}
?>
我在VM2(10.0.0.3)中的index.php文件是:
<?php
$time = $_POST['time'];
echo $time;
?>
我能夠在VM1(10.0.0.4)中從index.html到update.php接收html表單數據,但是無法使用上述方法將數據從update.php張貼到index.php。 還有其他我想使用curl將數據發布到遠程服務器的東西嗎?
確保在檢查是否已發布數據時在if條件中使用isset()。 像下面給出的
<?php
if(isset($_POST['time'])){
$time = $_POST['time'];
$data = array('time'=>$time);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://10.0.0.3/index.php');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.