2016-02-25 95 views
1

這是我的API函數我需要從HTTP請求中獲取表單數據中的數據。如何從PUT HTTP請求獲取數據(表單數據)

我需要得到put值來更新我的班級查詢中的數據。

function method() 
    { 
     $method = $_SERVER['REQUEST_METHOD']; 

     return $method; 
    } 

    function updateUser() 
    { 
     $method = method(); 

     if($method == 'PUT'){ 

      $put_data = file_get_contents("php://input"); 

      parse_str($put_data, $post_vars); 


      return $post_vars; 


     }else{ 

      $status = '400 Bad Request'; 

      return $status; 

     } 
    } 

{ 「------ WebKitFormBoundaryYKcobRh4FtrGCYaI \ r \ nContent處置:_form數據; _name」: 「\」 測試\「\ r \ n \ r \ ntest_value \ r \ N-- ---- WebKitFormBoundaryYKcobRh4FtrGCYaI - \ r \ n「}

這是我從返回$ POST_VARS得到

我需要測試:test_value

回答

1

變化從該請求的編碼的multipart/form-data的到應用程序/ x-WWW窗體-urlencoded

+0

WOW感謝(Y)得到我需要什麼 –

0

嘗試更換

$method = method(); 

     if($method == 'PUT'){ 
//dosomething 
}else{ 
//error 
} 

隨着

if (!($putData = fopen("php://input", "r"))){ 
       throw new Exception("Can't get PUT data."); } 
else{ //dostuff 
} 

對於更多信息,參見: http://php.net/manual/en/features.file-upload.put-method.php

+0

如果((! $ putData = fopen(「php:// input」,「r」))){ throw new Exception(「Can not get PUT data。」); \t}否則{ \t \t \t \t $ ID = $ putData [ 'ID']; \t \t \t \t return $ id; \t \t \t} 它感謝工作,但返回null :( –