PHP - 内容类型未指定假设 application/x-www-form-urlencoded
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20149556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Content-type not specified assuming application/x-www-form-urlencoded
提问by hannsch
For 2 days I'm having trouble with my PHP script on my server. I've changed nothing and suddenly it didn't work anymore.
两天来,我的服务器上的 PHP 脚本出现问题。我什么都没有改变,突然它不再起作用了。
Here is the code:
这是代码:
$query = http_build_query($data);
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($query)."\r\n",
'method' => "POST",
'content' => $query,
),
);
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n",'method' => 'POST',
'content' => http_build_query($data),));
$contexts = stream_context_create($opts);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $contexts, -1, 40000);
I'm getting these error messages:
我收到这些错误消息:
Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in
Warning: file_get_contents(https://mobile.dsbcontrol.de): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in
注意:file_get_contents(): Content-type 未指定假设 application/x-www-form-urlencoded in
警告:file_get_contents( https://mobile.dsbcontrol.de):无法打开流:HTTP 请求失败!HTTP/1.1 500 内部服务器错误
But when I try the script locally it works perfectly.
但是当我在本地尝试脚本时,它运行良好。
回答by AbraCadaver
You are passing $contextsto file_get_contents()and that only contains the User-Agentheader in the $optsarray. All other headers and options are in the $optionsarray which you add in to $contextbut aren't using. Try:
您正在传递$contexts给file_get_contents()并且只包含数组中的User-Agent标题$opts。所有其他标题和选项都在$options您添加$context但未使用的数组中。尝试:
$query = http_build_query($data);
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($query)."\r\n".
"User-Agent:MyAgent/1.0\r\n",
'method' => "POST",
'content' => $query,
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context, -1, 40000);
回答by Misaq
While the existing answers did not work for me, I managed to solve the problem like this:
虽然现有的答案对我不起作用,但我设法解决了这样的问题:
The PHP Manualsays paramsmust be an associative array in the format $arr['parameter'] = $value. Refer to context parameters for a listing of standard stream parameters.
PHP手册说params必须是格式的关联数组$arr['parameter'] = $value。有关标准流参数的列表,请参阅上下文参数。
$header = array(
"Content-Type: application/x-www-form-urlencoded",
"Content-Length: ".strlen($postdata)
);
$packet['method'] = "POST";
$packet['header'] = implode("\r\n", $header);
$packet['content'] = $postdata;
$transmit_data = array('http' => $packet);
$context = stream_context_create($transmit_data);

