Bad Request 400 загрузка изображений в imgur API - PullRequest
0 голосов
/ 12 февраля 2019

Я хочу загрузить несколько изображений с помощью imgur-API в imgur, но я всегда получаю ошибку 400 Bad Request.В чем проблема?

<?php
if (isset($_POST['uploadprofileimg'])) {
    $image = base64_encode(file_get_contents($_FILES['profileimg']['tmp_name']));
    $options = array('http'=>array(
            'method'=>"POST",
            'header'=>"Authorization: Bearer *MY_ACCESS_TOKEN*\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);
}
?>

<h1>My Account</h1>
 <form action="upload-pb.php" method="post" enctype="multipart/form-data">
    Upload a profile image:
    <input type="file" name="profileimg">
    <input type="submit" name="uploadprofileimg" value="Upload Image">
</form>

1 Ответ

0 голосов
/ 12 февраля 2019

Глядя на конечную точку для /image здесь , требуется параметр image.Вы передаете его как content и неправильно закодированы как image.Посмотрев здесь , я позаимствовал, как правильно загружать контент, используя stream_context_create / file_get_contents:

<?php
 if (isset($_POST['uploadprofileimg'])) {
    $image = base64_encode(file_get_contents($_FILES['profileimg']['tmp_name']));
    $postdata = http_build_query(
        array(
            'image' => $image,
        )
    );
    $options = array('http'=>array(
            'method'=>"POST",
            'header'=>"Authorization: Bearer *MY_ACCESS_TOKEN*\n".
            "Content-Type: application/x-www-form-urlencoded",
            'content' => $postdata
    ));
    $context = stream_context_create($options);
    $imgurURL = "https://api.imgur.com/3/image";
    $response = file_get_contents($imgurURL, false, $context);
...