reinisk22's avatar

How to retrieve data from ULR after redirect

Hey,

        $url = self::ACCOUNT_URL . '/authorize?' . $this->buildUrl();

        return redirect($url);

These two lines redirect me to a new page, and I'd like to retrieve one of the URL parameters, but can't seem to get it.

URL is like this: http://example.com/example?code=AQCr8fvYhAynSbRlfK

What I would like to get is the code value. I tried to get it with $_GET['code'] but couldn't.

0 likes
11 replies
Nakov's avatar

If you use Laravel, the request('code') will return the value of the code.

Sinnbeck's avatar

This would be your method in the controller

public function show(Request $request) 
{
    $value = $request->code;
reinisk22's avatar

@nakov I didn't understand how to implement @sinnbeck suggestion ;/

For me the problem is, that this code without return will not generate the URL, it just returns a blank page with the same URL it had before redirect. So basically I need to do return to get the URL, but after return, I can't really do anything else.


     return redirect($url);
Cronix's avatar

So basically I need to do return to get the URL, but after return, I can't really do anything else.

The "do something else" would happen in the controller/method that is responding to the /authorize route that you are redirecting to. It's a new/separate HTTP request. Do you have a GET route set up for /authorize?

Nakov's avatar

Where do you tried the GET then :)

in your routes file add this:

$router->get('/authorize', function(Request $request) {
    dd($request->code);
});
reinisk22's avatar

@cronix Hey, I guess I don't have the get request, I was thinking I could redirect, get parameters and then use the code in POST request after.

This is my class while I'm testing how stuff will work:

<?php

declare(strict_types=1);

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class SpotifyAuthController extends Controller
{
    const ACCOUNT_URL = 'https://accounts.spotify.com';
    const API_URL = 'https://api.spotify.com';

    public function __invoke(Request $request)
    {
        return $this->getAuthorizationCode();
    }

    public function buildUrl()
    {
        $data = [
            'client_id'     => '16a089d89e6942e080a33aae39602948',
            'response_type' => 'code',
            'redirect_uri'  => 'http://lumen1.test/callback',
            'scope'         => 'user-read-private',
        ];

        return http_build_query($data);
    }

    public function getAuthorizationCode()
    {
        $url = self::ACCOUNT_URL . '/authorize?' . $this->buildUrl();

        return redirect($url);
    }
}

And this is the route I'm using, for the controller to start to work when I press on a button:

$router->post('/', 'Auth\SpotifyAuthController');
reinisk22's avatar

@nakov I make this call to an external API, I don't have this view myself.

Nakov's avatar

so you don't have access to their code :)

but I bet there is a webhook that can be set on their site so when the authorization is successful they post to your endpoint.

reinisk22's avatar

@nakov Hmm, could be I guess. Will have to keep trying to find out how it's supposed to work :D Thanks again man

Please or to participate in this conversation.