如何使用PHP获取URL的特定部分

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.

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'];
?>

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.

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