PHP-未指定应用程序/x-www-form-urlencoded的内容类型 [英] PHP - Content-type not specified assuming application/x-www-form-urlencoded

查看:177
本文介绍了PHP-未指定应用程序/x-www-form-urlencoded的内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在两天内,我的服务器上的PHP脚本出现问题.我什么都没改变,突然间它不再起作用了.

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.

这是代码:

$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);

我收到以下错误消息:

注意:file_get_contents():假设未指定内容类型 application/x-www-form-urlencoded

Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in

警告:file_get_contents( https://mobile.dsbcontrol.de ):无法打开流:HTTP请求失败的! HTTP/1.1 500内部服务器 错误

Warning: file_get_contents(https://mobile.dsbcontrol.de): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in

但是当我在本地尝试该脚本时,它会完美运行.

But when I try the script locally it works perfectly.

推荐答案

您正在将$contexts传递给file_get_contents(),并且仅在$opts数组中包含User-Agent标头.所有其他标头和选项位于$options数组中,您将其添加到$context中但未使用.试试:

You are passing $contexts to file_get_contents() and that only contains the User-Agent header in the $opts array. All other headers and options are in the $options array which you add in to $context but aren't using. 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);

这篇关于PHP-未指定应用程序/x-www-form-urlencoded的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆