在两个网站之间传递JSON数组

I want to pass an array between two websites but i have difficult to do that,in my localhost i try this code :

 $array = array("12" => "val", "34" => "val2");
 $url = 'http://example1.com/save.php';
 $post = 'data='.json_encode($array);
 $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);
 curl_close($ch);

then in example1.com/save.php i do this just for testing:

 $result = json_decode($_POST['data'],true);

 foreach ($result as $key => $value) {
    echo $key.'='.$value.'<br />';
 }

but this give me this warning:

  Warning: Invalid argument supplied for foreach() in /home/a2549384/public_html/save.php on line 5

is there any solution?

You should check that what you are passing to foreach is an array by using the is_array function. Error means you are doing a foreach on something that is not an array.

Check out your foreach and look if the thing before the as is actually an array. Not only in theory but actually use var_dump to dump it.

Also If you are not sure it's going to be an array you can always check using the following PHP example code:

if (is_array($variable)) {

  foreach ($variable as $item) {
   //do something
  }
}

You might need to change

$post = 'data='.json_encode($array);

to

$post = 'data='.urlencode(json_encode($array));

In any case, just var_dump($_POST); on the other side and see if there are encoding errors.

With json_last_error() you can check if there was an error in decoding.