如何从在Laravel中重定向的URL下载文件? [英] How to download file from url that redirects in Laravel?

查看:111
本文介绍了如何从在Laravel中重定向的URL下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从summernote下载嵌入式图像时遇到问题 从GMAIL复制图像时.

I'm having a problem with downloading an inline image from summernote when the image is copied from GMAIL.

Gmail返回的链接如下:

The Gmail returns link like this:

https://mail.google.com/mail/u/0?ui=2&ik=d3d2b70b46&attid=0.2&permmsgid=msg-f:1651797278805284123&th=16ec5cdf9c25551b&view=fimg&sz=s0-l75-ft&attbid=ANGjdJ9_MfHq-YioubDagNZ5h216toG9gPg11wV4SkG0Q73urVHcSFnjGKdM4b6b7BfEir0xUp3f4A2ULo9A2srvLz4hlMdP727bJYHpSyL92kesSl3W74lRZFs9HBU&disp=emb&rv71

将这样重定向到动态网址:

Which redirects to dynamic URL like this:

https://gm1.ggpht.com/puicw0cCLZ4huu-Pwk3VHJ8R-wdB2bIHYi4v4F8yUxSnt0UpQQ5LArR9szM5JIqNHPo7JBUbFIf0nsh4JKakewl91Zl08pnNcxmMah6tAFNFWOFaiVJDhxA4RGHvyFxP90HR2DLKHL1MAz8TC9zY8qEliO3vxCv-KqNfq0WBvkFY2fb2GvAWtSTNhksmSHV8_pAKVRyT5aGnbmlH1SWRk_HmTfH27aoslF1P_Lc0245h2nwuhBQ6WpoMgE8eij_Z0QWrHqJWHB0xhAIFAdtIb5E

我无法使用此file_get_contents($link),因为我无法获得图像的原始链接

I failed using this file_get_contents($link) because I can't get the original link of the image

如何在Laravel控制器中获取图像的原始链接?

How can I get the original link of the image in the Laravel Controller?

注意: 这些只是虚拟网址.

我的问题与此类似:如何从url下载文件重定向?但是我似乎找不到它的Laravel版本.

My question is similar to this: How to download file from url that redirects? But I can't seem to find a Laravel version of that.

推荐答案

我们需要启用重定向.

We need to enable redirection.

$opts = array('http' =>
    array(
        'follow_location' => 1,
    )
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/', false, $context);

默认情况下,根据 docs 文档,重定向为启用,但是我出于教学目的在上面明确启用了该功能.

By default, according to the docs docs, redirection is enabled, however I explicitly enabled it above for educational purposes.

这篇关于如何从在Laravel中重定向的URL下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆