15

I am planning to upload images to imgur anonymously using its api, i registered my application in the anonymous upload category and got client id and client secret, How to use php to upload image to imgur and retrieve direct url to the image? can anyone suggest links to any example? this is what I have tried to do but i get the error "Fatal error: Maximum execution time of 30 seconds exceeded"

<?php

$client_id = :client_id; //put your api key here
$filename = "images/q401x74ua3402.jpg";
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));

//$data is file data
$pvars   = array('image' => base64_encode($data), 'key' => $client_id);
$timeout = 30;
$curl    = curl_init();

curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/upload.json');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
$xmlsimple = new SimpleXMLElement($xml);
echo '<img height="180" src="';
echo $xmlsimple->links->original;
echo '">';

curl_close ($curl);

?>
scottydelta
  • 1,586
  • 3
  • 17
  • 38
  • 1
    Facing same issue, please share if you are able to find a solution. – user434885 Jun 25 '13 at 10:20
  • Have you tried opening the image in `rb` mode? – rr- Jun 26 '13 at 07:57
  • If you run into the maximum execution time limit because it takes to long to upload the image, then you have two options: Either increase the limit on your server if possible, or find another way of upload the image (maybe by just giving image URL and have the service download it from your server itself, if they offer such an option). Btw., a "normal" image upload with a reasonable image size should hardly take 30 seconds if your server has a sufficient connection. – CBroe Jun 26 '13 at 07:59
  • http://api.imgur.com/endpoints/image says "image: A binary file, base64 data, or a URL for an image" - so I would try with an URL if the image you are trying to upload is publicly available via HTTP on your server. (Or try binary instead of base64, because base64 increases the amount of data to upload -> needs more time.) – CBroe Jun 26 '13 at 08:01
  • @CBroe, I have image in base64 only, I cant change that, and yes, its exceeding the maximum timeout – scottydelta Jun 26 '13 at 08:06
  • Well then your only option is to increase the timeout. (Or you write a script that outputs the image [appropriate Content-Type header folloed by the base64-decoded image data] and thereby make that image available via HTTP.) – CBroe Jun 26 '13 at 08:09
  • _"I have image in base64 only"_ - if that's the case, then why are you using `base64_encode` on the image data _again_? – CBroe Jun 26 '13 at 08:10
  • @CBroe, thats for test purpose, The browser sends the base64 image to my script and the script should upload that image to imgur, I even tried uploading images as small as 54 kb and it still endup in timeout, I am not sure if I am using correct keys in post data. – scottydelta Jun 26 '13 at 08:19
  • Well you could still base64-decode that data and send it as binary. Btw. have you tried setting the 'type' parameter as well? Not sure if the API realizes the used type on its own. – CBroe Jun 26 '13 at 08:26
  • I tried binary along with type parameter, I even tried putting a URL in the image parameter but it still gives "Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\path\imgur.php on line 24" – scottydelta Jun 26 '13 at 10:11

3 Answers3

30

Sending the client_id in a post variable is the problem. It needs to be sent in the request header. Also, you're requesting a JSON response, but trying to parse it as XML.

<?php

$client_id = "FILLMEIN";
$image = file_get_contents("img/cool.jpg");

$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)));

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

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

update 1

Live functional example with and source based on this code with debug output.

h0tw1r3
  • 5,957
  • 1
  • 26
  • 33
7

found the error, I need to send authorization details as header, eg code

<?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); 

?>
scottydelta
  • 1,586
  • 3
  • 17
  • 38
  • 11
    I think you mean that you found the error after looking at my code. – h0tw1r3 Jun 27 '13 at 19:12
  • actually the error is nowhere, just that putting headers in double quote and not using string concatenation worked strangely ;) your answer did help me and thanks for that :) – scottydelta Jun 28 '13 at 08:32
  • 2
    @h0tw1r3 lol I appreacite your hard work :) just found this today and it has helped me a lot. the Imgur API documentation is scarce with examples so I was hoping to find a Stack thread just like this to get me started. Kudos. – Jake Jul 27 '13 at 18:33
  • Can someone tell me how do i get link of the direct image from imgur if uploaded ? – Sejoo Dec 18 '19 at 22:19
5

If you have problem with above script, try the curl skip verified SSL like this:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Phu
  • 51
  • 1
  • 1