我使用php curl
向地理服务器发送REST请求并获得二进制响应。
$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);
服务器将png图像作为二进制数据发送给我,并将其保存在$response
变量中
现在,上面代码中的$response
是png图像的二进制数据。如何将其发送到客户端而不将其保存为文件?
另一个问题是,$response
的数据类型是什么?
答案 0 :(得分:-1)
您可以将二进制数据保存为文件,然后将其发送到客户端:
// 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();
或者您可以使用echo
:
echo $response;