I stumbled upon http://api.imgur.com and thought it would be a useful tool to use on my website. Then I noticed that StackOwerflow also uses it, so it should be good))) Although I am afraid when I try to implement it. I took a look at the http://api.imgur.com/examples PHP section, but that didn't help me much.
What interests me is including imgur api on my website so that users can upload their images. I will need to store the img url / path so that I can display it on the website.
eg. have a form that allows users to upload a photo and then save the URL of the downloaded image in a database (VARCHAR).
Someone was successful on this system and could help me understand how to implement it, how StackOwerflow uses it (only store the image URL in a database, not published).
The code I tried:
<form enctype="multipart/form-data" method="post" action="upload_img.php"> Choose your file here: <input name="uploaded_file" type="file"/> <input type="submit" value="Upload It"/> </form>
upload_img.php
<? $filename = "image.jpg"; $handle = fopen($filename, "r"); $data = fread($handle, filesize($filename)); // $data is file data $pvars = array('image' => base64_encode($data), 'key' => IMGUR_API_KEY); $timeout = 30; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'http://api.imgur.com/2/upload.xml'); 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); curl_close ($curl); ?>
source share