[英]PHP - Content-type not specified assuming application/x-www-form-urlencoded
For 2 days I'm having trouble with my PHP script on my server.两天来,我的服务器上的 PHP 脚本出现问题。 I've changed nothing and suddenly it didn't work anymore.
我什么都没改变,突然它不再起作用了。
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
注意:file_get_contents():未指定内容类型,假设 application/x-www-form-urlencoded 在
Warning: file_get_contents( https://mobile.dsbcontrol.de ): failed to open stream: HTTP request failed.
警告:file_get_contents( https://mobile.dsbcontrol.de ):未能打开 stream:HTTP 请求失败。 HTTP/1.1 500 Internal Server Error in
HTTP/1.1 500 内部服务器错误
But when I try the script locally it works perfectly.但是当我在本地尝试脚本时,它可以完美运行。
You are passing $contexts
to file_get_contents()
and that only contains the User-Agent
header in the $opts
array. 您正在将
$contexts
传递给file_get_contents()
,并且只包含$opts
数组中的User-Agent
标头。 All other headers and options are in the $options
array which you add in to $context
but aren't using. 所有其他标头和选项都在
$options
数组中,您添加到$context
但未使用。 Try: 尝试:
$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);
While the existing answers did not work for me, I managed to solve the problem like this: 虽然现有的答案对我不起作用,但我设法解决了这样的问题:
The PHP Manual says params
must be an associative array in the format $arr['parameter'] = $value
. PHP手册说
params
必须是$arr['parameter'] = $value
格式的关联数组。 Refer to context parameters for a listing of standard stream parameters. 有关标准流参数的列表,请参阅上下文参数。
$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);
i'm using this我在用这个
$url = '';
$result = json_decode(file_get_contents($url, false, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => http_build_query($dataQuery)
)
))), true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.