2012-04-02 92 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