![](/img/trans.png)
[英]Laravel - redirect request to external url and return its response as response
[英]Redirect to external URL with return in laravel
我正在嘗試使用 SMS INDIA HUB API 向用戶發送一次性密碼。 為此,我需要重定向到 URL 格式:
如果我們加載這個 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 發送短信?
有人可以幫忙嗎??
對於 Laravel 5.x 及更高版本
return redirect()->away('https://www.google.com');
如文檔中所述:
有時您可能需要重定向到應用程序之外的域。 您可以通過調用 away 方法來實現,該方法創建 RedirectResponse 而不需要任何額外的 URL 編碼、驗證或驗證:
在$url
定義要重定向的$url
然后只需使用
return Redirect::away($url);
如果你想在你的視圖中重定向使用
return Redirect::to($url);
這是一個簡單的例子
return Redirect::to('http://www.google.com');
由於發問者希望返回同一頁面
$triggersms = file_get_contents('http://www.cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=efg&password=abcd&msisdn=9197xxx2&sid=MYID&msg=Hello');
return $triggersms;
您可以使用Redirect::away($url)
對於 Laravel 5.x,我們可以只重定向
return redirect()->to($url);
另外,添加類
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 字符串。
如果您使用的是 InertiaJS,那么away()
方法將無法像在inertiaJS github 上看到的那樣工作,他們正在討論在inertiaJS 上創建“外部重定向”的最佳方法,現在的解決方案是使用X-Inertia-Location
返回409狀態X-Inertia-Location
標頭通知 url,如下所示:
return response('', 409)
->header('X-Inertia-Location', $paymentLink);
其中 paymentLink 是您要將用戶發送到的鏈接。
來源: 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
請參閱: https : //austencam.com/posts/setting-up-an-m1-mac-for-laravel-development-with-homebrew-php-mysql-valet-and-redis
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.