Skip to content

Instantly share code, notes, and snippets.

@scottydelta
Created December 15, 2013 11:37
Show Gist options
  • Save scottydelta/7971955 to your computer and use it in GitHub Desktop.
Save scottydelta/7971955 to your computer and use it in GitHub Desktop.
A php script to upload images to Imgur anonymously using Imguy API V3. The input is an image and Output is a direct ink.
<?php
$client_id = 'xxxxxxxx';
$file = file_get_contents("test-image.png");
$url = 'https://api.imgur.com/3/image.json';
$headers = array("Authorization: Client-ID $client_id");
$pvars = array('image' => base64_encode($file));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL=> $url,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $pvars
));
$json_returned = curl_exec($curl); // blank response
echo "Result: " . $json_returned ;
curl_close ($curl);
?>
@hamada147
Copy link

it always return a blank response
why???
and i get an error Trying to get property of non-object
which means that the curl_exec() failed to run
so any ideas what's going wrong??

Edit 1:
So i manged to make it work through this line of code

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

which over look the SSL verification is there is any other way

Edit 2:
the php code

if (empty($_POST['clientid']) || $_FILES['upload']['error'] !== 0 || $_FILES['upload']['size'] > 5000000000) {
    exit;
}

$client_id = $_POST['clientid'];

$filetype = explode('/',mime_content_type($_FILES['upload']['tmp_name']));
if ($filetype[0] !== 'image') {
    die('Invalid image type');
}

$image = file_get_contents($_FILES['upload']['tmp_name']);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
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_POSTFIELDS, array( 'image' => base64_encode($image) ));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$reply = curl_exec($ch);

curl_close($ch);

$reply = json_decode($reply);

echo "<h3>Image</h3>";
printf('<img height="180" src="%s" >', $reply->data->link);

echo "<h3>API Debug</h3><pre>";
var_dump($reply);

the html code

<html>
  <h3>Form</h3>
  <form method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="50000" />
    Image (< 50kb): <input type="file" name="upload" /><br/>
    ClientID: <input type="text" name="clientid" /><br/>
    <input type="submit" value="Upload to Imgur" />
  </form>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment