imgur commerce - 如何将图片上传到相册?

imgur commerce - how upload image to album?

对于 imgur.com 相册中的上传图片,我们使用 it information 并且代码:

$image = file_get_contents($_FILES['upload']['tmp_name']);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://imgur-apiv3.p.mashape.com/3/image');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: Client-ID ' . $client_id ));
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'X-Mashape-Key:  ' . $xmash ));
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type:  application/x-www-form-urlencoded' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'image' => base64_encode($image) ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'album' => $album_id ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'type' => 'base64' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'name' => 'test_name' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'title' => 'test title' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'description' => 'blablabla' ));
$result= curl_exec($ch);
var_dump($result);
curl_close($ch);

但结果我们得到错误 Invalid image type

请告诉我为什么会出现错误以及如何上传相册中的图片?

P.S.: 我们正在测试商业账户。

在发送 base64 之前确保您的图片结构正确..

$baseContent = "";

if (isset($_FILES['upload']['tmp_name'])) {
        $imgbinary = fread(fopen($_FILES['upload']['tmp_name'], "r"), filesize($_FILES['upload']['tmp_name']));
        $baseContent = 'data:image/png;base64,' . base64_encode($imgbinary);
    }

这会将 $baseContent var 设置为 base64.. 然后传递给您的请求:

curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'image' => $baseContent ));

看起来格式不正确。我还没有对此进行全面测试,但它应该确保您拥有正确的 base64。