Reputation: 745
I'm trying to upload images on imgur but I often have problem and I am not able to upload the image.
In the code I'll publish I don't understand why I keep getting the boolean value: false
as result of curl_exec($ch);
and not a json string. From the PHP Manual it means that the post failed but I don't understand why.
Here I successfully read the image from a post request
$imageContent = file_get_contents($myFile["tmp_name"][$i]);
if ($imageContent === false) {
// Empty image - I never get this error
} else {
//Image correctly read
$url = $this->uploadLogged($imageContent);
}
While here is my attempt to upload it
public function uploadLogged($image){
$upload_route = "https://api.imgur.com/3/image";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $upload_route);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer '.$this->access_token));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image)));
$response = curl_exec($ch);
$responseDecoded = json_decode($response);
curl_close ($ch);
$link = $responseDecoded->data->link;
if( empty($link) ){
throw new Exception("Cannot upload the image.<br>Response: ".json_encode($response));
}
return $link;
}
Moreover $this->access_token
correspond to a valid access token
Upvotes: 0
Views: 1142
Reputation: 21463
when curl_exec returns bool(false), there was an error during the transfer. to get an extended error description, use the curl_errno() and curl_error() functions. to get even more detailed info of the transfer, use the CURLOPT_VERBOSE and CURLOPT_STDERR options of curl_setopt. eg
$curlstderrh=tmpfile();
curl_setopt_array($ch,array(CURLOPT_VERBOSE=>1,CURLOPT_STDERR=>$curlstderrh));
$response = curl_exec($ch);
$curlstderr=file_get_contents(stream_get_meta_data($curlstderrh)['uri']);
fclose($curlstderrh);
if(false===$response){
throw new \RuntimeException("curl_exec failed: ".curl_errno($ch).": ".curl_error($ch).". verbose log: $curlstderr");
}
unset($curlstderrh,$curlstderr);
should get you both the libcurl error code, an error description, and a detailed log of what happened up until the error, in the exception message.
common issues include an SSL/TLS encryption/decryption error, timeout errors, and an unstable connection.
Upvotes: 1