Question

Here is my code - why would I be getting a blank response?

<?php

$client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$client_id = 'xxxxxxxxxxxxxxx';

$file = file_get_contents("test-image.png");

$url = 'https://api.imgur.com/3/image.json';
$headers = array('Authorization: Client-ID $client_id');
$pvars  = array('image' => base64_encode($file));

$curl = curl_init();

curl_setopt_array($curl, array(
   CURLOPT_URL=> $url,
   CURLOPT_TIMEOUT => 30,
   CURLOPT_POST => 1,
   CURLOPT_RETURNTRANSFER => 1,
   CURLOPT_HTTPHEADER => $headers,
   CURLOPT_POSTFIELDS => $pvars
));

$json_returned = curl_exec($curl); // blank response
echo "Result: " . $json_returned ;

curl_close ($curl); 

?>

Edit:

Some var_dump results:

var_dump(base64_encode($file)):
    string iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAQAAAD2e2DtAADS7ElEQVR4Xu39B7SlaVbehz9fPDndnEPl0F2dw8z05CEjGIQAC2ELhABhIbAlZMmyhOEvB0mWg2zZQsaykJElkdMwwOTUM527qit0xVs353NPjl/6r/Vb77prCcYwDNM9A5pzV9UNJ33nffe7w7OfvbeV6A++xeZfwr+UM4hs85unP/m3r97+EAGIlcjip4SffSVK+LurPw23r97cP+z820YAJFsJXzY//8m+/TUnsEciWeeS9+oF/QPJ+qvJC3pS56wXk5+3Fm1XG/FsUnJ837LDKIhdO52Sx4GJO4OZ6L5txYtBEF7Udf1k/CdeA/zpv/0lu+THtu046SStiqZUiRK1kqS1Fu62c9NzltXYq3V7g6LizPhYuuSN24/GW14qmkliHaiehNbJeCGeSBwNrY6G1lF80719b/U3N2U/kXmxrVgak5KD+KsC8GXe6HXNas9KKSfpyC44l+yB'...
(length=72080)

var_dump($json_returned):
    boolean false

Edit 2:

print_r(curl_getinfo($curl)):
    '( [url] => https://api.imgur.com/3/image.json [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.296 [namelookup_time] => 0.047 [connect_time] => 0.312 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => 54.243.128.92 [primary_port] => 443 [local_ip] => 192.168.1.69 [local_port] => 4923 [redirect_url] => )'
Was it helpful?

Solution 2

I've solved it:

'Authorization: Client-ID $client_id' should have been
"Authorization: Client-ID $client_id"

Basics, to use single quotes I would have to concatenate.

OTHER TIPS

You need to set a complete header like this:

$post_header  = array("POST /3/image.json HTTP/1.1",
"Host: api.imgur.com",
"User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1",
"Accept-Language: en-us,en;q=0.5",
"Accept-Encoding: gzip, deflate",
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Connection: keep-alive",
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
);

Then put this header in the opt array CURLOPT_HTTPHEADER => $post_header

This is how CURL POST will work.

.

also, you need to use http_build_query() to build postdata to be send to CURLOPT_POSTFIELDS, and not array directly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top