尽管将内容类型设置为'application / json',PHP Slim仍会在响应中添加其他文本

时间:2017-11-02 13:48:01

标签: php slim

尽管添加了代码返回响应,内容类型为application / json。 我只想要JSON数据但是添加额外的垃圾而不管我调试PHP时的变化我可以看到Route.php正在添加带有这些附加数据的$ output

`$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);
}});'
     

`

我收到以下错误。我有各种方法,如withJson和使用请求,响应接口仍然在下面添加文本  使用我的JSON响应数据

  

{"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>

1 个答案:

答案 0 :(得分:0)

当我使用XDebug调试PHP时,由于错误而正在编写其他内容。

我可以禁用XDebug和PHP错误报告,并在代码中处理异常。

在我的PHP文件中添加了以下代码。

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


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

此问题还与作为null发送的字段之一有关。请求中的deptID字段导致此垃圾数据被添加到响应中。

我已经纠正了这一点,并且总是尝试从客户端传递该deptID的空白值,或者我可以在服务器端处理它,但这可能会产生复杂性。