我正在尝试使用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
,我无法理解为什么
我哪里做错了?
谢谢!
答案 0 :(得分:2)
您在"$event->type"
作业中错过$sql
周围的连接点。
开发时我建议您添加错误和警告消息
error_reporting(E_ALL);
ini_set('display_errors', 1);
代码。
PHP语法检查:解析错误:语法错误,意外&#39; $ event&#39; (T_VARIABLE)在第62行的代码中。
比
更容易理解500内部服务器错误。
作为替代方案,如果您要检查一小段代码,可以使用某些在线工具,例如http://phpcodechecker.com/