我想在两个网站之间传递一个数组,但我很难做到这一点,在我的本地主机中我尝试这个代码:
$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);
然后在 example1.com/save.php 我这样做只是为了测试:
$result = json_decode($_POST['data'],true);
foreach ($result as $key => $value) {
echo $key.'='.$value.'<br />';
}
但这给了我这个警告:
Warning: Invalid argument supplied for foreach() in /home/a2549384/public_html/save.php on line 5
有什么解决办法吗?