我需要使用Laravel从api响应源下载文件。
调用此API后,我将获得JSON响应。 JSON响应如下。
{
"result": {
"success": "1",
"invoice": {
"src": "webservice.test.de/docs/invoice?secure_key=333444555666777888&key= 1234567894",
"filename": "Invoice_12345-234566.pdf"
}
}
}
当我在浏览器中运行此src URL(['result']['invoice']['src']
)时,将下载pdf文件(Invoice_12345-234566.pdf
)。如何使用Laravel从src
下载文件?
Laravel代码
public function curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
$jsonDecodedResults = json_decode($result, true);
return $jsonDecodedResults;
}
$getOrderInvoice = 'http://webservice.test.de/merchants/orders/getOrderInvoice?key=1234567894&format=json&order_no=444555666';
$jsonDecodedResults = $this->curl($getOrderInvoice);
if( $jsonDecodedResults['result']['success'] === '1' ) {
$fileSource = $jsonDecodedResults['result']['invoice']['src'];
$fileName = $jsonDecodedResults['result']['invoice']['filename'];
$headers = ['Content-Type: application/pdf'];
return response()->download($fileSource, $fileName, $headers);
}
错误
exception: "Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException"
任何帮助将不胜感激。
答案 0 :(得分:0)
我正在使用一种解决方案。
您应该使用copy()函数下载外部图像,然后在响应中将其发送给用户:
$fileSource = $jsonDecodedResults['result']['invoice']['src'];
$headers = ['Content-Type: application/pdf'];
$tempFile = tempnam(sys_get_temp_dir(), $fileSource);
copy(**YOUR_TEMP_Directory**, $tempFile);
return response()->download($tempFile, $fileName, $headers);
答案 1 :(得分:0)
我不确定您要做什么,但是我建议使用Guzzle向外部HTTP
发送API
请求。在您的composer
应用程序上install很容易使用Laravel
。
如Using Responses中所述,您可以像下面这样访问您的回复body
(在本例中为contents
文件的.pdf
):
$body = $response->getBody();
// Explicitly cast the body to a string
$stringBody = (string) $body;
然后您可以使用Laravel File System将文件存储在本地存储中,如下所示:
Storage::disk('local')->put('Invoice_12345-234566.pdf', $stringBody);
如The Local Driver中所述。
答案 2 :(得分:0)
问题已解决。
我已经使用内置函数file_get_contents
和file_put_contents
从api资源获取和存储内容。功能现在运行良好。
// URL src and Filename from API response
$fileSource = $jsonDecodedResults['result']['invoice']['src'];
$fileName = $jsonDecodedResults['result']['invoice']['filename'];
$headers = ['Content-Type: application/pdf'];
$pathToFile = storage_path('app/'.$fileName);
$getContent = file_get_contents($fileSource); // Here cURL can be use.
file_put_contents( $pathToFile, $getContent );
return response()->download($pathToFile, $fileName, $headers);
OR
我们可以使用curl $getContent = $this->curl($fileSource);
代替$getContent = file_get_contents($fileSource);
public function curl($url)
{
//create a new cURL resource
$ch = curl_init();
// TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// set url and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab url and pass it to the browser
$result = curl_exec($ch);
// close cURL resouces, and free up system resources
curl_close($ch);
$jsonDecodedResults = json_decode($result, true);
return $jsonDecodedResults;
}