显示API输出时出现标题错误

时间:2016-03-19 11:02:41

标签: php

我正在创建一个API,其输出将采用json格式。我收到了所需的输出但是当我在服务器上传时也出现了标题错误。 我的脚本中没有空格,我也尝试将ob_start和ob_end_clean()放在脚本的开头和结尾但没有用。

这里我在其他API中调用一个API会导致此标头问题吗?

还有其他方法可以摆脱这个错误吗?请帮忙。

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;
            }
        }
    }
}

更新:这是我得到的错误:

无法修改标题信息 - 已在第338行的/home/xyz/public_html/folder/Controller.php中通过(输出/home/xyz/public_html/folder/Controller.php:339开始)输出的标题

_writeJSON有一个单独的函数,代码是:

private function _writeJSON($outputArr) {           
    $json = json_encode($outputArr);
    header("Content-Type: application/json"); // line number 399
    echo $json; 
}

0 个答案:

没有答案