1

在 PHP 中,如何将一个对象(实际上是一个数组)从一个站点传递到另一个站点(通过不丢失其原始对象结构和值)?

  • 如何从主机站点传递/发送
  • 不要从目标站点拉取

我想通过不使用HTML和 web 表单直接从自动化脚本传递。
有什么建议,请。

4

2 回答 2

8

最好的方法是使用json_encode()

file_get_contents('http://www.example.com/script.php?data='.json_encode($object));

另一方面:

$content = json_decode($_GET['data']);

或使用 cURL 发送

$url = 'http://www.example.com/script.php';
$post = 'data='.json_encode($object);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_exec($ch);

另一方面:

$content = json_decode($_POST['data']);
于 2012-10-08T08:07:26.153 回答
1

您可以将其转换为 JSON,然后将其转换回 PHP 对象。当它是一个数组时,这很容易。您可以在其他网站上使用json_encode($array)和。json_decode($json)我会通过 POST 发送数据,因为 GET的长度限制: GET 请求的长度是否有限制?

于 2012-10-08T08:07:47.520 回答