Search code examples
phpparameter-passingxml-rpcjson-rpc

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


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.


Solution

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