在Laravel中如何重定向到外部URL并返回?

69

我正在尝试使用SMS INDIA HUB API向用户发送一次性密码。为此,我需要重定向到以下URL格式:

http://cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=abc&password=xyz&msisdn=919898xxxxxx&sid=SenderId&msg=test%20message&fl=0&gwid=2

如果我们加载此URL,它将返回一些消息。 我也需要获取那条信息。

我尝试了这样:

$url = "http://cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=wwww&password=eee&msisdn=9197xxxxx&sid=yyyyy&msg=rrrrr&fl=0&gwid=2";

return Redirect::intended($url);

但它并没有指向那个链接,它试图在本地主机中加载该URL。

还是有使用SMS INDIA HUB发送短信的插件吗?

有人能帮忙吗?

9个回答

87

1
我想通了。你必须“return”重定向。否则它不会起作用。 - tmarois
另外,我不得不剥离掉协议中的“http”或“https”,并添加“http”(因为现在每个站点都有它),以便实际重定向到外部链接。 - Mariana Marica

59

适用于 Laravel 5.x 及以上版本

return redirect()->away('https://www.google.com');

根据文档所述:

有时您可能需要重定向到应用程序外的域。 您可以通过调用away方法来实现,该方法创建一个RedirectResponse,而无需进行任何其他URL编码、验证或验证:


1
这对于 Laravel 5.8 仍然有效 -> 更新链接:https://laravel.com/docs/5.8/responses#redirecting-external-domains - Brad Ahrens
1
仍适用于 Laravel 6.x 和 7.x。文档链接:https://laravel.com/docs/7.x/responses#redirecting-external-domains - G Chris DCosta
作为 @DavidLartey,使用 Laravel 8.x…… 不要忘记添加 use Illuminate\Http\RedirectResponse; - bcag2

30
< p >在$url中定义您想要重定向的URL

然后只需使用

return Redirect::away($url);

如果您想在视图内部进行重定向,请使用

return Redirect::to($url);

阅读更多关于重定向

更新1:

这里是简单的例子

return Redirect::to('http://www.google.com');

更新2:

由于提问者希望在同一页返回,因此需要进行相应的调整。

$triggersms = file_get_contents('http://www.cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=efg&password=abcd&msisdn=9197xxx2&sid=MYID&msg=Hello');
return $triggersms;

return Redirect::to('http://www.cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=chipsnbytes&password=w5p898e3&msisdn=919747320492&sid=SCHOOL&msg=Your WEB YOUR SCHOOL 登录的 OTP 为 5He6i。请不要将 OTP 透露给任何人。&fl=0&gwid=2') 无法正常工作.... 如果我们尝试进行加载,它会直接指向 http://localhost/mywork/http://www.www.cloud.smsindiahub*****.com .... 但是 return Redirect::away($url); 是可行的.. 但是在加载 $url 并发送短信后,它并未返回到我的控制器。 - manoos
你能提供一下你正在尝试的虚拟网址吗? - Sulthan Allaudeen
它对我来说运行良好,并返回结果 {"ErrorCode":"7","ErrorMessage":"用户名或密码无效","JobId":null,"MessageData":null} - Sulthan Allaudeen
$url = "http://www.cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=efg&password=abcd&msisdn=9197xxx2&sid=MYID&msg=Hello OTP&fl=0&gwid=2"; return Redirect::to($url);我还收到了消息,但是在发送短信后,我需要在我的控制器中获取响应...也就是说,在成功发送短信后,URL应该重定向到我的Laravel控制器...这是我的问题。 - manoos
显示剩余8条评论

6
您可以使用 Redirect::away($url)

嗨,我使用了return Redirect :: away($ url),它运行良好...非常感谢..但是在加载此url后,我将如何获取返回的消息..这个url只是给出消息“ {”ErrorCode“:”000“,”ErrorMessage“:”Success“,”JobId“:”f136e997-8885-4cc3-a5b4-e9ff394c0a4c“,”MessageData“:[{”Number“:”9197xxxxx“,”MessageParts“:[{”MsgId“:”9197xxx2-21ac722669d248a5b75c73d79500e154“,”PartId“:1,”Text“:”Message。“}]}]}“..它没有返回到我的控制器。你知道怎么解决这个问题吗? - manoos
你好,return Redirect::to($url); 只是用于在网站内部进行重定向。 - Vishal Wadhawan
@Cishal: 你好,我使用了return Redirect::away($url)并且它运行良好...非常感谢...但是在加载此URL后,我该如何获取返回的消息。这个URL只是给出消息"{"ErrorCode":"000","ErrorMessage":"Success","JobId":"f136e997-8885-4cc3-a5b4-e9‌​ff394c0a4c","MessageData":[{"Number":"9197xxxxx","MessageParts":[{"MsgId":"9197xx‌​x2-21ac722669d248a5b75c73d79500e154","PartId":1,"Text":"Message."}]}]}"。它没有返回到我的控制器。你知道如何解决这个问题吗? - manoos
在这种情况下,您不应该重定向它。您可以使用file_get_contents($url)来获取返回的值。 - Vishal Wadhawan
我已经尝试过使用 file_get_contents($url); 但它返回了一个错误。file_get_contents(http://www.cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=fff&password=ssdfs&msisdn=97dddd&sid=sdsd&msg=sdas.&fl=0&gwid=2):无法打开流:HTTP请求失败!HTTP / 1.1 400 Bad Request - manoos

4
针对 Laravel 5.x,我们可以只使用以下代码进行重定向:
return redirect()->to($url);

4
如果你正在使用InertiaJS,那么像在inertiaJS的github上看到的away()方法是行不通的,他们正在讨论创建一个"外部重定向"的最佳方法。目前的解决方案是返回409状态,并使用X-Inertia-Location头部告知URL,如下所示:
return response('', 409)
            ->header('X-Inertia-Location', $paymentLink);

paymentLink是您想要发送给用户的链接。

SOURCE: https://github.com/inertiajs/inertia-laravel/issues/57#issuecomment-570581851


3

此外,添加类

use Illuminate\Http\RedirectResponse;

然后,就像这样:
public function show($id){
    $link = Link::findOrFail($id);  // get data from db table Links
    return new RedirectResponse($link->url);  // and this my external link, 
}

或者

return new RedirectResponse("http://www.google.com?andParams=yourParams"); 

对于外部链接,必须使用以'http'开头的完整URL字符串。


1
对于 Laravel 8,您也可以使用:


Route::redirect('/here', '/there');
//or
Route::permanentRedirect('/here', '/there');

这也适用于外部URL


0

return Redirect::away($url); 可以用于重定向

另外,return Redirect::to($url); 可以在视图中进行重定向。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接