简体   繁体   English

php file_get_contents向Bugzilla安装发送错误的内容类型

[英]php file_get_contents sending wrong content-type to a Bugzilla install

I am creating a script that should send a request to a bugzilla installation in order to login a user and post bugs. 我正在创建一个脚本,该脚本应该向bugzilla安装发送请求,以便登录用户并发布错误。

I am using BugzillaPHP available on Google Code http://code.google.com/p/bugzillaphp/ all is working fine on my local server but not on a remote server where the script should run from. 我使用的是Google Code上的BugzillaPHP http://code.google.com/p/bugzillaphp/所有在我的本地服务器上工作正常但在运行脚本的远程服务器上没有。

The error I am getting back from Bugzilla is: 我从Bugzilla回来的错误是:

Content-Type must be 'text/xml,' 'multipart/*,' 'application/soap+xml,' 'or 'application/dime' instead of 'application/x-www-form-urlencoded' Content-Type必须是'text / xml','multipart / *,''application / soap + xml,''或'application / dime'而不是'application / x-www-form-urlencoded'

This means that my script is sending in the header a wrong content-type (or Bugzilla is incorrectly detecting the header). 这意味着我的脚本在标头中发送了错误的内容类型(或者Bugzilla错误地检测到标头)。 However I am pretty sure the content-type is set to the correct value. 但是我很确定content-type设置为正确的值。 This is my code: 这是我的代码:

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


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

Any ideas? 有任何想法吗?

What php version is your remote server? 什么是php版本的远程服务器? There is a bug in 5.2 that blocks headers from being sent. 5.2中存在阻止发送标头的错误。 Need to add to ini_set before stream_context_create: 需要在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);

You should store headers in array. 您应该将标头存储在数组中。

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

Notice the \\r\\n at the end of the header value. 注意标题值末尾的\\r\\n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP函数file_get_contents检索编码的信息-标头:“'Content-Type:image / png'” - PHP function file_get_contents retrieves encoded information - header: “'Content-Type: image/png'” 带有get请求的file_get_contents中的错误处理和内容类型 - error handling and content-type in file_get_contents with get request file_get_contents():假设应用程序/x-www-form-urlencoded 未指定内容类型 - file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded file_get_contents获取了错误的内容 - file_get_contents is getting the wrong content CSV file_get_contents发送PHP - CSV file_get_contents sending PHP PHP将变量发送到file_get_contents() - PHP sending variables to file_get_contents() php file_get_contents 与 ajax 内容 - php file_get_contents with ajax content file_get_contents():假设application / x-www-form-urlencoded with imgur API,未指定Content-type - file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded with imgur API 重新验证错误; 注意:file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in - recaptcha error; Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in PHP:使用file_get_contents发送GET / POST - PHP: Sending GET/POST with file_get_contents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM