I want to return data with the HTTP error code in Yii 1. So I used the following way to get the data.
$code = (0 == $ex->getCode()) ? (isset($ex->statusCode) ? $ex->statusCode : 500) : $ex->getCode();
$this->setOutputError($ex->getMessage());
$this->setOutputCode($code);
When I use it this way API returns data with 200 error code as below

But I want to change header status 200, so I threw exception for this, then output data also changed. I want to change only the header status.
$code = (0 == $ex->getCode()) ? (isset($ex->statusCode) ? $ex->statusCode : 500) : $ex->getCode();
$this->setOutputError($ex->getMessage());
$this->setOutputCode($code);
throw new CHttpException(400, 'Bad Request');
Advertisement
Answer
Yii 1.1 does not have response abstraction, you need to use http_response_code() to change response status code:
$code = (0 == $ex->getCode()) ? (isset($ex->statusCode) ? $ex->statusCode : 500) : $ex->getCode(); $this->setOutputError($ex->getMessage()); $this->setOutputCode($code); http_response_code(400);
Alternatively you may also use header(), but this is more tricky.
