2016-04-06 7 views
0

私は郵便配達で次のコードを実行しますが、すべてのフィールドはnullになります。私はecho json_encode($ cus)を使ってこれを見ました。私は何時間も手をつけていましたが、どこでエラーが発生しているのか分かりません。どんな助けもありがとう!PHP Slim APIはgetを呼び出した後にnullを返します

require 'Slim/Slim.php'; 

$app = new Slim(); 

$app->get('/Customers', 'getCustomers'); 
$app->get('/Customers/:id', 'getCustomer'); 
$app->post('/New_Customer', 'addCustomer'); 
$app->put('/Customers/:id', 'updateCustomer'); 
$app->delete('/Customers/:id', 'deleteCustomer'); 

$app->run(); 


// Add new Customer to the Database 
function addCustomer() { 
    $request = Slim::getInstance()->request(); 
    $cus = json_decode($request->getBody()); 

    $sql = "INSERT INTO customers (Username, First_Name, Last_Name, Email, Status) VALUES (:username, :firstname, :lastname, :email, :status)"; 
    try { 
     $db = DB_Connection(); 
     $stmt = $db->prepare($sql); 
     $stmt->bindParam("username", $cus->Username); 
     $stmt->bindParam("firstname", $cus->First_Name); 
     $stmt->bindParam("lastname", $cus->Last_Name); 
     $stmt->bindParam("email", $cus->Email); 
     $stmt->bindParam("status", $cus->Status); 
     $stmt->execute(); 
     $cus->id = $db->lastInsertId(); 
     $db = null; 
     echo json_encode($cus); 
    } catch(PDOException $e) { 
     echo '{"error":{"text":'. $e->getMessage() .'}}'; 
     //echo json_encode($cus); 
    } 
} 

EDIT:POSTMANで

var_dump($cus) results in 
object(stdClass)#18 (5) { ["Username"]=> &NULL ["First_Name"]=> &NULL ["Last_Name"]=> &NULL ["Email"]=> &NULL ["Status"]=> &NULL 

私は、フォームデータを使用してx-www-form-urlencodedでいます。

+0

var_dump($ cus)は実際の値を表示します。 –

+0

POSTMANで使用しているコンテンツタイプは.... – geggleto

+0

var_dump($ cus)はオブジェクト(stdClass)#18(5){["Username"] =>&NULL ["First_Name"] =>&NULL [" &NULL ["Status"] =>&NULL – user2084641

答えて

0

(お使いのバージョンは何ですか?)

私はスリム

あなた

$request = Slim::getInstance()->request(); 

鉱山を見て:バージョン2.4.2

$request = \Slim\Slim::getInstance()->request(); 
0

あなたはJSONを行っているので、 APIリクエストは、Content-Type: application/x-www-form-urlencodeの代わりにContent-Type: application/jsonを使用する必要があります。

関連する問題