问题
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