无法在 imgur [API 3] 上上传图片
Cannot upload an image on imgur [API 3]
我正在尝试在 imgur 上上传图片,但我经常遇到问题,无法上传图片。
在我将发布的代码中,我不明白为什么我一直得到布尔值:false
作为 curl_exec($ch);
和 not 的结果json 字符串。从 PHP Manual 来看,这意味着 post 失败了,但我不明白为什么。
在这里,我成功地从 post 请求中读取了图像
$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);
}
虽然这是我尝试上传的
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;
}
而且$this->access_token
对应一个有效访问令牌
when curl_exec returns bool(false), 传输时出错。要获得扩展的错误描述,请使用 curl_errno() 和 curl_error() 函数。要获得更详细的传输信息,请使用 curl_setopt 的 CURLOPT_VERBOSE 和 CURLOPT_STDERR 选项。例如
$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);
应该在异常消息中为您提供 libcurl error code、错误描述和错误发生之前所发生情况的详细日志。
常见问题包括 SSL/TLS encryption/decryption 错误、超时错误和不稳定的连接。
我正在尝试在 imgur 上上传图片,但我经常遇到问题,无法上传图片。
在我将发布的代码中,我不明白为什么我一直得到布尔值:false
作为 curl_exec($ch);
和 not 的结果json 字符串。从 PHP Manual 来看,这意味着 post 失败了,但我不明白为什么。
在这里,我成功地从 post 请求中读取了图像
$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);
}
虽然这是我尝试上传的
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;
}
而且$this->access_token
对应一个有效访问令牌
when curl_exec returns bool(false), 传输时出错。要获得扩展的错误描述,请使用 curl_errno() 和 curl_error() 函数。要获得更详细的传输信息,请使用 curl_setopt 的 CURLOPT_VERBOSE 和 CURLOPT_STDERR 选项。例如
$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);
应该在异常消息中为您提供 libcurl error code、错误描述和错误发生之前所发生情况的详细日志。
常见问题包括 SSL/TLS encryption/decryption 错误、超时错误和不稳定的连接。