I am creating an API where the output will be in json format. I am getting the required output but with header error that too when i upload on the server. There are no whitespace in my script and i have also tried putting ob_start and ob_end_clean() at the start and end of my script but of no use.
Here i am calling one API within other API will that be the cause for this header problem?
Is there any other method to get rid of this error? Please help.
public function userinteract(){
$usersData = new TeleDataModel();
$dataArr = $this->_readJSON();
//print_r($dataArr);
//------Get the mobile number------- //
if ($dataArr['mobile']==''){
$outputArr["check"] = "failed";
$outputArr["message"] = "Please enter your mobile number with ISD Code..";
$this->_writeJSON($outputArr);
return;
}
//--------Check if call is from India, if call is out of India then reject call--------//
$mobile=$dataArr['mobile'];
$mobile=urlencode($mobile);
if(substr($mobile,0,3)== "+91"){
//------Remove +91 from given number--------//
$mobile=preg_replace("/\+91/", '', $mobile);
//--------Check if user is registered or not---------//
a:$checkVerify=$usersData->checkExistingUser($mobile,'');
if(count($checkVerify) > 0){
for($i=0;$i<count($checkVerify);$i++){
$firstname=$checkVerify[$i]['firstname'];
}
if($firstname==''){
echo ('Not a registered user');
$check=$usersData->checkunregistered($mobile);
if($check!=0 && count($check)>0){
for($i=0;$i<count($check);$i++){
$count=$check[$i]['count'];
if(($count==1)||($count==2)||($count==3)){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://xyz dot com/folder/tele/adSelection"); //call to another API
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"mobile=$mobile");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
}
}
}
}else{
$outputArr['message']="No previous call records...";
$this->_writeJSON($outputArr);
return;
}
}
}
}
Update: This is the error I am getting:
Cannot modify header information - headers already sent by (output started at /home/xyz/public_html/folder/Controller.php:339) in /home/xyz/public_html/folder/Controller.php on line 338
There is a seperate function for _writeJSON and the code is:
private function _writeJSON($outputArr) {
$json = json_encode($outputArr);
header("Content-Type: application/json"); // line number 399
echo $json;
}