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

0

In this code I'm creating to upload profile images to imgur

if (isset($_POST['uploadprofileimg'])) {
    $image = base64_encode(file_get_contents($_FILES['profileimg']['tmp_name']));

    $options = array('http' => array(
        'method' => "POST",
        'header' => "Authorization: Bearer sdf541gs6df51gsd1bsb16etb16teg1etr1ge61g\n",
        "Content-Type: application/x-www-form-urlencoded",
        'content' => $image
    ));

    $context = stream_context_create($options);

    $imgurURL = "https://api.imgur.com/3/image";

    $response = file_get_contents($imgurURL, FALSE, $context);
}

You are giving me this information:

  

Notice: file_get_contents (): Content-type not specified assuming application / x-www-form-urlencoded in C: \ WebServer \ Apache24 \ Apache24 \ htdocs \ html \ www \ SocialNetwork \ my-account.php on line 17

Line 17 is the one that has response = file_get_contents ($ imgurURL, FALSE, $ context);

I do not understand why I'm giving this "error"

I've tried adding "User-Agent: MyAgent / 1.0 \ r \ n", and "Connection: close" but without success

    
asked by anonymous 20.02.2017 / 19:45

2 answers

0

The error is in the formation of $ options. Content-Type is not a valid parameter. It is an http header and the right place would be in $ options ['header'];

Notice the concatenation of the string.

Follows:

if (isset($_POST['uploadprofileimg'])) {
    $image = base64_encode(file_get_contents($_FILES['profileimg']['tmp_name']));

    $options = array('http' => array(
        'method' => "POST",
        'header' =>
            "Authorization: Bearer sdf541gs6df51gsd1bsb16etb16teg1etr1ge61g\n" . // ---
            "Content-Type: application/x-www-form-urlencoded",
        'content' => $image
    ));

    $context = stream_context_create($options);

    $imgurURL = "https://api.imgur.com/3/image";

    $response = file_get_contents($imgurURL, FALSE, $context);
}
    
20.02.2017 / 23:11
0

The problem with your code is that you are adding an extra parameter to the array instead of concatenating it.

Change this code:

$options = array('http' => array(
        'method' => "POST",
        'header' => "Authorization: Bearer sdf541gs6df51gsd1bsb16etb16teg1etr1ge61g\n",
        "Content-Type: application/x-www-form-urlencoded",
        'content' => $image
    ));

For this:

 $options = array('http' => array(
            'method' => "POST",
            'header' => 
"Authorization: Bearer sdf541gs6df51gsd1bsb16etb16teg1etr1ge61g\n" .
"Content-Type: application/x-www-form-urlencoded",
            'content' => $image
        ));

Note : The change is just in the comma that was at the end of the header parameter line. I changed the% wrapper% by this% wrapper%

    
21.02.2017 / 12:36