2

Neste código que estou a criar para fazer o upload de imagens de perfil para o 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);
}

Está-me a dar esta informação:

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

A linha 17 é a que tem o response = file_get_contents($imgurURL, FALSE, $context);

Não estou a perceber o porquê de me estar a dar esse "erro"

Já experimentei adicionar "User-Agent:MyAgent/1.0\r\n", e"Connection: close" mas sem sucesso

4
  • Já experimentou concatenar o header com o content-type? Me parece que você está usando virgula.
    – jlHertel
    20/02/2017 às 20:39
  • Sim, o que fiz foi 'header' => "Authorization: Bearer sdf541gs6df51gsd1bsb16etb16teg1etr1ge61g\n". "User-Agent:MyAgent/1.0\r\n", mas mesmo assim nada 20/02/2017 às 20:48
  • mas no seu exemplo tem uma virgula e não um ponto.
    – jlHertel
    20/02/2017 às 21:06
  • Li mal a resposta e quando tentei editar não me deixou. Consegui resolver desta forma 'header' => "Authorization: Bearer e9e87b8334e9dafb3b14f55993b1b1bc4515f7c4\n". "Content-Type: application/x-www-form-urlencoded", 20/02/2017 às 21:09

2 Respostas 2

2

O erro está na formação do $options. Content-Type não é um parametro válido. Ele é um cabeçalho do http e o lugar certo dele seria no $options['header'];

Observe a concatenação da string.

Segue:

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

O problema no seu código é que você está adicionando um parâmetro a mais no array ao invés de concatenar.

Mude este código:

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

Por este:

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

Nota: A mudança está apenas na virgula que estava no final da linha do parâmetro header. Mudei o trecho etr1ge61g\n" , "Content- por este etr1ge61g\n" . "Content-

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .