Convert binary data to image cURL GET request

I am trying to get QRCode as a response using Google api that returns in image/png format

Below is my code

    // Get CURL resource
    $curl = curl_init();
   
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_HTTPHEADER =>array('Content-type: image/png'),
        CURLOPT_URL => $response,//$response will have url
    ));
    
   // Send the request & save response to $resp
    $resp = curl_exec($curl);
    echo "$resp"

   // Close request to clear up some resources
    curl_close($curl);
}

but i’m getting below response.

    ���c�bKGD��������IDATx���͎�0@�P���r��


���mf�AWV?����~�"�u��Q%䕐WB^ y%䕐WB^ y%䕐WB^ y%䕐WB^ y%䕐WB^ y?�_�m���|��f�ݟ1��9��
y%䕐WBބqfg���c�����eG�oZ���J�+!o�8�s�?�)�x�k$����V!����J�[>άs�ٙ�k�J�+!��<x�95�JO7�B^ y%䕐�|�Y7)�\����i�O��WB^ y%��g.{,hg��IwчZ���J�+!o{ڹ�Y�v��:�B^ y%䕐7a��x��KB��|ꙩ�O�h�J�+!�����;s�hp�O}:� ��M��WB^ y%�-��42��x������J�+!�����{̺�/����B^ y%䕐��b�� �u�0}�sۭB^ y%䕐a?ٴ��ԩwd2�ޙ�^%�%䕐�=;j��ݑevo�_vZ���J�+!�Q{�ʱ��p�sۭB^ y%䕐a�=q�������u�B^ y%䕐aoA9Ѻ�Ǐ/TM�*䕐WB^ y�3Μ:�r�кsFS� y%䕐WB����]�nߠ�U�+!�������׽�w�{?�*䕐WB^ y�{g�G��WB^ y%䕐WB^ y%䕐WB^ y%䕐WB^ y%䕐WB^ y%䕐WB�o�p% f_�IEND�B����c�bKGD��������IDATx���͎�0@�P���r��

���mf�AWV?����~�"�u��Q%䕐WB^ y%䕐WB^ y%䕐WB^ y%䕐WB^ y%䕐WB^ y?�_�m���|��f�ݟ1��9��
y%䕐WBބqfg���c�����eG�oZ���J�+!o�8�s�?�)�x�k$����V!����J�[>άs�ٙ�k�J�+!��<x�95�JO7�B^ y%䕐�|�Y7)�\����i�O��WB^ y%��g.{,hg��IwчZ���J�+!o{ڹ�Y�v��:�B^ y%䕐7a��x��KB��|ꙩ�O�h�J�+!�����;s�hp�O}:� ��M��WB^ y%�-��42��x������J�+!�����{̺�/����B^ y%䕐��b�� �u�0}�sۭB^ y%䕐a?ٴ��ԩwd2�ޙ�^%�%䕐�=;j��ݑevo�_vZ���J�+!�Q{�ʱ��p�sۭB^ y%䕐a�=q�������u�B^ y%䕐aoA9Ѻ�Ǐ/TM�*䕐WB^ y�3Μ:�r�кsFS� y%䕐WB����]�nߠ�U�+!�������׽�w�{?�*䕐WB^ y�{g�G��WB^ y%䕐WB^ y%䕐WB^ y%䕐WB^ y%䕐WB^ y%䕐WB�o�p% f_�IEND�B

What is the error…??

Except for the PNG-header, this may be an image. Just store it on your machine and try an image viewer like irfanview.

@chorn…i have created it as an api…this will be sent as an response …I cannot save in my local machine

save whatever response you get in a file and download it to your local machine. this looks like a PNG file from google charts api.

yes…its sending in img/png format

and what’s the problem then? you requested a PNG, you get a PNG.

instead of… ?

its response itself in png format…i need to display it that’s it.

where? save it as a file, then send it to whatever client you have, or make a redirect on that file, or output the string with the appropriate header

http://php.net/header

k…Thank you @chrorn

@chorn…i tried using file but its giving me response like this…

just clicl this link to see response image

https://imgur.com/a/kWxrE

below is my code…

    $curl = curl_init();
    // Set some options - we are passing in a useragent too here
    //header('Content-type: : image/png');

    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_HTTPHEADER =>array('Content-type: image/png'),
        CURLOPT_URL => $response,
        // CURLOPT_USERAGENT => 'Codular Sample cURL Request'
    ));
    // Send the request & save response to $resp
    $resp = curl_exec($curl);
    // Close request to clear up some resources
    curl_close($curl);


    header ("Content-Type: image/png");
    readfile($resp);

As $resp is not a file, you can’t use readfile(). It’s a string, so use echo. You can var_dump() any variable if you are unsure what it contains.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.