【发布时间】:2016-07-20 10:23:11
【问题描述】:
我有两个 Symfony 应用程序 (API) 通过 HTTP 请求/响应使用 cURL PHP 函数相互通信。当他们收到小的 JSON 响应时,这很好用,但是在获取和提供文件时会出现问题。 API1(暴露在互联网上)需要提供一个只能由 API2(私有,通过 VPN 连接到 API1)访问的文件。
如果我在第一个 API 中对文件的内容进行编码,然后将其传递到响应正文中没有问题,我可以将文件重新转换回流并在第一个 API 中作为 BinaryFileResponse 提供。问题来自大文件 (>30MB),其中响应主体很大,symfony 无法分配那么多内存。
有没有办法将 BinaryFileResponse 从一个 API 转发或重定向到另一个 API,这样中间层对客户端是不可见的?
这是每个应用程序中的两段代码:
公共 API:
/**
*
* @Get("/api/login/getfile")
*/
public function testGetFilePrivate(Request $request)
{
$url = 'private/variantset/9/getfile';
$url = $this->container->getParameter('api2_url').$url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 300000); //Set timeout in ms
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, TRUE);
$fileContent = base64_decode($data['filedata']);
$response = new Response($fileContent);
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$data['filename']
);
$response->headers->set('Content-Disposition', $disposition);
return $response;
}
私有 API:
/**
* @Get("/api/private/variantset/{id}/getfile")
*/
public function getVariantsetDataFileById($id)
{
$variantset = $this->getVariantsetById($id);
if(!$variantset){
$response = array("getdata"=>"ko","error"=>"Variantset does not exists");
return new JsonResponse($response);
}
if($variantset->getCreated()){
$auth_variants_dir = $this->container->getParameter('auth_variants_path');
$file_path = $auth_variants_dir . '/' . $variantset->getDatafile() . '.gpg';
$data = [
"getdata"=>"ok",
"filename" => $variantset->getDatafile() . '.gpg',
"filedata" => base64_encode(file_get_contents($file_path))
];
$response = new Response();
$response->setContent($this->container->get('serializer')->serialize($data, 'json'));
$response->headers->set('Content-Type', 'application/json');
}else{
$response = new JsonResponse(array("getdata"=>"ko","error"=>"Variantset does not have a file yet"));
}
return $response;
}
【问题讨论】:
标签: symfony curl download httprequest api-design