【发布时间】:2014-03-24 20:32:18
【问题描述】:
您好
我是 Slim Micro Framework 的新手,而不是 PHP 专家。我一直在寻找与我的问题相关的相似答案,但没有得到好的结果。
我的问题是,我正在尝试通过 API 更新数据库,使用该框架提供的 PUT 方法,并且每次运行请求时,我都会得到
Unexpected token S
当请求头是:
Status: 200 OK Show explanation Loading time: 422 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36 Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo Content-Type: application/json Accept: */* Accept-Encoding: gzip,deflate,sdch Accept-Language: es,en-US;q=0.8,en;q=0.6和我得到的响应头Date: Mon, 24 Mar 2014 20:05:14 GMT Server: Apache Access-Control-Allow-Origin: * Vary: Accept-Encoding Content-Encoding: gzip Content-Length: 98 Keep-Alive: timeout=10, max=500 Connection: Keep-Alive Content-Type: application/json
基本上这是代码:
$app->put('/update-user/:id', 'updateUser');
function updateUser($id){
$app = \Slim\Slim::getInstance();
$app->contentType('application/json');
$app->response()->header('Access-Control-Allow-Origin', '*');
$app->response()->header('Content-Type', 'application/json');
$response = $app->response();
$body = $app->request()->getBody();
$user = json_decode($body);
// $response = array();
$sql = "update usr SET experiencie=:experiencie WHERE id=:id";
try {
$db = PDOConnection();
$stmt = $db->prepare($sql) or die("Error: query preparation to database failed.");
$stmt->bindParam("experiencie", $user->experiencie);
$stmt->bindParam("id", $id) or die("Error:: fallo en parámetro -> id");
$stmt->execute();
$db = null;
if ($stmt->execute()) {
$response['error'] = false;
$response['text'] = '"Good!"';
} else {
$response['error'] = true;
$response['text'] = '"Bad!"';
}
echo json_encode($response);
} catch (PDOException $e) {
error_log($e->getMessage(), 3, '/var/tmp/php.log');
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
我收到status 200 ok,但无论好坏都没有任何响应,并且数据库没有反映任何更改。
提前致谢。
【问题讨论】:
-
对不起语法错误,我不得不编辑主要代码,以便我可以提问。
标签: php api frameworks slim