Search code examples
phpxamppimgur

Xampp php permission denied when trying to post images using imgur api?


The error I get:

Warning: file_get_contents(https://api.imgur.com/3/image): failed to open stream: HTTP request failed! HTTP/1.1 403 Permission Denied in C:\xampp\htdocs\sn0\classes\Image.php on line 22

Notice: Trying to get property of non-object in C:\xampp\htdocs\sn0\classes\Image.php on line 25

Notice: Trying to get property of non-object in C:\xampp\htdocs\sn0\classes\Image.php on line 25

Here's my Image.php file:

<?php
class Image {

        public static function uploadImage($formname, $query, $params) {
                $image = base64_encode(file_get_contents($_FILES[$formname]['tmp_name']));

                $options = array('http'=>array(
                        'method'=>"POST",
                        'header'=>"Authorization: Bearer ###\n".
                        "Content-Type: application/x-www-form-urlencoded",
                        'content'=>$image
                ));

                $context = stream_context_create($options);

                $imgurURL = "https://api.imgur.com/3/image";

                if ($_FILES[$formname]['size'] > 10240000) {
                        die('Image too big, must be 10MB or less!');
                }

                $response = file_get_contents($imgurURL, false, $context);
                $response = json_decode($response);

                $preparams = array($formname=>$response->data->link);

                $params = $preparams + $params;

                DB::query($query, $params);

        }

}
?>

Solution

  • You making bad request.

    At the beginning you need to generate your Client ID (more info @ https://api.imgur.com/#registerapp)

    To do this go to https://api.imgur.com/oauth2/addclient and Select Anonymous usage without user authorization option as Authorization Type.

    Use Authorization: Client-ID not Bearer, you can keep file_get_contents (so - then you need change only authorization header) but CURL will be better for this.

    Example with CURL:

    <?php
    class Image
    {
        public static function uploadImage($formname, $query, $params)
        {
            $client_id = 'YOUR CLIENT ID';
            $image = base64_encode(file_get_contents($_FILES[$formname]['tmp_name']));
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => 'https://api.imgur.com/3/image.json',
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTPHEADER => array(
                    'Authorization: Client-ID ' . $client_id
                ) ,
                CURLOPT_POST => TRUE,
                CURLOPT_RETURNTRANSFER => TRUE,
                CURLOPT_POSTFIELDS => array(
                    'image' => $image
                )
            ));
            $out = curl_exec($curl);
            curl_close($curl);
            $response = json_decode($out);
            $preparams = array(
                $formname => $response->data->link
            );
            $params = $preparams + $params;
            DB::query($query, $params);
        }
    }