imgur商业-如何上传图片到相册


imgur commerce - how upload image to album?

对于在imgur.com上上传相册中的图像,我们使用it信息和it代码:

$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);

但结果得到误差CCD_ 1。

请告诉我为什么我们会出错,以及如何在相册中上传图像?

附言:我们正在测试商务科目。

在发送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变量设置为base64。然后直接传递到您的请求:

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

它的格式似乎不正确。我还没有完全测试过,但它应该能确保你有正确的基础64。