如何使用 Laravel 从外部 api 响应源下载文件?

我需要使用 Laravel 从 api 响应源下载文件。


一个示例 api - ( http://webservice.test.de/merchants/orders/getOrderInvoice?key=1234567894&format=json&order_no=444555666。


调用此 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如何src使用 Laravel 下载文件?


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"


任何帮助,将不胜感激。


ITMISS
浏览 232回答 3
3回答

守候你守候我

我不完全确定您要做什么,但我建议使用GuzzleHTTP向外部发送请求API。在您的应用程序上使用它很容易安装。composerLaravel正如使用响应中解释的那样,您可以像这样访问您的响应body(在这种情况下contents是.pdf文件):$body = $response->getBody();// Explicitly cast the body to a string$stringBody = (string) $body;然后您可以使用Laravel 文件系统将文件存储在本地存储中,执行如下操作:Storage::disk('local')->put('Invoice_12345-234566.pdf', $stringBody);正如本地驱动程序中所解释的那样。

弑天下

在我的案例中,我使用了一种解决方案。您应该使用 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);

www说

我已经使用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); 或者我们可以使用 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;}
打开App,查看更多内容
随时随地看视频慕课网APP