简体   繁体   中英

PHP Slim adding additional text in response despite setting content type to 'application/json'

Despite adding the code return response with content type as application/json. I want only JSON data but additional junk is added irrespective the changes when I debug the PHP I could see Route.php is adding $output which has this additional data

 `$app->POST('/api/user/new', function ($Request, $response) { $json = file_get_contents('php://input'); $obj = new users(); $obj = json_decode($json); // {"id":"1","typeId":"1","name":"Haridev Nirgude","phone":"8121155693", // "email":"haridevnirgude@gmail.com","deptID":null,"password":"Hyderabad143","insession":"0"} $typeId= $obj->typeId; $name= $obj->name; $phone=$obj->phone; $email=$obj->email; $deptID=$obj->deptID; $password=$obj->password; $userID = $obj->userID; $sql = "INSERT INTO users (typeId,name,phone,email,deptID,password,userID) VALUES (:typeId,:name,:phone,:email,:deptID,:password,:userID)"; try{ // Get DB Object $db = new db(); // Connect $db = $db->connect(); $stmt = $db->prepare($sql); //$stmt->bindParam(':',$); $stmt->bindParam(':typeId',$typeId); $stmt->bindParam(':name',$name); $stmt->bindParam(':phone',$phone); $stmt->bindParam(':email',$email); $stmt->bindParam(':deptID',$deptID); $stmt->bindParam(':password',$password); $stmt->bindParam(':userID', $userID); $stmt->execute(); $result = '{"type":"success","text": "User Registered successfully"}'; return $response->withStatus(200)->withHeader('Content-Type', 'application/json')->write($result); //echo '{"type":"success","text": "User Added successfully"}'; // echo '{"notice": {"text": "User Added successfully"}'; } catch(PDOException $e){ $result = '{"type":"error","text":'.$e->getMessage().'}'; return $response->withStatus(200)->withHeader('Content-Type', 'application/json')->write($result); }});' 

`

I am getting below error. I have various methods such as withJson and also using the Request, Response from interface still getting below text added with my JSON response data

{"type":"error","text":SQLSTATE[23000]: Integrity constraint
> violation: 1062 Duplicate entry 'Ha8' for key 'userID'}
> <br /> <font size='1'><table class='xdebug-error xe-notice' dir='ltr'
> border='1' cellspacing='0' cellpadding='1'> <tr><th align='left'
> bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000;
> color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined
> property: stdClass::$deptID in
> C:\xampp\htdocs\SlimSwachhmApp\src\routes\AuthUsers.php on line
> <i>134</i></th></tr> <tr><th align='left' bgcolor='#e9b96e'
> colspan='5'>Call Stack</th></tr> <tr><th align='center'
> bgcolor='#eeeeec'>#</th><th align='left'
> bgcolor='#eeeeec'>Time</th><th align='left'
> bgcolor='#eeeeec'>Memory</th><th align='left'
> bgcolor='#eeeeec'>Function</th><th align='left'
> bgcolor='#eeeeec'>Location</th></tr> <tr><td bgcolor='#eeeeec'
> align='center'>1</td><td bgcolor='#eeeeec'
> align='center'>0.1126</td><td bgcolor='#eeeeec'
> align='right'>374600</td><td bgcolor='#eeeeec'>{main}(  )</td><td
> title='C:\xampp\htdocs\SlimSwachhmApp\src\routes\AuthUsers.php'
> bgcolor='#eeeeec'>...\AuthUsers.php<b>:</b>0</td></tr> <tr><td
> bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec'
> align='center'>0.1723</td><td bgcolor='#eeeeec'
> align='right'>773568</td><td bgcolor='#eeeeec'>Slim\App->run( ???
> )</td><td
> title='C:\xampp\htdocs\SlimSwachhmApp\src\routes\AuthUsers.php'
> bgcolor='#eeeeec'>...\AuthUsers.php<b>:</b>196</td></tr> <tr><td
> bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec'
> align='center'>0.1997</td><td bgcolor='#eeeeec'
> align='right'>1289520</td><td bgcolor='#eeeeec'>Slim\App->process(
> ???, ??? )</td><td
> title='C:\xampp\htdocs\SlimSwachhmApp\vendor\slim\slim\Slim\App.php'
> bgcolor='#eeeeec'>...\App.php<b>:</b>295</td></tr> <tr><td
> bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec'
> align='center'>0.1999</td><td bgcolor='#eeeeec'
> align='right'>1289496</td><td
> bgcolor='#eeeeec'>Slim\App->callMiddlewareStack( ???, ??? )</td><td
> title='C:\xampp\htdocs\SlimSwachhmApp\vendor\slim\slim\Slim\App.php'
> bgcolor='#eeeeec'>...\App.php<b>:</b>370</td></tr> <tr><td
> bgcolor='#eeeeec' align='center'>5</td><td bgcolor='#eeeeec'
> align='center'>0.2000</td><td bgcolor='#eeeeec'
> align='right'>1289632</td><td bgcolor='#eeeeec'>Slim\App->__invoke(
> ???, ??? )</td><td
> title='C:\xampp\htdocs\SlimSwachhmApp\vendor\slim\slim\Slim\MiddlewareAwareTrait.php'
> bgcolor='#eeeeec'>...\MiddlewareAwareTrait.php<b>:</b>122</td></tr>
> <tr><td bgcolor='#eeeeec' align='center'>6</td><td bgcolor='#eeeeec'
> align='center'>0.2078</td><td bgcolor='#eeeeec'
> align='right'>1356576</td><td bgcolor='#eeeeec'>Slim\Route->run( ???,
> ??? )</td><td
> title='C:\xampp\htdocs\SlimSwachhmApp\vendor\slim\slim\Slim\App.php'
> bgcolor='#eeeeec'>...\App.php<b>:</b>476</td></tr> <tr><td
> bgcolor='#eeeeec' align='center'>7</td><td bgcolor='#eeeeec'
> align='center'>0.2078</td><td bgcolor='#eeeeec'
> align='right'>1356576</td><td
> bgcolor='#eeeeec'>Slim\Route->callMiddlewareStack( ???, ??? )</td><td
> title='C:\xampp\htdocs\SlimSwachhmApp\vendor\slim\slim\Slim\Route.php'
> bgcolor='#eeeeec'>...\Route.php<b>:</b>316</td></tr> <tr><td
> bgcolor='#eeeeec' align='center'>8</td><td bgcolor='#eeeeec'
> align='center'>0.2078</td><td bgcolor='#eeeeec'
> align='right'>1356712</td><td bgcolor='#eeeeec'>Slim\Route->__invoke(
> ???, ??? )</td><td
> title='C:\xampp\htdocs\SlimSwachhmApp\vendor\slim\slim\Slim\MiddlewareAwareTrait.php'
> bgcolor='#eeeeec'>...\MiddlewareAwareTrait.php<b>:</b>122</td></tr>
> <tr><td bgcolor='#eeeeec' align='center'>9</td><td bgcolor='#eeeeec'
> align='center'>0.2101</td><td bgcolor='#eeeeec'
> align='right'>1390032</td><td
> bgcolor='#eeeeec'>Slim\Handlers\Strategies\RequestResponse->__invoke(
> ???, ???, ???, ??? )</td><td
> title='C:\xampp\htdocs\SlimSwachhmApp\vendor\slim\slim\Slim\Route.php'
> bgcolor='#eeeeec'>...\Route.php<b>:</b>344</td></tr> <tr><td
> bgcolor='#eeeeec' align='center'>10</td><td bgcolor='#eeeeec'
> align='center'>0.2101</td><td bgcolor='#eeeeec'
> align='right'>1390032</td><td bgcolor='#eeeeec'><a
> href='http://www.php.net/function.call-user-func:{C:\xampp\htdocs\SlimSwachhmApp\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php:41}'
> target='_new'>call_user_func:{C:\xampp\htdocs\SlimSwachhmApp\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php:41}</a>
> ( ???, ???, ???, ??? )</td><td
> title='C:\xampp\htdocs\SlimSwachhmApp\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php'
> bgcolor='#eeeeec'>...\RequestResponse.php<b>:</b>41</td></tr> <tr><td
> bgcolor='#eeeeec' align='center'>11</td><td bgcolor='#eeeeec'
> align='center'>0.2101</td><td bgcolor='#eeeeec'
> align='right'>1390032</td><td
> bgcolor='#eeeeec'>Slim\Container->{closure:C:\xampp\htdocs\SlimSwachhmApp\src\routes\AuthUsers.php:110-191}(
> ???, ???, ??? )</td><td
> title='C:\xampp\htdocs\SlimSwachhmApp\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php'
> bgcolor='#eeeeec'>...\RequestResponse.php<b>:</b>41</td></tr>
> </table></font>

As I was using XDebug to debug PHP it's writing additional content due to error.

I could disable both XDebug and PHP error reporting and handled the exceptions in code.

Added below code in my PHP file.

error_reporting(0);// disable the error from PHP


 if (function_exists('xdebug_disable')) {
    xdebug_disable(); //disable xdebug error
  }

Also the problem was related to one of the field which is sent as null. deptID field in request which is causing this junk data to get added in response.

I have rectified that and always try to pass blank value for that deptID from client or I can handle it in server side but this can create complications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM