Uploading Images Using Imgur API in PHP

Read {count} times since 2020

This tutorial was a request from Anandu.
Imgur provides developers to upload images anonymously using their app. For this you only need to signup for Imgur and get an Application Client ID. In this post I’m going to tell you the step by step process of uploading images using Imgur API in PHP. This is pretty easy. You only have to send a POST request to a file on their site.


<ul class="toc_list">
    <a href="#sign-up"><span class="toc_number toc_depth_1">1</span> Sign Up</a>
    <a href="#create-application-get-client-id"><span class="toc_number toc_depth_1">2</span> Create Application & Get Client ID</a>
    <a href="#html"><span class="toc_number toc_depth_1">3</span> HTML</a>
    <a href="#uploadphp"><span class="toc_number toc_depth_1">4</span> upload.php</a>
    <a href="#fixes"><span class="toc_number toc_depth_1">5</span> Fixes</a>

Sign Up

Go to this page to sign up. It supports Social signup with Google, Facebook, Twitter & Yahoo! or you can sign up via the form.

Create Application & Get Client ID

You need to create an app to get the Client ID. For creating an application go to this page.
Select Anonymous usage without user authorization option as Authorization Type.

Fill up all the other fields including CAPTCHA. Then click submit button.
You will now get a client ID and a client Secret key. We will only need a client ID.


<form action="upload.php" enctype="multipart/form-data" method="POST">
 Choose Image : <input name="img" size="35" type="file"/><br/>
 <input type="submit" name="submit" value="Upload"/>

The above form is just like any other normal file upload form.


  echo "<h2>An Image Please.</h2>";
  $filename = $img['tmp_name'];
  $handle = fopen($filename, "r");
  $data = fread($handle, filesize($filename));
  $pvars   = array('image' => base64_encode($data));
  $timeout = 30;
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
  curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
  $out = curl_exec($curl);
  curl_close ($curl);
  $pms = json_decode($out,true);
   echo "<h2>Uploaded Without Any Problem</h2>";
   echo "<img src='$url'/>";
   echo "<h2>There's a Problem</h2>";
   echo $pms['data']['error'];  


Sometimes, the code won’t work or other problems will arise. In this case, check the following :

  • Is PHP JSON extension installed ?
  • Is PHP cURL extension installed ?

If the above didn’t fix your problem, add this code just after setting the $curl variable in the code :

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

By doing this, we are disabling the SSL Certificate verification which is not good, so I recommend you do this.

Replace the content of $client_id variable with the Client ID value you got. You can get the URL of the image uploaded from $url variable.
If you have any problems / suggestions / feedback contact me through the comments.

Show Comments