1

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.

Manikandan C
  • 668
  • 1
  • 9
  • 22
夏期劇場
  • 17,821
  • 44
  • 135
  • 217
  • 1
    Are you asking HOW to do it ? Or how to encode variables (e.g [json_encode()](http://php.net/json_encode) or [serialize()](http://php.net/serialize)) ? – Touki Oct 08 '12 at 08:08

2 Answers2

8

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']);
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
1

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?

Community
  • 1
  • 1
FrediWeber
  • 1,099
  • 1
  • 10
  • 21