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.

1
  • 1
    Are you asking HOW to do it ? Or how to encode variables (e.g json_encode() or serialize()) ? Commented Oct 8, 2012 at 8:08

2 Answers 2

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']);
Sign up to request clarification or add additional context in comments.

4 Comments

updated my question, also please. I'm not getting. I want to send.
you are calling that URL, it doesn't matter if you don't want to get.
Thanks buddy. This is the one ;) This combination of json_encode + json_decode + cURL is an amazing remedy.
its helpfull by using json for small data. but if the data size bigger that 70MB then it will be a problem.
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.