沃梦达 / 编程问答 / php问题 / 正文

如何从在 Laravel 中重定向的 url 下载文件?

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

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

问题描述

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

Gmail 返回这样的链接:

https://mail.google.com/mail/u/0?ui=2&ik=d3d2b70b46&attid=0.2&permmsgid=msg-f:1651797278805284123&th=16ec5cdf9c&view=fimg;sz=s0-l75-ft&attbid=ANGjdJ9_MfHq-YioubDagNZ5h216toG9gPg11wV4SkG0Q73urVHcSFnjGKdM4b6b7BfEir0xUp3f4A2ULo9A2srvL7svlpHpZ3RampLdPhlpHpHpZ3RampLvLvlpHpLz&L&L&L

像这样重定向到动态 URL:

<预> <代码> https://gm1.ggpht.com/puicw0cCLZ4huu-Pwk3VHJ8R-wdB2bIHYi4v4F8yUxSnt0UpQQ5LArR9szM5JIqNHPo7JBUbFIf0nsh4JKakewl91Zl08pnNcxmMah6tAFNFWOFaiVJDhxA4RGHvyFxP90HR2DLKHL1MAz8TC9zY8qEliO3vxCv-KqNfq0WBvkFY2fb2GvAWtSTNhksmSHV8_pAKVRyT5aGnbmlH1SWRk_HmTfH27aoslF1P_Lc0245h2nwuhBQ6WpoMgE8eij_Z0QWrHqJWHB0xhAIFAdtIb5E

我使用这个file_get_contents($link)失败了,因为我无法获得图片的原始链接

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

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

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

解决方案

我们需要启用重定向.

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

默认情况下,根据 docs 文档,重定向是已启用,但我出于教育目的在上面明确启用了它.

I'm having a problem with downloading an inline image from summernote when the image is copied from 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

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

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

Note: These are just dummy URLs.

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);

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

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

本文标题为:如何从在 Laravel 中重定向的 url 下载文件?

基础教程推荐