Blog Posts » PHP » [PHP] Upload ke imgur.com menggunakan API v3

[PHP] Upload ke imgur.com menggunakan API v3

Setelah sekian lama fitur upload gambar di blog ini dibiarkan tidak berfungsi karena masih menggunakan imgur API v2, akhirnya ada sedikit semangat untuk update ke versi 3. Saya akan share contoh skrip PHP nya. Saya buat menjadi satu file namun mudah dipahami. Bisa langsung dijalankan di server. Didalamnya juga terdapat cara untuk mendapatkan link thumbnail pada API v3 ini, yang berbeda dari API v2. [Demo]
Langsung saja berikut kode nya:

imgur.php [Textarea][RAW][Download]

  1. <?php
  2. # imgur Uploader (v3.0.1)
  3. # Made only for educational purposes
  4. # (c) 20161209 nggit
  5. $url       = 'https://api.imgur.com/3/image.json'; // API endpoints, info: https://api.imgur.com/endpoints/image#image-upload
  6. $client_id = 'd4fc5abb11c0ac3'; // Get client_id here: https://api.imgur.com/#registerapp
  7. switch ($_SERVER['QUERY_STRING']) {
  8.     case 'upload':
  9.         if ($file = isset($_FILES['file']) ? $_FILES['file'] : false) {
  10.             if ($file['error'] == UPLOAD_ERR_NO_FILE) {
  11.                 exit('Nothing to upload?');
  12.             }
  13.             if ($file['size'] > 1000000 || $file['error'] == UPLOAD_ERR_INI_SIZE) {
  14.                 exit('Sorry, your file is too large');
  15.             }
  16.             if (!exif_imagetype($file['tmp_name'])) {
  17.                 exit('File is not an image');
  18.             }
  19.             if ($file['error'] == UPLOAD_ERR_OK) {
  20.                 $fh   = fopen($file['tmp_name'], 'r');
  21.                 $read = fread($fh$file['size']);
  22.                 fclose($fh);
  23.                 $post = array(
  24.                             'image' => base64_encode($read)
  25.                         );
  26.                 $ch   = curl_init();
  27.                 curl_setopt($ch, CURLOPT_URL, $url);
  28.                 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  29.                 curl_setopt($ch, CURLOPT_POST, true);
  30.                 curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  31.                 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  32.                                                          'Authorization: Client-ID '.$client_id
  33.                                                      ));
  34.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  35.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  36.                 $json = curl_exec($ch); // Response, info: https://api.imgur.com/#responses
  37.                 curl_close($ch);
  38.                 // $image = json_decode($json, true); // Array
  39.                 $image = json_decode($json); // Object
  40.                 // var_dump($image);
  41.                 // $original = $image['data']['link']; // Array-style
  42.                 $original = $image->data->link; // Object-style
  43.                 $thumb    = substr_replace($original, 't', -4, 0); // t = Small Thumbnail, info: https://api.imgur.com/models/image
  44.                 echo '<img src="'.$thumb.'" alt="'.$image->data->id.'" /><br /><a href="'.$original.'">'.$original.'</a>'; // Example
  45.                 // ...
  46.             }
  47.             else {
  48.                 echo 'Upload was failed. Error code: '.$file['error'];
  49.             }
  50.         }
  51.         break;
  52.     default:
  53.         echo '<!DOCTYPE html>'."\n"
  54.             .'<html>'."\n"
  55.             .'    <head>'."\n"
  56.             .'        <title>imgur Uploader</title>'."\n"
  57.             .'    </head>'."\n"
  58.             .'    <body>'."\n"
  59.             .'        <div>'."\n"
  60.             .'            <h2>imgur Uploader (v3.0.1)</h2>'."\n"
  61.             .'            <p>Upload an image to imgur.com.</p>'."\n"
  62.             .'        </div>'."\n"
  63.             .'        <form action="'.basename($_SERVER['SCRIPT_NAME']).'?upload" method="post" enctype="multipart/form-data">'."\n"
  64.             .'            <div>'."\n"
  65.             .'                <label for="file">Select an image file to upload:</label>'."\n"
  66.             .'                <input type="file" name="file"><br />'."\n"
  67.             .'                <input type="submit" value="Upload">'."\n"
  68.             .'            </div>'."\n"
  69.             .'        </form>'."\n"
  70.             .'        <div>'."\n"
  71.             .'            <p>&copy; 20161209 <a href="http://anggit.com">nggit</a></p>'."\n"
  72.             .'        </div>'."\n"
  73.             .'    </body>'."\n"
  74.             .'</html>';
  75. }


Mohon ganti client_id dengan punya Anda sendiri! Terimakasih :)
Semoga bermanfaat :)

(admin.anggit.com)