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

How to download file from external api response source using Laravel?

我需要使用 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"

如有任何帮助,我们将不胜感激。

我在我的案例中使用了一种解决方案。

您应该使用 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);

我不完全确定您要做什么,但我建议在您的 Laravel 应用程序中使用 Guzzle to send an HTTP request to an external API. It is easy to installcomposer

Using Responses 中所述,您可以像这样访问您的响应 body(在本例中是 .pdf 文件的 contents):

$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 中所述。

问题已解决。

我已经使用 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;
}