title: How to download file from external api response source using Laravel?
body: <p>I need to download files from an api response source using Laravel.</p>
  
  <p>A sample api - (<a href="http://webservice.test.de/merchants/orders/getOrderInvoice?key=1234567894&amp;format=json&amp;order_no=444555666" rel="nofollow noreferrer">http://webservice.test.de/merchants/orders/getOrderInvoice?key=1234567894&amp;format=json&amp;order_no=444555666</a>.</p>
  
  <p>After calling this API I will get a JSON response. JSON response is given below.</p>
  
  <pre><code>{
      "result": {
          "success": "1",
          "invoice": {
              "src": "webservice.test.de/docs/invoice?secure_key=333444555666777888&amp;key= 1234567894",
              "filename": "Invoice_12345-234566.pdf"
          }
      }
  }
  </code></pre>
  
  <p>When I run this src url (<code>['result']['invoice']['src']</code>) on browser, a pdf file (<code>Invoice_12345-234566.pdf</code>) will download. How can I download file from <code>src</code> using Laravel? </p>
  
  <p><strong>Laravel code</strong></p>
  
  <pre><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&amp;format=json&amp;order_no=444555666';
  
  $jsonDecodedResults = $this-&gt;curl($getOrderInvoice);
  
  if( $jsonDecodedResults['result']['success'] === '1' ) {
  
       $fileSource = $jsonDecodedResults['result']['invoice']['src'];
       $fileName = $jsonDecodedResults['result']['invoice']['filename'];
       $headers = ['Content-Type: application/pdf'];
       return response()-&gt;download($fileSource, $fileName, $headers);
  
  }
  </code></pre>
  
  <p><strong>Error</strong>
  <code>exception: "Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException"</code></p>
  
  <p>Any help would be appreciated.</p>