imgur with php - 如何在我的相册中上传图片(错误401"需要验证")?

时间:2016-03-29 16:06:58

标签: php imgur

要在imgur.com上的相册中上传图片,请输入代码:

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

$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)); //. $xmash 
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type:  application/x-www-form-urlencoded' ));
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'image' => $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' ));

$reply = curl_exec($ch);
var_dump($reply);
curl_close($ch);

但现在我们得到的答案是错误的:

  

string(112)" {" data":{" error":" Authentication   所需""请求":" / 3 /图像""方法":" POST"}"成功& #34;:假,"状态":401}"

结果我们有一些问题:

  1. 我如何授权(在php上)?
  2. 文档https://market.mashape.com/imgur/imgur-9中的
  3. 需要粘贴Authorization HEADER AUTH。怎么弄他?

1 个答案:

答案 0 :(得分:0)

  

需要将这些连接到一个数组中,因此需要一组调用:

$headers = array('Authorization: Client-ID ' . $client_id, 'X-Mashape-Key: ' . $Mashape_Key)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);