3 回答

TA贡献1802条经验 获得超10个赞
我不完全确定您要做什么,但我建议使用GuzzleHTTP向外部发送请求API。在您的应用程序上使用它很容易安装。composerLaravel
正如使用响应中解释的那样,您可以像这样访问您的响应body(在这种情况下contents是.pdf文件):
$body = $response->getBody();
// Explicitly cast the body to a string
$stringBody = (string) $body;
然后您可以使用Laravel 文件系统将文件存储在本地存储中,执行如下操作:
Storage::disk('local')->put('Invoice_12345-234566.pdf', $stringBody);
正如本地驱动程序中所解释的那样。

TA贡献1818条经验 获得超8个赞
在我的案例中,我使用了一种解决方案。
您应该使用 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);

TA贡献1775条经验 获得超8个赞
我已经使用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;
}
- 3 回答
- 0 关注
- 231 浏览
添加回答
举报