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

时间:2016-03-29 13:50:20

标签: php imgur

对于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。:我们是测试商务会员。

1 个答案:

答案 0 :(得分:0)

在发送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。