0

This is different from the linked duplicate question because the other question does not explain how to display the body as an image instead of the encoded text.

I have a GET API call that is returning back a UTF-8 PNG image. I am making the call from PHP and want to display the PNG thumbnail embedded in the webpage. Can someone help me figure out how to display the image inline with the rest of the webpage? So far I am just taking the CURL response and echoing it out to the page. Here is what I see. Thank you in advance.

$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'$url');
curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
curl_setopt($curl,CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
$headers = array( 'X-Tableau-Auth: ' . $GLOBALS['authToken'] );
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$resp = curl_exec($curl);
echo $resp;
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($resp, 0, $header_size);
$body = substr($resp, $header_size);
echo $body;
curl_close($curl);

enter image description here

anton2g
  • 923
  • 6
  • 12
  • 29
  • 1
    please post your current code – cmorrissey May 28 '15 at 17:34
  • $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,'$url'); curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17'); curl_setopt($curl,CURLOPT_HEADER, true); $headers = array( 'X-Tableau-Auth: ' . $GLOBALS['authToken'] ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $resp = curl_exec($curl); echo $resp; curl_close($curl); – anton2g May 28 '15 at 18:09
  • 1
    possible duplicate of [PHP cURL retrieving response headers AND body in a single request?](http://stackoverflow.com/questions/9183178/php-curl-retrieving-response-headers-and-body-in-a-single-request) – cmorrissey May 28 '15 at 20:35
  • Thanks cmorrissey but this did not fix the issue. It helped me separate the body from the header, but I still have encoded junk instead of an image. – anton2g May 28 '15 at 20:48
  • This is not a duplicate and I now do not know how to get the banner removed so that someone can actually help answer my question. – anton2g May 28 '15 at 21:03
  • [`header('Content-Type: image/png');`](http://php.net/manual/en/function.header.php) – Sam May 28 '15 at 21:31
  • Thanks Sam, but that didn't seem to fix it. I still don't have an image, just encoded text and now all the rest of my webpage just shows up in raw HTML – anton2g May 28 '15 at 21:33
  • Oh, you're returning a whole page not just the PNG contents. You'll need to save the file with [`file_put_contents()`](http://php.net/manual/en/function.file-put-contents.php) (or [`fopen()`](http://php.net/manual/en/function.fopen.php)) and then echo an HTML tag: `` – Sam May 28 '15 at 22:35
  • 1
    Someone else answered it an a separate question I made: echo ''; – anton2g May 28 '15 at 22:48

0 Answers0