I'm using the Imgur API to upload images. They have detailed in their API docs that each request (when I upload an image via their API) also has response headers, which will tell me how much credit the account has left.
I need to return the HTTP response header X-RateLimit-ClientRemaining
. Here is the code I am currently using to get the cURL body back:
$filename = dirname(realpath(__FILE__))."/images/$value";
$client_id = "f*************c";
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$pvars = array('image' => base64_encode($data));
$timeout = 30;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$out = curl_exec($curl);
curl_close ($curl);
$pms = json_decode($out,true);
$url=$pms['data']['link'];
if($url!=""){
// add to success
array_push($success, $url);
}
else {
// add to fail
$p = $value.' failed, error: '.$pms['data']['error'];
array_push($fail, $p);
}
($value
is coming from a loop I have not included)