簡體   English   中英

帶有POST請求的500個內部服務器錯誤-Slim框架

[英]500 internal server error with POST request - Slim framework


我正在嘗試使用Slim框架構建一個簡單的REST應用程序,但是當我嘗試執行POST請求時遇到500錯誤。 到目前為止,我已經實現了兩個有效的GET請求。 這是代碼:

index.php

require_once '../include/DbHandler.php';
require '.././libs/Slim/Slim.php';

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();

$app->get("/", function () {
    echo "<h1>Hello!!!!</h1>";
});

/**
 * Get all the events
 * method GET
 * url /events
 */
$app->get('/events', function() {
    $db = new DbHandler();
    $response = array();

    // fetch events
    $result = $db->getAllEvents();

    if ($result != NULL) {
        $response["error"] = false;
        $response = $result;
        echoResponse(200, $response);
    } else {
        $response["error"] = true;
        $response["message"] = "The requested resource doesn't exists";
        echoResponse(404, $response);
    }
});

$app->get('/event/:id', function ($id) {
    $response = array();
    $db = new DbHandler();

    // fetch event
    $result = $db->getEvent($id);

    if ($result != NULL) {
        $response["error"] = false;
        $response = $result;
        echoResponse(200, $response);
    } else {
        $response["error"] = true;
        $response["message"] = "The requested resource doesn't exists";
        echoResponse(404, $response);
    }
});

$app->post('/events', function() {
    // opening db connection
    $db = new DbConnect();

    //$db = new DbHandler();

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

    //$result = $db->addEvent($request);

    $event = json_decode($request->getBody());

    $img = " ";
    $sql = "INSERT INTO event (title, location, date_event, ageMin, ageMax, groupSize, limited, maxParticipants, joining, description, img, type, language) VALUES (".$event->title.", ".$event->location.", ".$event->date_event.", ".$event->ageMin.", ".$event->ageMax.", ".$event->ageMax.", ".$event->limited.", ".$event->maxParticipants.", ".$event->joining.", ".$event->description.", ".$img.", "$event->type", ".$event->language.")";
    try {
        //$db = $this->conn;
        $conn = $db->connect();
        $result = $conn->query($sql);
        $event->id = $conn->lastInsertId();
        echoResponse(200, $event->id);
    } catch(PDOException $e) {
        echoResponse(404, '{"error":{"text":'. $e->getMessage() .'}}');
    }
});

/**
 * Echoing json response to client
 * @param String $status_code Http response code
 * @param Int $response Json response
 */
function echoResponse($status_code, $response) {
    $app = \Slim\Slim::getInstance();
    // Http response code
    $app->status($status_code);

    // setting response content type to json
    $app->contentType('application/json');

    echo json_encode($response);
}

$app->run();

?>

如前所述,這兩個GET方法都可以使用,但是當我嘗試使用POST方法使用此數據添加一行時:

{"title": "Test 1", "location": “Rome, Italy", "date_event": "2016-05-12", "time": "22:00:00", "ageMin": 21, "ageMax": 27, "groupSize": "3", "limited": false, "maxParticipants": "", "joining": "2", "description": "Description test 1", "img": "", "type": "hanging out", "language": "Italian/English"}

我收到500 Internal Server Error ,但我不明白為什么。
我在哪里做錯了?
謝謝!

您已經錯過了$sql分配中"$event->type"周圍的串聯點。


在開發過程中,我建議您啟用添加錯誤和警告消息的功能

error_reporting(E_ALL);
ini_set('display_errors', 1);

您的代碼。

PHP語法檢查:解析錯誤:語法錯誤,代碼在第62行上出現意外的'$ event'(T_VARIABLE)。

比以下更容易理解

500內部服務器錯誤。

或者,如果您需要檢查一小段代碼,則可以使用一些在線工具,例如http://phpcodechecker.com/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM