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

问题描述 投票:1回答:3

我需要使用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)。如何使用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"

任何帮助将不胜感激。

php laravel laravel-5 laravel-5.8
3个回答
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);

0
投票

我不确定您要做什么,但是我建议使用Guzzle向外部HTTP发送API请求。在install应用程序上使用composer轻松进行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中所述。


0
投票

问题已解决。

我已经使用内置函数file_get_contentsfile_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;
}
© www.soinside.com 2019 - 2024. All rights reserved.