user8114890
user8114890

Reputation: 31

How to get a specific part of a URL using PHP

I've had a look around and can't find an answer specific to my needs...

This is the URL being sent back to my redirect page from an identity server: http://localhost/?code=92092c2f65560d835a73cda852393316&state=random_state&session_state=CEwifhoy_x1RJWH-DoV_9Q1yJVNUXV4z5YRGGuXsUvs.39c667f469b8f6585a9f699d0b7d76f3

I need to get the code between "code=" and "&state".

Then put it in the <> part of "grant_type=authorization_code&code=<>&redirect_uri=<>" so I can request a token. This part I can do, I just can't figure out how to get the code out of the URL.

Thanks.

Upvotes: 0

Views: 76

Answers (3)

Remco K.
Remco K.

Reputation: 717

For this you can use the $_GET variable. It will become more clear for you if you do:

<?php
print_r($_GET);
?>

Then you can see there is a key "code" in this array. You can get the value of this key like this:

<?php
$code = $_GET['code'];
?>

Upvotes: 0

Musa
Musa

Reputation: 97672

Use parse_url with a component of PHP_URL_QUERY to get the query string.
Then parse_str to get the particular argument

http://php.net/manual/en/function.parse-url.php
http://php.net/manual/en/function.parse-str.php

Upvotes: 0

Dominik Szymański
Dominik Szymański

Reputation: 822

You should just use $_GET environment variable. All these data after the file address itself (in your case /) and ? sign are GET data. To retrieve "code" value you should use:

$variable = $_GET['code'];

It's just as simple as that.

Upvotes: 1

Related Questions