如何在浏览器上显示来自API的原始png响应?

问题描述

我尝试使用Curl在浏览器上显示来自api的原始png图像,以下是我尝试过的内容;但是我收到的是黑色图像,中间有一个小方块。但是,当我在邮递员上使用相同的URL时,imgage会正确显示。请协助,仔细阅读我的代码,看看我在做什么错。另外,我尝试检查了Raw response of a image/png resonpse,但没有给出相同的结果,我尝试搜索其他对象,但没有给出太多建议。


$curl = curl_init();

curl_setopt_array($curl,array(
    CURLOPT_URL => "https://url",CURLOPT_RETURNTRANSFER => true,CURLOPT_ENCODING => "",CURLOPT_MAXREDirs => 10,CURLOPT_TIMEOUT => 0,CURLOPT_FOLLOWLOCATION => true,CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,CURLOPT_CUSTomrEQUEST => "GET",CURLOPT_HTTPHEADER => array(
        "Accept: application/json","Authorization: Bearer xxxxxxxxxx"
    ),));

$response= curl_exec($curl);

curl_close($curl);


$size = filesize($response);
header("Content-length: $size"); 
header("Content-Type: image/png"); 
//header('Content-disposition: attachment; filename ="image.png"');
header('Content-disposition: inline; filename="image.png"');
$png= file_put_contents("image.png",$response);


echo $response; gives this     ����JFIF���� ICC_PROFILE mntrRGB XYZ acspAPPL���- desc�|cprtx(wtpt�bkpt�rXYZ�gXYZ�bXYZ�rTRCgTRCbTRCdesc"Artifex

在var_dump($ response);我收到这种类型的字符串

string(64086) "�PNG  IHDR� ����,bKGD���̿ pHYs��U[��tIME�2�b��IDATx���-��L�������J��JNYղ���:V�uDVY���`떗^�Һ�`�����Bv�;���#�kO2��p�0/��O���.ψ�`,...

我在此链接上有原始数据:https://pastebin.com/Q5rNu6dX

string(198663)“ .PNG IHDRm��1Џ&iCCPiccH��gP�Y���

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)