How to implement imgur api (host image) on a website?

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); ?> 
+4
source share
1 answer

I see that you have a copy and an example is inserted from the imgur API site, which shows an example of the file name that is contained in $filename . You need to specify this variable in the file that PHP downloaded.

Change the $filename part of your script:

 $filename = $_FILES['uploaded_file']['tmp_name']; 

Source: POST Method File Download

+4
source

Source: https://habr.com/ru/post/1384174/


All Articles