0

This is my code so far, I have three different file_get_contents() warnings all for the definition of the $response variable here they are,

  1. SSL operation failed with code
  2. Failed to enable crypto, and
  3. (https://api.imgur.com/3/image): failed to open stream:

I've used the curl requests(commented out), which solved the warnings but, I had to change the definition of $response, which I don't intend to do.

<?php
include_once("connect.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 access code\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 = array_merge($preparams,$params);

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

        }
    }

?>
Bubba
  • 151
  • 1
  • 9
  • What are the exact warnings you are getting? – Cameron Roe Apr 25 '17 at 18:43
  • they're in the description – Bubba Apr 25 '17 at 19:00
  • No, you just have a summary of them. Paste the actual warnings as PHP displays them. – Cameron Roe Apr 25 '17 at 19:01
  • ok, 1.Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in C:\wamp64\www\image.php on line 38 – Bubba Apr 25 '17 at 19:18
  • 2.Warning: file_get_contents(): Failed to enable crypto in C:\wamp64\www\image.php on line 38 – Bubba Apr 25 '17 at 19:19
  • 3.Warning: file_get_contents(https://api.imgur.com/3/image): failed to open stream: operation failed in C:\wamp64\www\image.php on line 38 – Bubba Apr 25 '17 at 19:19
  • Which version of PHP do you have installed? – Cameron Roe Apr 25 '17 at 19:20
  • 7.0.10 and 5.6.25 – Bubba Apr 25 '17 at 19:27
  • You probably need to follow the instructions here: http://stackoverflow.com/questions/26148701/file-get-contents-ssl-operation-failed-with-code-1-and-more/28701786#28701786. I haven't tested this, but you'll probably have to add the `ssl` array element in that answer to your existing `$options` array. You'll also probably need to download the `cacert.pem` file, which you can find more details about here (including a download link): http://stackoverflow.com/questions/24611640/curl-60-ssl-certificate-unable-to-get-local-issuer-certificate/31830614#31830614 – Ian Drake Apr 25 '17 at 19:37
  • how would I nest the array I tried this: $options = array('http'=>array( 'method'=>"POST", 'header'=>"Authorization: Bearer access code\n". "Content-Type: application/x-www-form-urlencoded", 'content'=>$image) 'ssl'=>array( "cafile" => "/path/to/bundle/cacert.pem", "verify_peer"=> true, "verify_peer_name"=> true ) ); but i got a syntax error for adding the element – Bubba Apr 25 '17 at 20:10
  • Looks like you're missing a comma after the first array. Try this `$options = array('http'=>array( 'method'=>"POST", 'header'=>"Authorization: Bearer access code\n". "Content-Type: application/x-www-form-urlencoded", 'content'=>$image), 'ssl'=>array( "cafile" => "/path/to/bundle/cacert.pem", "verify_peer"=> true, "verify_peer_name"=> true ) );` Also make sure you swap out the value of `cafile` with the real path to the `cacert.pem` file. You can download it and put it pretty much anywhere, just make sure you reference the path correctly. For example: `C:\curl\certificates\cacert.pem` – Ian Drake Apr 25 '17 at 20:29
  • ok, now I just have the third warning, Warning: file_get_contents(https://api.imgur.com/3/image): failed to open stream: HTTP request failed! HTTP/1.1 403 Permission Denied – Bubba Apr 25 '17 at 20:53
  • is there a specific place where I have to put the command in the php.ini file? – Bubba Apr 25 '17 at 20:54
  • At this point the original issue is resolved & the HTTP request is working, but you're not authenticating with the Imgur API correctly. You need to read this & implement the OAuth protocol: https://api.imgur.com/oauth2. This means swapping out the `Authorization` header in your `$options` array with the correct access token. Also read this and make sure your POST body has the correct structure: https://api.imgur.com/endpoints/image. Also, you need to use `\r\n` instead of `\n` as the line ending character of the header line. This is probably beyond the scope of Stack Overflow comments now... – Ian Drake Apr 25 '17 at 21:17
  • what do you mean exactly by the correct structure of POST body? – Bubba Apr 25 '17 at 22:35
  • Right now you're putting the base64 encoded image data directly in the `content` element of your `$options` array (as the POST request body). But based on the info [here](https://api.imgur.com/endpoints/image#image-upload), it looks like you actually need to use JSON format, and the encoded image goes in the `image` JSON key. But this is just a guess; I'm not very familiar with the Imgur API. I still think your main issue is the [OAuth described here](https://api.imgur.com/oauth2), based on the 403 response code you're seeing. – Ian Drake Apr 26 '17 at 14:52

1 Answers1

1

The extension php_openssl must be enabled on your server.

Call phpinfo() and check the section openssl is present and OpenSSL support is set to enabled.

Additionaly, verify that you have installed the common certificates on your server. If your are on linux and Debian/Ubuntu, you can do apt-get install ca-certificates.