I use php curl
to send a REST request to geoserver and get a binary response.
$curl = curl_init();
$url = 'http://localhost:8080/geoserver/worksp/wms';
$query_str = "service=WMS&LAYERS=" . $_GET['LAYERS'] . "&TRANSPARENT=" . $_GET['TRANSPARENT'] . "&VERSION=" . $_GET['VERSION'] . "&REQUEST=" . $_GET['REQUEST'] . "&STYLES=" . $_GET['STYLES'] ;
$query = $url . '?' . $query_str;
//var_dump($query);
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $query,
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_BINARYTRANSFER => true,
CURLOPT_HEADER => false
));
set_time_limit(30); // set time in secods for PHP
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_USERPWD, "admin:geoserver");
$response = curl_exec($curl);
curl_close($curl);
the server sends me a png image as a binary data an I save it in the $response
variable.
Now, $response
in the above code is a binary data of a png image. how can I send it to client without saving it as a file?
Another question is that, what is the data type of $response
?
You can save the binary data as a file and then send it to the client:
// place the above code here
$saveTo = "c:\\temp.png";
if (file_exists($saveTo)) {
unlink($saveTo);
}
$fp = fopen($saveTo, 'wb');
fwrite($fp, $resp);
fclose($fp);
header("Content-type:image/png");
$fp = fopen($saveTo, 'rb');
fpassthru($fp);
exit();
or you can use echo
:
echo $response;