【问题标题】:How to download file from external api response source using Laravel?如何使用 Laravel 从外部 api 响应源下载文件?
【发布时间】:2019-12-12 07:21:09
【问题描述】:

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

任何帮助将不胜感激。

【问题讨论】:

  • 您提供的示例网址无效,如果它只是一个虚拟的那么我很糟糕,否则您不需要运行CURL,您可以通过file_get_contens()函数获取文件。然后将文件保存在本地并将其作为下载响应。
  • @BlackXero。给定的网址是虚拟的。
  • 您是否使用诸如 TCPDF 之类的包创建文件?如果是这样,我有一个建议,例如,将文件保存到服务器目录并从保存的目录下载(对于从目录路径下载,有几种方法可以使用 php)谢谢 :)
  • @Ajith。不使用任何软件包。文件源是从外部 api 获取的。

标签: php laravel laravel-5 laravel-5.8


【解决方案1】:

我不完全确定您要做什么,但我建议使用 Guzzle 将 HTTP 请求发送到外部 API。在您的Laravel 应用程序上使用composer 很容易install。

正如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 中所解释的那样。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      问题已解决。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-05
        • 1970-01-01
        • 1970-01-01
        • 2020-08-27
        • 2020-05-22
        • 1970-01-01
        • 2016-12-12
        • 2021-08-19
        相关资源
        最近更新 更多