drci47425 2013-01-09 17:09 采纳率: 100%
浏览 70

在两个网站之间传递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?

  • 写回答

2条回答 默认 最新

  • dongliehuan3925 2013-01-09 17:19
    关注

    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
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)