Search code examples
phpapicurlimgur

Upload an image to imgur with php


I am using php to upload images to imgur this is the code i am using:

    $id = 'clientId';
    $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 ' . $id));
    curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'image' => base64_encode($image)));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);
    $response = json_decode($response);
    curl_close ($ch);

The code is working but i want the images to be upload to my account album if anyone know how to do it, Also i am trying to understand why do i have to set curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); for the code to work, I did visit the manual but if anyone can give me some background about how and why i will be very thankful, Thank you all and have a nice day.


Solution

  • You can send these parameters with your request:

    image   required    A binary file, base64 data, or a URL for an image
    album   optional    The id of the album you want to add the image to. For anonymous albums, {album} should be the deletehash that is returned at creation.
    type    optional    The type of the file that's being sent; file, base64 or URL
    name    optional    The name of the file, this is automatically detected if uploading a file with a POST and multipart / form-data
    title   optional    The title of the image.
    description optional    The description of the image.
    

    So, how about this:

    curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image), 'album' => 1234));