2012-04-02 90 views
4

我正在創建一個腳本,該腳本應該發送請求到bugzilla安裝,以登錄用戶併發布錯誤。php file_get_contents將錯誤的內容類型發送到Bugzilla安裝

我正在使用谷歌代碼上提供的BugzillaPHP http://code.google.com/p/bugzillaphp/ 所有在我的本地服務器上工作正常,但沒有腳本應該運行的遠程服務器上。

我從Bugzilla的取回的錯誤是:

內容類型必須是 '文本/ XML', '多/ *', '應用程序/肥皂+ XML', '或' 應用程序/ (而不是'application/x-www-form-urlencoded')。

這意味着我的腳本在標頭中發送了錯誤的內容類型(或者Bugzilla錯誤地檢測到標題)。 但是我很確定內容類型設置爲正確的值。 這是我的代碼:

$context = stream_context_create(array('http' => array(
     'method' => 'POST', 
     'header' => 'Content-Type: text/html', 
     'content' => $body 
    ))); 


    $response = file_get_contents($url, false, $context); 

任何想法?

回答

0
$context = stream_context_create(array('http' => array(
     'method' => 'POST', 
     'header' => "Content-Type: text/html\r\n", 
     'content' => $body 
    ))); 

請注意\r\n在標頭值的末尾。

+0

我剛剛嘗試過這一點,但我得到了相同的結果。這也不能解釋爲什麼它完全在我的本地服務器上工作。無論如何感謝 – Martin 2012-04-02 12:39:25

1

您應該在數組中存儲標題。

$context = stream_context_create(array('http' => array(
    'method' => 'POST', 
    'header' => array("Content-Type: text/html"), 
    'content' => $body 
))); 
+0

我剛剛嘗試過,但我得到了同樣的結果。這也不能解釋爲什麼它完全在我的本地服務器上工作。我也在內容類型的末尾嘗試了\ r \ n。無論如何謝謝 – Martin 2012-04-02 12:39:49

2

什麼php版本是您的遠程服務器? 5.2中存在一個阻止標題被髮送的錯誤。需要在stream_context_create之前添加到ini_set中:

$params = array('http' => array(
     'method' => 'POST', 
     'header' => 'Content-Type: text/html', 
     'content' => $body 
    )); 

    // workaround for php bug where http headers don't get sent in php 5.2 
    if(version_compare(PHP_VERSION, '5.3.0') == -1){ 
     ini_set('user_agent', 'PHP-SOAP/' . PHP_VERSION . "\r\n" . $params['http']['header']); 
    } 

    $context = stream_context_create($params); 
    $response = file_get_contents($url, false, $context); 
+0

沒有運氣。遠程服務器在PHP 5.3.3上 - 腳本在PHP 5.3.6中工作的本地服務器。不幸的是,遠程服務器是共享主機,否則我只是複製相同的設置。 – Martin 2012-04-03 07:27:13