繁体   English   中英

PHP - 假设应用程序/x-www-form-urlencoded 未指定内容类型

[英]PHP - Content-type not specified assuming application/x-www-form-urlencoded

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

这是代码:

$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 在

警告:file_get_contents( https://mobile.dsbcontrol.de ):未能打开 stream:HTTP 请求失败。 HTTP/1.1 500 内部服务器错误

但是当我在本地尝试脚本时,它可以完美运行。

您正在将$contexts传递给file_get_contents() ,并且只包含$opts数组中的User-Agent标头。 所有其他标头和选项都在$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);

虽然现有的答案对我不起作用,但我设法解决了这样的问题:

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

我在用这个

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM