【问题标题】:imgur commerce - how upload image to album?imgur commerce - 如何将图像上传到相册?
【发布时间】:2016-03-29 22:03:00
【问题描述】:

对于在 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.:我们正在测试商业账户。

【问题讨论】:

    标签: php imgur


    【解决方案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 var 设置为base64 .. 然后传递给您的请求:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-12
      • 2020-11-13
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多