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

Asked

Viewed 1,510 times

2

In this code I am 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’re 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 with the Response = file_get_contents($imgurURL, FALSE, $context);

I don’t understand why you’re giving me this "mistake"

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

  • Have you tried concatenating the header with the content-type? It seems to me that you are using comma.

  • Yes, what I did was 'header' => "Authorization: Bearer sdf541gs6df51gsd1bsb16etb16teg1etr1ge61g n". "User-Agent:Myagent/1.0 r n", but still nothing

  • but in your example has a comma and not a point.

  • I misread the answer and when I tried to edit it did not leave me. I managed to solve in this way 'header' => "Authorization: Bearer e9e87b8334e9dafb3b14f55993b1b1bc4515f7c4 n". "Content-Type: application/x-www-form-urlencoded",

2 answers

2

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

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

2

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

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 only in the comma that was at the end of the line of the header parameter. I changed the chunk etr1ge61g\n" , "Content- for this etr1ge61g\n" . "Content-

Browser other questions tagged

You are not signed in. Login or sign up in order to post.