api - PHP Slim Framework: PUT method "Unexpected token S" -


greetings

i'm new slim micro framework, , not pro in php. i've been looking how similar answer relate problem , haven't had results.

my problem is, i'm trying update database trough api, put method framework offers, , every time , running request,

unexpected token s

when request header is:

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, 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 , response header 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

basically code:

 $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 id=:id"; try {     $db = pdoconnection();     $stmt = $db->prepare($sql) or die("error: query preparation 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() .'}}';   } } 

i status 200 ok, not response if either or bad, , database not reflects changes.

thanks in advance.

my guess error occurs while trying decipher hand-rolled error json. put, never produce json manually. change exception handler this...

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

assuming have pdo set throw exceptions, rid of or die() statements.

you calling $stmt->execute() twice closing connection before you're done it. code work better as...

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()]]); } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -