【问题标题】:PHP Slim Framework: PUT method "Unexpected token S"PHP Slim 框架:PUT 方法“意外的令牌 S”
【发布时间】: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


【解决方案1】:

我的猜测是在尝试破译您的手动 错误 JSON 时发生错误。简而言之,永远不要手动生成 JSON。将您的异常处理程序更改为此...

catch (PDOException $e) {
    error_log($e->getMessage(), 3, '/var/tmp/php.log');
    http_response_code(500);
    echo json_encode(['error' => ['text' => $e->getMessage()]]);
}

假设您已将 PDO 设置为抛出异常,请去掉 or die() 语句。

您还调用了两次$stmt->execute() 以及在完成连接之前关闭连接。你的代码会更好地工作......

try {
    if (!isset($user->experiencie)) {
        throw new Exception('Missing "experiencie" request parameter');
    }

    $db = PDOConnection();
    $stmt = $db->prepare($sql);

    $stmt->bindParam(':experiencie', $user->experiencie);

    $stmt->bindParam(':id', $id);
    if ($stmt->execute()) {
        $response['error'] = false;
        $response['text'] = 'Good!';
    } else {
         $response['error'] = true;
         $response['text'] = 'Bad!';
    }

    echo json_encode($response);
} catch (Exception $e) {
    error_log($e->getMessage(), 3, '/var/tmp/php.log');
    http_response_code(500);
    echo json_encode(['error' => ['text' => $e->getMessage()]]);
}

【讨论】:

  • 太好了,谢谢,这解决了这个问题,但是took me into another 我将不得不问一个完全不同的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
相关资源
最近更新 更多