Php 无法在imgur[API 3]上上载图像

Php 无法在imgur[API 3]上上载图像,php,curl,image-uploading,imgur,Php,Curl,Image Uploading,Imgur,我正在尝试在imgur上上传图像,但我经常遇到问题,无法上传图像 在我将要发布的代码中,我不明白为什么我一直得到布尔值:false作为curl\u exec($ch)的结果和不是json字符串。从表面上看,这意味着帖子失败了,但我不明白为什么 在这里,我成功地读取了post请求中的图像 $imageContent = file_get_contents($myFile["tmp_name"][$i]); if ($imageContent === false) { // Empty im

我正在尝试在imgur上上传图像,但我经常遇到问题,无法上传图像

在我将要发布的代码中,我不明白为什么我一直得到布尔值:
false
作为
curl\u exec($ch)的结果
不是json字符串。从表面上看,这意味着帖子失败了,但我不明白为什么

在这里,我成功地读取了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;
}
public函数上传记录($image){
$upload_路线=”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,数组('Authorization:Bearer'.$this->access_token));
curl_setopt($ch,CURLOPT_POSTFIELDS,array('image'=>base64_encode($image));
$response=curl\u exec($ch);
$responseDecoded=json_decode($response);
卷曲关闭($ch);
$link=$responseDecoded->data->link;
if(空($link)){
抛出新异常(“无法上载图像。
响应:.json_encode($Response)); } 返回$link; }

此外,
$this->access\u令牌
对应于一个有效的访问令牌

当curl\u exec返回bool(false)时,传输过程中出现错误。要获得扩展的错误描述,请使用curl\u errno()和curl\u 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);
在异常消息中,您应该同时获得异常消息、错误描述和错误发生前的详细日志


常见问题包括SSL/TLS加密/解密错误、超时错误和连接不稳定。

您的代码生成一个json字符串作为来自imgur的响应(尽管是身份验证失败的消息,因为我没有访问令牌)。你从哪里得到“false”?@不,作为imgur的响应,我得到的是布尔值false,而不是json字符串。从php手册来看,这意味着curl失败了,但我不明白为什么:不是在我的测试床上:我剪切并粘贴了你的代码,然后打印出$result字符串。同时检查答案如下图所示,从下面的答案来看,图像没有正确读取。如何将映像传递给函数?这与问题无关:就http而言,curl函数的设置是正确的,这确实是您的问题。由于您的代码,我得到了以下信息:
[错误代码:52]-来自服务器的空回复
是否有可能解决或是imgur的问题?@Timmy你能发布CURLOPT_详细日志吗?(Un)幸运的是,今天的工作似乎很好,所以很可能是服务器的问题。感谢您的帮助我再次获得了错误,这是我获得的:
错误:curl\u exec失败:52:服务器的空回复。详细日志:
日志似乎是empty@Timmy嗯,不要使用stream\u get\u contents,试试
$curlstderr=file\u get\u contents(stream\u get\u meta\u data($curlstderh)['uri']),还是空的吗?