2012-10-08 106 views
1

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

  • 如何传递/发送
  • 不会从目标网站

我想通过不使用html和网页形式,从自动化的脚本直接通过拉。
请提出任何建议。

+1

您是否在问如何做?或者如何编码变量(例如[json_encode()](http://php.net/json_encode)或[serialize()](http://php.net/serialize))? – Touki

回答

6

要做到这一点,最好的办法是使用json_encode()

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

的另一面:

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

或使用卷曲

$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']); 
+0

我怎么能通过它? –

+0

更新了我的答案。 –

+0

更新了我的问题,也请。我没有得到。我想发送。 –

1

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

相关问题