title: An Error occurred while handling another error: yii\web\HeadersAlreadySentException: Headers already sent
body: <p>I am developing a rest api web service with yii2.
  in response as json on localhost, a null object end of json response to me.
  and on host a error end of json.</p>
  <p>In localhost, the response I receive is as follows :
  response on localhost :</p>
  <pre><code>{
      &quot;status&quot;: 1,
      &quot;data&quot;: {
          &quot;data&quot;: &quot;Factor Api&quot;
      }
  }null
  </code></pre>
  <p>but on server, the response I receive is as follows :
  response on server :</p>
  <pre><code>{
      &quot;status&quot;: 1,
      &quot;data&quot;: {
          &quot;data&quot;: &quot;Factor Api&quot;
      }
  }&lt;pre&gt;An Error occurred while handling another error:
  yii\web\HeadersAlreadySentException: Headers already sent in /home/factorap/public_html/common/components/Api.php on line 53. in /home/factorap/public_html/vendor/yiisoft/yii2/web/Response.php:366
  Stack trace:
  #0 /home/factorap/public_html/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response-&amp;gt;sendHeaders()
  #1 /home/factorap/public_html/vendor/yiisoft/yii2/web/ErrorHandler.php(136): yii\web\Response-&amp;gt;send()
  #2 /home/factorap/public_html/vendor/yiisoft/yii2/base/ErrorHandler.php(135): yii\web\ErrorHandler-&amp;gt;renderException(Object(yii\web\HeadersAlreadySentException))
  #3 [internal function]: yii\base\ErrorHandler-&amp;gt;handleException(Object(yii\web\HeadersAlreadySentException))
  #4 {main}
  Previous exception:
  yii\web\HeadersAlreadySentException: Headers already sent in /home/factorap/public_html/common/components/Api.php on line 53. in /home/factorap/public_html/vendor/yiisoft/yii2/web/Response.php:366
  Stack trace:
  #0 /home/factorap/public_html/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response-&amp;gt;sendHeaders()
  #1 /home/factorap/public_html/vendor/yiisoft/yii2/base/Application.php(656): yii\web\Response-&amp;gt;send()
  #2 /home/factorap/public_html/common/components/Api.php(56): yii\base\Application-&amp;gt;end()
  #3 /home/factorap/public_html/api/controllers/SiteController.php(92): common\components\Api-&amp;gt;sendSuccessResponse(Array)
  #4 [internal function]: api\controllers\SiteController-&amp;gt;actionIndex()
  #5 /home/factorap/public_html/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
  #6 /home/factorap/public_html/vendor/yiisoft/yii2/base/Controller.php(180): yii\base\InlineAction-&amp;gt;runWithParams(Array)
  #7 /home/factorap/public_html/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller-&amp;gt;runAction(&amp;#039;&amp;#039;, Array)
  #8 /home/factorap/public_html/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module-&amp;gt;runAction(&amp;#039;&amp;#039;, Array)
  #9 /home/factorap/public_html/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application-&amp;gt;handleRequest(Object(yii\web\Request))
  #10 /home/factorap/public_html/api/v2/index.php(17): yii\base\Application-&amp;gt;run()
  #11 {main}&lt;/pre&gt;
  </code></pre>
  <p>common/components/Api.php on line 53 =  $this-&gt;setHeader(200);</p>
  <p>My function to send response :
  api function :</p>
  <pre><code>    public function sendSuccessResponse($data = false,$additional_info = false)
      {
  
          $this-&gt;setHeader(200);
  
          $response = [];
          $response['status'] = 1;
  
          if (is_array($data))
              $response['data'] = $data;
  
          if ($additional_info) {
              $response = array_merge($response, $additional_info);
          }
  
          $response = Json::encode($response, JSON_PRETTY_PRINT);
          //$strpos = strpos($response,'null');
  
  
          if (isset($_GET['callback'])) {
              /* this is required for angularjs1.0 client factory API calls to work */
              $response = $_GET['callback'] . &quot;(&quot; . $response . &quot;)&quot;;
  
              echo $response;
          } else {
              echo $response;
          }
  
          Yii::$app-&gt;end();
  
      }
  </code></pre>
  <p>and controller action to run.
  controller action :</p>
  <pre><code>    public function actionIndex()
      {
          Yii::$app-&gt;api-&gt;sendSuccessResponse(['data' =&gt; 'Factor Api']);
      }
  </code></pre>
  <p>Please help me to fix them.</p>