0

I would like to return a file in my controller that is downloaded from an API (who needs a basic auth). This API returns the file and the problem is I can't use ajax because of Access-Control-Allow-Origin, even when I add the basic auth in the header, it returns this error. And I don't have access to this API so I can't change it the config.

So I found another way! I call this API from my controller with the header and the basic auth. It's working! I can access this API and I don't have the error message: Access-Control-Allow-Origin.

But the problem is the API returns heavy files, and I have this exception OutOfMemoryException. It's normal.

So I would like to returns in my controller just a redirect link to this API with the header and basic auth to download the file. Do you think it's possible?

An example of what I want (But it's not working):

public function indexAction()
{
    // API
    $url = "http://api-who-callback-a-file.com/api/file";

    //Create header 
    $curl = curl_init();
    $url = sprintf("%s?%s", $url, http_build_query(false));

    // Optional Authentication:
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "admin:admin");

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    // Get file (Problem here because the file is to heavy to be downloaded by the server)
    $result = curl_exec($curl);
    curl_close($curl);

    // Return the file
    return $this->file($result);


}

In this example, it's not working because the server download the file and it's too heavy. Me I just want to return the URL with the Header to the client, and he will download the file.

Thank you

Fasco
  • 233
  • 3
  • 18
  • Possible duplicate of [How does Access-Control-Allow-Origin header work?](https://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work) – rkosegi Sep 27 '17 at 10:04
  • " I would like to returns in my controller just a redirect link to this API with the header and basic auth to download the file. Do you think it's possible?" Yes of course. You can return whatever information you like. A URL to the file's download link should be pretty straightforward. Then it's up to the calling client code to follow the link or not. – ADyson Sep 27 '17 at 10:26

0 Answers0