簡體   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