2013-07-05 76 views
1

我試圖與$this->post()一起使用以json格式發送郵件。由於我無法得到任何結果,例如$this->post('name')

這是代碼:

<?php 

require(APPPATH . '/libraries/REST_Controller.php'); 

class user_api extends REST_Controller { 

    function user_post() { 

       $user = array(
        'id' => null, 
        'firstname' => $this->post('firstname'), 
        'lastname' => $this->post('lastname') 
       ); 

       $result = $this->user_model->insert($user); 
       if ($result) { 
        $this->response($result, 200); // 200 being the HTTP response code 
       } else { 
        $this->response(NULL, 404); 
       } 
    } 

} 

?> 

數據的JSON格式發送到鏈接:http://mydomain.com/myapplication/user_api/user

{"firstname":"test", 
"lastname":"test" 
} 

有數據庫中沒有任何數據,如它不能從$this->post('firstname')獲取數據。

任何想法????你無法通過郵寄方式獲得

回答

7

JSON數據,你必須使用類似這樣的

require(APPPATH . '/libraries/REST_Controller.php'); 

class user_api extends REST_Controller { 

    function user_post() { 

     $params = json_decode(file_get_contents('php://input'), TRUE); 

     $user = array(
      'id' => null, 
      'firstname' => $params['firstname'], 
      'lastname' => $params['lastname'] 
     ); 

     $result = $this->user_model->insert($user); 
     if ($result) { 
      $this->response($result, 200); // 200 being the HTTP response code 
     } else { 
      $this->response(NULL, 404); 
     } 
    } 

} 
+0

它的工作就像一個魅力非常感謝你。 – Madhawas

1

我覺得你沒有使用正確的函數來得到你想要的發佈數據中的值,但是你想獲得$this->post('name')

你應該使用CI的方法來得到這樣

$this->input->post('name'); 

值3210
$this->input->get('name'); 


      $user = array(
       'id' => null, 
       'firstname' => $this->input->post('firstname'), 
       'lastname' => $this->input->post('lastname') 
      ); 

你應該看看Input class

0

用CodeIgniter訪問後變量,像這樣

$this->input->post('variable_name'); 

此外,如果有

$config['global_xss_filtering'] = TRUE; 

在你的配置文件,它會停止跨站點腳本所以要小心,

4
//You should use 

$array=array(
'firstName' => $this->input->post("firstName"), 
'lastName' => $this->input->post("lastName") 
); 
0

當你在URL的POST參數傳遞給CI RestController,你需要使用 $this->input->get('id')代替$this->get('id') [即如果ID在傳遞的url]。

$this->get('id')不適用於POST URL參數(例如:http://abcd.xyz.com/get_stuff?id=1)。

雖然,$this->get('id')由於某種原因似乎與GETs協同工作。

0

我面臨同樣的問題,同時在codeigniter中用REST編寫webservice。

解決方案==>您應該在標頭請求中設置內容類型。

- 由於你是JSON格式發送數據後,你應該設置內容類型爲application/JSON

- 如果您有任何其他的客戶端集內容類型爲「application/X-WWW測試您迴應 - 形式進行了urlencoded」

0
 $postdata = file_get_contents("php://input"); 
     header('Content-type: application/json'); 
     $obj = json_decode($postdata,true); 
     $firstname=$obj['firstname']; 
     $lastname=$obj['lastname']; and pass to an array 
0

IN REST API

=>如果要使用get方法然後在下面的代碼中使用從服務器獲取數據

$user = json_decode(
    file_get_contents('http://example.com/index.php/api/user/id/1/format/json') 
); 

echo $user->name; 

=>如果要插入/更新的數據服務器,那麼你需要使用如下

<form method="post" id="validateFrm" action="http://example.com/index.php/api/create"> 
    <div class="row"> 
     <label>Name*</label> 
     <input type="text" class="input-field" name="name" id="name" /> 
    </div> 
    <div class="row"> 
     <label>Email</label> 
     <input type="text" class="input-field" name="email" /> 
    </div> 
    <div class="row last-row"> 
     <input type="submit" name="submit" value="submit"> 
    </div> 
</form> 

現在你可以使用所有的表單元素在你的控制器/法像

$name = $this->post('name'); 
$email = $this->post('email'); 
表單動作

享受!

0

這裏存在兩種情況。 如果您正從服務器的數據,然後需要設置格式:

[休息客戶端]

$this->rest->format('application/json'); 

[FOR Android的排球]

public Map<String, String> getHeaders() throws AuthFailureError 
headers.put("Content-Type", "application/json; charset=UTF-8"); 
} 

如果從客戶端發送數據到服務器,然後需要設置格式: [FOR REST Client]

$this->rest->format('application/x-www-form-urlencoded'); 

[FOR Android的排球]

public Map<String, String> getHeaders() throws AuthFailureError 
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
} 
0

你應該這樣的代碼:

$data=array{ 

'firstname' => $this->input->('firstname'), 
'email' => $this->input->post('email') 
} 

$this->user_model->update($data);*emphasized text* 
相關問題