【问题标题】:Pass an PHP Object or Array from one Site to another Site?将 PHP 对象或数组从一个站点传递到另一个站点?
【发布时间】:2012-09-28 11:03:08
【问题描述】:

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

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

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

【问题讨论】:

标签: php parameter-passing xml-rpc json-rpc


【解决方案1】:

最好的方法是使用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']);

【讨论】:

  • 请更新我的问题。我不明白。我要发送。
  • 你调用的是那个网址,不想得到也没关系。
  • 谢谢哥们。这是一个 ;) json_encode + json_decode + cURL 的组合是一个了不起的补救措施。
  • 对小数据使用 json 很有帮助。但是如果数据大小大于 70MB 那就有问题了。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2019-09-12
    • 2018-06-15
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多