Pass an PHP Object or Array from one Site to another Site?

ぃ、小莉子 提交于 2019-12-11 06:49:51

问题


In PHP, how can I pass an Object (actually an Array) from one Site to another Site (by not losing its original Object Structure and Values)?

  • How to PASS/SEND from the host site
  • NOT to pull from the destination site

I want to pass directly from the automated script by NOT using HTML and web forms.
Any suggestion, please.


回答1:


The best way to do that is to use json_encode():

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

on the other side:

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

or send it using 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);

on the other side:

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



回答2:


You can convert it to JSON and then convert it back to the PHP object. This is very easy when it is an array. You can just use json_encode($array) and json_decode($json)on the other site. I would send the data via POST because the limit length of GET: Is there a limit to the length of a GET request?



来源:https://stackoverflow.com/questions/12777604/pass-an-php-object-or-array-from-one-site-to-another-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!