使用 laravel 中的 return 重定向到外部 URL
Redirect to external URL with return in laravel
我正在尝试使用 SMS INDIA HUB API 向用户发送一次性密码。
为此,我需要重定向到 URL 格式:
如果我们加载此 URL,它将 return 一些消息。我需要将该消息发送至。
我这样试过
$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);
但它不指向那个link。它尝试在本地主机中加载 URL。
或者是否有任何插件可以使用 SMS INDIA HUB 发送短信?
有人能帮忙吗??
您应该可以像这样重定向到 url
return Redirect::to($url);
您可以使用Redirect::away($url)
在 $url
中定义要重定向的 url
那就用
return Redirect::away($url);
如果你想在你的视图中重定向使用
return Redirect::to($url);
阅读更多关于 Redirect here
更新 1:
这是一个简单的例子
return Redirect::to('http://www.google.com');
更新 2:
因为发问者想要return在同一页
$triggersms = file_get_contents('http://www.cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=efg&password=abcd&msisdn=9197xxx2&sid=MYID&msg=Hello');
return $triggersms;
此外,添加 class
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");
对于外部链接,必须使用完整的 URL 字符串,开头为 'http'。
对于 Laravel 5.x 我们可以重定向
return redirect()->to($url);
Laravel5.x及以上
return redirect()->away('https://www.google.com');
如 docs 中所述:
Sometimes you may need to redirect to a domain outside of your
application. You may do so by calling the away method, which creates a
RedirectResponse without any additional URL encoding, validation, or
verification:
如果您使用的是 InertiaJS,away()
方法将无法像在 inertiaJS github 上看到的那样工作,他们正在讨论创建“外部redirect" on inertiaJS,目前的解决方案是 return 一个 409 状态 X-Inertia-Location
header 通知 url,像这样:
return response('', 409)
->header('X-Inertia-Location', $paymentLink);
其中 paymentLink 是您要将用户发送到的 link。
来源:https://github.com/inertiajs/inertia-laravel/issues/57#issuecomment-570581851
return Redirect::away($url);
应该可以重定向
此外,return Redirect::to($url);
在视图内重定向。
对于Laravel 8你也可以使用
Route::redirect('/here', '/there');
//or
Route::permanentRedirect('/here', '/there');
这也适用于外部 URL
我正在尝试使用 SMS INDIA HUB API 向用户发送一次性密码。 为此,我需要重定向到 URL 格式:
如果我们加载此 URL,它将 return 一些消息。我需要将该消息发送至。
我这样试过
$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);
但它不指向那个link。它尝试在本地主机中加载 URL。
或者是否有任何插件可以使用 SMS INDIA HUB 发送短信?
有人能帮忙吗??
您应该可以像这样重定向到 url
return Redirect::to($url);
您可以使用Redirect::away($url)
在 $url
那就用
return Redirect::away($url);
如果你想在你的视图中重定向使用
return Redirect::to($url);
阅读更多关于 Redirect here
更新 1:
这是一个简单的例子
return Redirect::to('http://www.google.com');
更新 2:
因为发问者想要return在同一页
$triggersms = file_get_contents('http://www.cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=efg&password=abcd&msisdn=9197xxx2&sid=MYID&msg=Hello');
return $triggersms;
此外,添加 class
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");
对于外部链接,必须使用完整的 URL 字符串,开头为 'http'。
对于 Laravel 5.x 我们可以重定向
return redirect()->to($url);
Laravel5.x及以上
return redirect()->away('https://www.google.com');
如 docs 中所述:
Sometimes you may need to redirect to a domain outside of your application. You may do so by calling the away method, which creates a RedirectResponse without any additional URL encoding, validation, or verification:
如果您使用的是 InertiaJS,away()
方法将无法像在 inertiaJS github 上看到的那样工作,他们正在讨论创建“外部redirect" on inertiaJS,目前的解决方案是 return 一个 409 状态 X-Inertia-Location
header 通知 url,像这样:
return response('', 409)
->header('X-Inertia-Location', $paymentLink);
其中 paymentLink 是您要将用户发送到的 link。
来源:https://github.com/inertiajs/inertia-laravel/issues/57#issuecomment-570581851
return Redirect::away($url);
应该可以重定向
此外,return Redirect::to($url);
在视图内重定向。
对于Laravel 8你也可以使用
Route::redirect('/here', '/there');
//or
Route::permanentRedirect('/here', '/there');
这也适用于外部 URL