我正在创建一个脚本,该脚本应该向bugzilla安装发送请求,以便登录用户并发布错误。
我正在使用Google代码http://code.google.com/p/bugzillaphp/上提供的BugzillaPHP 一切都在我的本地服务器上工作正常,但在脚本运行的远程服务器上没有。
我从Bugzilla回来的错误是:
Content-Type必须是'text / xml','multipart / *,''application / soap + xml,''或'application / dime'而不是'application / x-www-form-urlencoded'
这意味着我的脚本在标头中发送了错误的内容类型(或者Bugzilla错误地检测到标头)。 但是我很确定content-type设置为正确的值。 这是我的代码:
$context = stream_context_create(array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: text/html',
'content' => $body
)));
$response = file_get_contents($url, false, $context);
有什么想法吗?
答案 0 :(得分: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);
答案 1 :(得分:1)
您应该将标头存储在数组中。
$context = stream_context_create(array('http' => array(
'method' => 'POST',
'header' => array("Content-Type: text/html"),
'content' => $body
)));
答案 2 :(得分:0)
$context = stream_context_create(array('http' => array(
'method' => 'POST',
'header' => "Content-Type: text/html\r\n",
'content' => $body
)));
请注意标题值末尾的\r\n
。