using imgur api v3 to upload images anonymously using php
Asked Answered
A

3

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

?>
Almetaalmighty answered 24/6, 2013 at 6:44 Comment(10)
Facing same issue, please share if you are able to find a solution.Roldan
Have you tried opening the image in rb mode?Middlesworth
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.Biblioclast
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.)Biblioclast
@CBroe, I have image in base64 only, I cant change that, and yes, its exceeding the maximum timeoutAlmetaalmighty
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.)Biblioclast
"I have image in base64 only" - if that's the case, then why are you using base64_encode on the image data again?Biblioclast
@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.Almetaalmighty
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.Biblioclast
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"Almetaalmighty
A
8

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

?>
Almetaalmighty answered 27/6, 2013 at 11:58 Comment(4)
I think you mean that you found the error after looking at my code.Dale
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 :)Almetaalmighty
@Dale 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.Rosalie
Can someone tell me how do i get link of the direct image from imgur if uploaded ?Uhhuh
D
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.

Dale answered 26/6, 2013 at 20:43 Comment(4)
Its not going in to timeout state but I am getting error which says "Trying to get property of non-object in C:\xampp\htdocs\path\imgur.php on line 20" and line 20 is the printf line.Almetaalmighty
Add error_reporting(E_ALL); to the top of your script. The curl request is failing for some reason.Dale
Check update 1 links for a live example. Looks like something is wrong with your local environment.Dale
Links down. UpdateThisbe
A
8

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

?>
Almetaalmighty answered 27/6, 2013 at 11:58 Comment(4)
I think you mean that you found the error after looking at my code.Dale
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 :)Almetaalmighty
@Dale 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.Rosalie
Can someone tell me how do i get link of the direct image from imgur if uploaded ?Uhhuh
M
5

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

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Maggoty answered 9/2, 2014 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.