如何通过PHP将数据发送到另一台计算机?

时间:2018-05-07 21:57:50

标签: php

我想知道是否有办法将数据从一台计算机上打开的index.php发送到另一台计算机上打开的index.php。 我不知道该怎么做。

2 个答案:

答案 0 :(得分:-1)

简单/懒惰:只需执行远程获取请求

// example1.com/index.php 
file_get_contents("http://example2.com/index.php?your_data=goes_here");

// example2.com/index.php
$your_data = $_GET['your_data'];

更难/更好方式:使用curl发送帖子请求

// on example1.com/index.php
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, "http://example2.com/index.php");
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, 'your_data' => 'goes_here');
$result = curl_exec($ch);
curl_close($ch);

// on example2.com/index.php
$your_data = $_POST['your_data'];

警告:任何人都可以使用这些方法之一在脚本中输入任何内容。确保以这种方式加密和/或验证您正在传输的任何数据。

答案 1 :(得分:-1)

是的,假设计算机能够连接,那么显而易见的方法是通过HTTP:

<?php

$message="Hello otherhost!";
$response=file_get_contents("http://otherhost.example.com/?data=" 
  . urlencode($message));
print "I said \"$message\"<br />\nand other host replied \""
  . htmlentities($response) . "\"";
?>
<?php

if (isset($_GET['data'])) {
     print "Hello - you just said \"" . htmlentities($_GET["data"]) . "\"";
} else {
     print "Sorry - I can't hear you";
}
?>