PHP - Content-type not specified assuming application/x-www-form-urlencoded
Asked Answered
D

3

26

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.

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

But when I try the script locally it works perfectly.

Dulce answered 22/11, 2013 at 16:19 Comment(1)
I also ran into this issue. It looks like it was caused by upgrading packages. I added: $http['header'] = 'Content-Type: application/json' . "\r\n"; which suppresses the notice.Contracture
B
51

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);
Becerra answered 22/11, 2013 at 17:6 Comment(1)
the first error is gone thanks but im still having trouble with warning.Dulce
G
0

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

Guttapercha answered 6/4, 2017 at 21:30 Comment(1)
In my case I had specified content-type but there was no space that's why PHP engine could not find it. Added a space between another option and content-type and it worked. $this->options= ['http' => [ 'header' => 'Authorization:' . md5($ftpUserKey) . ' Content-type: application/json', 'method' => $method, 'content' => json_encode($this->params),]];Laparotomy
S
0

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);
Sindhi answered 22/8, 2022 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.