アンドロイドアプリからREST APIに投稿されたJSON
のデータをcodeigniter
に保存します。で残りのAPIを使用する
JSON
データがある:私はJSON
データからすべての値を取ることによって、テーブル に複数の行を挿入する質問のためにループにしたい。このことから
コントローラ
<?php defined('BASEPATH') OR exit('No direct script access allowed');
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';
class Test extends REST_Controller
{
private $postData;
private $getData;
function __construct(){
parent::__construct();
header('Content-Type: application/json');
#Check if Content Type is JSON
if(isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], "application/json") !== false){
$jsonReqData = json_decode(trim(file_get_contents('php://input')), true);
}else{
$jsonReqData = $this->input->post();
}
$this->postData = $jsonReqData;
$this->load->model('mymodel');
$this->methods['user_get']['limit'] = 500; //500 requests per hour per user/key
$this->methods['user_get']['limit'] = 100; //100 requests per hour per user/key
$this->methods['user_delete']['limit'] = 50; //50 requests per hour per user/key
}
function save_post(){
$formData=array();
$photo=trim($_POST['photo'],'"');
$photo=filter_var($photo, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);
//$questions=trim($_POST['questions'],'"');
//$formData['questions']=filter_var($questions, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);
$formData['questions']=$_POST['questions'];
print_r($formData['questions']);die();
$caseId=trim($_POST['case'],'"');
$formData['case']=filter_var($caseId, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);
$stateId=trim($_POST['stat'],'"');
$formData['stat']=filter_var($stateId, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);
$foe_tat=trim($_POST['tat'],'"');
$formData['tat']=filter_var($foe_tat, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);
$applicantId=trim($_POST['applicant'],'"');
$formData['applicant']=filter_var($applicantId, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);
//$answer=trim($_POST['answer'],'"');
//$formData['answer']=filter_var($answer, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);
$foe_user=trim($_POST['uid'],'"');
$formData['foe_user']=filter_var($foe_user, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);
$comment=trim($_POST['comment'],'"');
$formData['comment']=filter_var($comment, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);
$data_image=array();
$data_image = explode(",",$photo);
$imageList=array();
for ($x = 0; $x < count($data_image); $x++) {
$rand_num = rand();
$data_img_dec = base64_decode($data_image[$x]);
#image name is combination of applicantId+rand+foe_user
$img_filename = $applicantId."_".$rand_num.'_'.$foe_user.'.png';
$imgpath = APPPATH.'uploads/uploads/'.$img_filename; #saving image
$success = file_put_contents($imgpath, $data_img_dec);
$imageList[]=$img_filename;
}
$formData['site_photo']=implode(",",$imageList);
//echo "<pre>";print_r($formData);
$result = $this->mymodel->saveTest($formData);
$this->response(array('status'=>'Success','data'=>$result),200);
}
{
"tat": "6",
"stat": "4",
"comment": "",
"uid": "2",
"case": "8EZKq2QgJR",
"photo": "",
"applicant": "1230000",
"questions": {
"34": "No",
"46": "fgbfb",
"48": "NA",
"29": "NA",
"45": "dsd",
"49": "InConclusive",
"43": "1 BHK",
"35": "NA",
"38": "12",
"39": "2",
"27": "q1",
"41": "Others",
"52": "fgfdg",
"47": "fgfg",
"31": "Chawl",
"33": "Upper Middle Class",
"37": "Self Owned",
"30": "fdgfdgb",
"50": "fgfdgb",
"51": "fgfdg",
"32": "NA",
"44": [
"Refrigerator",
"Airconditioner"
]
}
}
。
はjson_decodeを使用して注意してください。 http://php.net/manual/en/function.json-decode.php – Fr0z3n7
ありがとう@ Fr0z3n7、私は私の答えを編集します! –