首页 > 解决方案 > 通过 API(v3) 上传的视频未附加到我在 Imgur 中的帐户

问题描述

代码片段:

这是我的代码片段:

function uploadByCurl($uploadFilePath, $accessToken){
    $ch = curl_init();
    $mimeType = mime_content_type($uploadFilePath);
    $fileType = '';
    if(strpos($mimeType, 'image')!==false){
        $fileType = 'image';
    }else if(strpos($mimeType, 'video')!==false){
        $fileType = 'video';
    }
    if(!$fileType){
        return false;
    }
    $filename = basename($uploadFilePath);
    $options = [
        CURLOPT_URL => 'https://api.imgur.com/3/upload',
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            'Authorization:Bearer ' . $accessToken,
        ],
        CURLOPT_POSTFIELDS => [
            //$fileType's value can be 'image' or 'video'
            $fileType => new \CURLFile($uploadFilePath),
            'type' => 'file',
            'name' => $filename,
            'title' => $filename,
            'description' => $filename,
        ],
        //In case you're in Windows, sometimes will throw error if not set SSL verification to false
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_TIMEOUT => 30,
    ];
    //In case you need a proxy
    //$options[CURLOPT_PROXY] = 'http://127.0.0.1:1087';
    // var_dump($options);exit;
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    if(curl_errno($ch)){
        echo 'Error: ' . curl_error($ch);
    }
    return $result;
}

$uploadFilePath = '/path/to/YOU_VIDEO.mp4';
$accessToken = '<YOU ACCESS TOKEN>';
$result = uploadByCurl($uploadFilePath, $accessToken);
var_dump($result);

上传图片

上面的代码片段可以用来上传图片和视频,对于图片,它可以正常工作,成功上传图片并返回图片链接,最重要的是,图片已附加到我的Imgur帐户,响应json是这样的:

{
  "data": {
    "id": "C1CuRYw",
    "title": "Xnip2020-04-04_15-43-41.jpg",
    "description": "Xnip2020-04-04_15-43-41.jpg",
    "datetime": 1585987477,
    "type": "image/jpeg",
    "animated": false,
    "width": 748,
    "height": 632,
    "size": 126123,
    "views": 0,
    "bandwidth": 0,
    "favorite": false,
    "account_id": 96115121,
    "is_ad": false,
    "in_most_viral": false,
    "has_sound": false,
    "tags": [],
    "ad_type": 0,
    "ad_url": "",
    "edited": "0",
    "in_gallery": false,
    "deletehash": "uTg1rOZhEggKjgR",
    "name": "Xnip2020-04-04_15-43-41.jpg",
    "link": "https://i.imgur.com/C1CuRYw.jpg"
  },
  "success": true,
  "status": 200
}

上传视频

我也可以成功上传视频,响应json格式和上传图片一样:

{
  "status": 200,
  "success": true,
  "data": {
    "id": "SHCsEXK",
    "deletehash": "44oeDQCmP0Oba3o",
    "title": "testvideo.mp4",
    "description": "testvideo.mp4",
    "name": "",
    "type": "video/mp4",
    "width": 0,
    "height": 0,
    "size": 0,
    "views": 0,
    "bandwidth": 0,
    "animated": true,
    "favorite": false,
    "in_gallery": false,
    "in_most_viral": false,
    "has_sound": false,
    "is_ad": false,
    "link": "https://i.imgur.com/SHCsEXK.mp4",
    "tags": [],
    "processing": {
      "status": "pending"
    },
    "datetime": 1585986090,
    "mp4": "https://i.imgur.com/SHCsEXK.mp4",
    "hls": ""
  }
}

没问题,link但问题是我无法在我的 Imgur 帐户中看到此视频,这意味着它没有附加到我的帐户。

另一个上传端点

实际上,可以使用另外一个上传端点,即https://api.imgur.com/3/image(参见此处),我尝试更改:

CURLOPT_URL => 'https://api.imgur.com/3/upload',

至:

CURLOPT_URL => 'https://api.imgur.com/3/image',

它确实有效,这次视频成功附加到我的 Imgur 帐户,但重点是:响应中没有视频链接,这是来自https://api.imgur.com/3/image端点的响应 json:

{
  "data": {
    "ticket": "33565dc7"
  },
  "success": true,
  "status": 200
}

有什么ticket用?我从未在 api 文档上找到它。

锁定图标是什么意思?

我在这里找到了这个: 图标是什么意思在此处输入图像描述lock

标签: php-curlimgur

解决方案


推荐阅读