I need to download files from an api response source using Laravel.
A sample api - (http://webservice.test.de/merchants/orders/getOrderInvoice?key=1234567894&format=json&order_no=444555666.
After calling this API I will get a JSON response. JSON response is given below.
{
"result": {
"success": "1",
"invoice": {
"src": "webservice.test.de/docs/invoice?secure_key=333444555666777888&key= 1234567894",
"filename": "Invoice_12345-234566.pdf"
}
}
}
When I run this src url (['result']['invoice']['src']) on browser, a pdf file (Invoice_12345-234566.pdf) will download. How can I download file from src using Laravel?
Laravel code
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);
}
Error exception: "Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException"
Any help would be appreciated.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire