首页 > 解决方案 > 如何通过php将数据发送到另一台计算机?

问题描述

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

标签: php

解决方案


是的。

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

// 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 发送 post 请求

// 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'];

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


推荐阅读