سؤال

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.

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top