[英]content-type wasn send by setting it with header() in php
我在通過php函數header()設置內容類型時遇到問題; 我完整的代碼永遠不會發送任何輸出。 只有響應對象可以做到。 在每個輸出之前,我發送標題。 根據我的Accept-Header,我設置一個Content-Type並進行正確的輸出,它看起來很簡單:
switch($aceptHeader){
case 'text/html':
#and some othe stupid acept-headers of the ie
$this->setHeader('Content-Type: text/html; charset=".."');
$this->sendHeaders();
$this->sendDefault();
break;
case 'application/json':
$this->setHeader('Content-Type: application/json');
$this->sendJSON();
break;
case 'application/xml':
$this->setHeader('Content-Type: application/xml');
$this->sendXML();
break;
}
setHeader的方法僅填充一個數組,將通過foreach循環將wicht放入header()中; 功能。 這並不重要,因為我通過直接將$this->setHeader()
更改為header('Content-Type..');
來進行$this->setHeader()
header('Content-Type..');
它具有相同的結果(稍后)輸出僅是由樹枝模板渲染數組的結果。 這是唯一的輸出。 標頭的設置不是問題。 當我執行print_r(header_list());
時,我什至得到正面的結果print_r(header_list());
。 在那里,我確切地看到了我之前設置的標頭。 我在幾乎所有瀏覽器中都看到相同的響應標頭,但在每種瀏覽器中卻看不到:ie8在預標記后的正文中僅向我顯示html代碼作為字符串。 這就是為什么我用w3c驗證器(和其他工具)測試頁面的原因。 全部顯示相同的結果:
他們沒有Content-Type標頭
我可能會犯什么錯誤? 在設置標題之前可能有任何錯誤嗎? -id不執行任何狂雜的事情或其他任何操作-標頭的設置沒有問題-我在請求和響應之間的模型中唯一更改的標頭是http狀態,其后是$response-send();
幾乎每次都
渲染樹枝模板時可以對標頭進行操作嗎?
問題是:什么會干擾header()函數對內容類型標頭的設置?
一個有趣的事:我將以下代碼放在index.php的開頭
header('Content-Type: text/html');
echo 'test';
exit;
然后我將通過所有驗證工具進行綠色測試。 標頭已發送。
我可以遍歷整個代碼,看一下我在哪里松開了標頭,但這可能要花很長時間。 有誰遇到過同樣的問題,或者可以想象我做錯了什么?
這只是一個類似的示例。 開關塊不是問題,sendHeader也不是。 因為我整個下午都在不同情況下通過直接設置標題來替換它們-每次都具有相同的結果。
我在不同情況下使用了這些輸出開關,每次都能正常工作。 我發送json,並帶有jQuery的$ .ajax()接受的標頭作為json而不是字符串。 我為我的RESTful API生產了xml,它可以正常工作,並以我想要的格式發送數據。 僅驗證器工具和ie8不喜歡它。 我不明白為什么...
switch($aceptHeader){
case 'text/html':
#and some othe stupid acept-headers of the ie
$this->setHeader('Content-Type: text/html; charset=".."');
$this->sendHeaders();
$this->sendDefault();
break;
case 'application/json':
$this->setHeader('Content-Type: application/json');
$this->sendJSON();
break;
case 'application/xml':
$this->setHeader('Content-Type: application/xml');
$this->sendXML();
break;
}
忘記了告訴代碼何時嘗試下一種情況的時間間隔。
我解決了問題。
和...
這是實施的問題。 這是您看不到的東西,因為我沒有發布。 確實,這是默認路徑,我將客戶端希望獲得的路徑發送為Content-Type。 意思是如果客戶端(IE8或驗證工具)未發送任何接受標頭或我發送的空字符串並且“ Content-Type:”,則失敗。 我會哭。 grrrrrrrrrr。 抱歉
為了讓您知道我的正確實現(我需要一些重構):
$acceptFormat = $this->request->getAcceptFormat();
#do a switch for the type of the output and send the headers before
switch ($acceptFormat ){
case 'text/html':
case 'application/x-www-form-urlencoded':
case 'application/x-ms-application':
case 'application/vnd.wap.xhtml+xml':
case '*/*':
$this->setHeader('content-type: text/html; charset=UTF-8');
$this->_sendHeaders();
$this->_sendDefault();
break;
case 'application/json':
$this->setHeader('Content-Type: application/json');
$this->_sendHeaders();
$this->_sendJSON();
break;
case 'application/xml':
$this->setHeader('Content-Type: application/xml');
$this->_sendHeaders();
$this->_sendXML();
break;
default:
$this->debugger->log('Unknown Accept-Header set: '.$acceptFormat.' for setting the Content-Type ');
#$this->setHeader('content-type: '.$acceptFormat);
$this->setHeader('content-type: text/html; charset=UTF-8');
$this->_sendHeaders();
$this->_sendDefault();
break;
}
評論的部分是過錯。
哦,我的上帝。 ...
但我無法想象,瀏覽器將不會發送任何內容作為接受類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.