Redirect to external URL with return in laravel

PhpLaravelLaravel 4Sms Gateway

Php Problem Overview


I am trying to send one time password to a user using SMS INDIA HUB API. For that purpose I need to redirect to a URL format:

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

If we load this URL, it will return some message. I need to get that message to.

I tried like this

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

But it is not directing to that link. It tries to load that URL in localhost.

Or is there any plugin to send sms using SMS INDIA HUB?

Can anyone help??

Php Solutions


Solution 1 - Php

You should be able to redirect to the url like this

return Redirect::to($url);

You can read about Redirects in the Laravel docs here.

Solution 2 - Php

For Laravel 5.x and above

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

as stated in the 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:

Solution 3 - Php

Define the url you want to redirect in $url

Then just use

return Redirect::away($url);

If you want to redirect inside your views use

return Redirect::to($url);

Read more about Redirect here

Update 1 :

Here is the simple example

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

Update 2 :

As the Questioner wants to return in the same page

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

Solution 4 - Php

You can use Redirect::away($url)

Solution 5 - Php

For Laravel 5.x we can redirect with just

return redirect()->to($url);

Solution 6 - Php

Also, adding class

use Illuminate\Http\RedirectResponse;

and after, like this:

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

or

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

For external links have to be used full URL string with 'http' in begin.

Solution 7 - Php

If you're using InertiaJS, the away() approach won't work as seen on the inertiaJS github, they are discussing the best way to create a "external redirect" on inertiaJS, the solution for now is return a 409 status with X-Inertia-Location header informing the url, like this:

return response('', 409)
            ->header('X-Inertia-Location', $paymentLink);

Where paymentLink is the link you want to send the user to.

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

Solution 8 - Php

For Laravel 8 you can also use

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

This also works with external URLs

Solution 9 - Php

return Redirect::away($url); should work to redirect

Also, return Redirect::to($url); to redirect inside the view.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmanoosView Question on Stackoverflow
Solution 1 - PhpLaurenceView Answer on Stackoverflow
Solution 2 - PhpAdamView Answer on Stackoverflow
Solution 3 - PhpSulthan AllaudeenView Answer on Stackoverflow
Solution 4 - PhpVishal WadhawanView Answer on Stackoverflow
Solution 5 - PhpsadiqView Answer on Stackoverflow
Solution 6 - PhpArthur TsidkilovView Answer on Stackoverflow
Solution 7 - PhpCarlos SallesView Answer on Stackoverflow
Solution 8 - PhpFlorian FalkView Answer on Stackoverflow
Solution 9 - PhpAviView Answer on Stackoverflow