【问题标题】:Bad Request 400 uploading images to the imgur API将图像上传到 imgur API 的错误请求 400
【发布时间】:2019-07-05 13:04:41
【问题描述】:

我想用 imgur-API 将一些图片上传到 imgur,但我总是收到错误 400 错误请求。有什么问题?

<?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>

【问题讨论】:

    标签: php html imgur


    【解决方案1】:

    查看/imagehere的端点,需要image的参数。您将其作为content 传递,但未正确编码为image。看here,借用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);
    

    【讨论】:

    • 另外,考虑一起跳过base64_encode。它将使上传更小,并且应该可以将二进制文件上传到 imgur。
    猜你喜欢
    • 1970-01-01
    • 2021-02-11
    • 2015-11-01
    • 2015-10-20
    • 2020-10-31
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    相关资源
    最近更新 更多