繁体   English   中英

如何将json格式作为输入发送并存储在web服务的codeigniter rest中的数据库中

[英]how to send json format as input and store in database in codeigniter rest for webservices

我正在使用以下代码来发送json格式作为输入并将其存储在db中。 当我解码该值一无所获。 我的代码有什么问题。 有人能帮助我吗。

控制者

function join_community_post(){
        $serviceName = 'join_community';    
        //getting posted values
        $c =  '{"community" : [{"community_id":1},{"community_id":2},{"community_id":3}]}';
        $ip = trim($this->input->post($c));
        $ipJson = json_decode($ip);
        print_r ($ipJson);exit;
        $retVals = $this->user_model->user_join_community($ip, $serviceName);

        header("content-type: application/json");
        echo $retVals;
        exit;
    }

模型

function user_join_community($ip,$serviceName) {
        $ipJson = json_decode($ip);
    }

如果第二个参数未作为真正的PHP传递,则json_decode提供stdClass对象

其次,您正在传递$ c的后值...检查其空白输入类

控制者

   function join_community_post(){
    $serviceName = 'join_community';    
    //getting posted values
    //$c =  '{"community" : [{"community_id":1},{"community_id":2},{"community_id":3}]}';
    $ip = trim($this->input->post($c));
    $ipJson = json_encode($ip);

    $retVals = $this->user_model->user_join_community($ip, $serviceName);

    header("content-type: application/json");
    echo $retVals;
    exit;
}

模型

 function user_join_community($c,$serviceName) {
         $ipJson = json_decode($c,true);
         $createArray = array();
         foreach($ipJson['community'] as $key=>$value){
           $createArray = array(
             'user_community_created_date' => date('Y-m-d H:i:s'),
             'user_community_modified_date' => date('Y-m-d H:i:s'),
             'community_id' => $value['community_id'] 
          );
      $insert = $this->db->insert('user_community', $createArray);
   }
   if ($insert) {
         $data['message'] = 'success';
         $status = $this->ville_lib->return_status('success', $serviceName, $data, $c);
    } else {
        $data['message'] = 'Error';
         $status = $this->ville_lib->return_status('error', $serviceName, $data, $c);
    }

  }

在foreach的花括号后删除分号,并将$ c添加到webservices表中,这将使您收到错误.....希望它可以帮助...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM