简体   繁体   English

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

[英]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.

相关问题 file_get_contents():假设应用程序/x-www-form-urlencoded 未指定内容类型 - file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded file_get_contents():假设application / x-www-form-urlencoded with imgur API,未指定Content-type - file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded with imgur API 重新验证错误; 注意:file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in - recaptcha error; Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in 使用Content-Type = application / x-www-form-urlencoded发送POST请求时,POST值为空 - POST value is empty when Sending POST request with Content-Type = application/x-www-form-urlencoded 如何使用内容类型从 webhook 获取数据:application/x-www-form-urlencoded;charset=UTF-8? - How to get data from webhook with content-type: application/x-www-form-urlencoded;charset=UTF-8? PHP curl将Content-Type更改为application / x-www-form-urlencoded。 不应该这样做 - PHP curl changes the Content-Type to application/x-www-form-urlencoded. Shouldn't do that 如何使用内容类型为“ application / x-www-form-urlencoded”的PHP curl发送原始JSON? - How can I send a raw JSON using PHP curl with the content type “application/x-www-form-urlencoded”? 如何通过在laravel中传递参数来获得内容类型application/x-www-form-urlencoded的响应 - How to get response of content type application/x-www-form-urlencoded by passing parameters in laravel ionic和php之间的application / x-www-form-urlencoded - application/x-www-form-urlencoded between ionic and php Php CURL虽然我将标题设置为application / json,但请求仍然是application / x-www-form-urlencoded - Php CURL Though I set the header as application/json the request goes as application/x-www-form-urlencoded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM