将PHP对象或数组从一个站点传递到另一个站点?

夏期劇*_*期劇場 1 php xml-rpc parameter-passing json-rpc

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

  • 如何从主机站点发送/发送
  • 不要从目的地网站拉

我想通过NOT using HTML和web表单直接从自动脚本传递.
请提出任何建议.

Mih*_*rga 7

最好的方法是使用json_encode():

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

另一方面:

$content = json_decode($_GET['data']);
Run Code Online (Sandbox Code Playgroud)

或使用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);
Run Code Online (Sandbox Code Playgroud)

另一方面:

$content = json_decode($_POST['data']);
Run Code Online (Sandbox Code Playgroud)