0
$\begingroup$

I am using google api and in url i am getting like this:

http://website.com/generate-token/#access_token=ya29.AHES6ZTJwb9lzb0lua81oHa47-8ImcJf-8qE-02kIn8JcgEv&token_type=Bearer&expires_in=3600

I tried:

$_SERVER['QUERY_STRING']
$url =  "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

But what i got is http://website.com/generate-token/

How can i get either the complete url and use regex to get the access_token or any other way in which i can get it?

$\endgroup$
5
  • $\begingroup$ The URL there has no querystring. The values are part of the fragment of the URL. ie. the # values. You would need to get window.location.hash and split it twice by & and then =, and find the key you want to get the value using JS. $\endgroup$ Commented Oct 30, 2013 at 13:25
  • $\begingroup$ Short answer: you can't. (not without JS) $\endgroup$ Commented Oct 30, 2013 at 13:25
  • $\begingroup$ Did you mean to tag jQuery? I assume (from the above) that you want to do this at the server. $\endgroup$ Commented Oct 30, 2013 at 13:26
  • $\begingroup$ I need just the access_token from this url, it can be by any method, either by js, jquery, etc. @RoryMcCrossan can you tell how can i get it via JS? $\endgroup$ Commented Oct 30, 2013 at 13:30
  • $\begingroup$ It depends on what you want to do with it. If you want to use the value in PHP then you don't want to do it in JS. $\endgroup$ Commented Oct 30, 2013 at 13:33

1 Answer 1

0
$\begingroup$

My two cents (since you tagged it with jQuery i suppose Javascript is a valid solution):

var url = 'http://website.com/generate-token/
           #access_token=ya29.AHES6ZTJwb9lzb0lua81oHa47-8ImcJf-8qE-
           02kIn8JcgEv&token_type=Bearer&expires_in=3600';

function getParameterByName(name, url) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&#]" + name + "=([^&#]*)"),
        results = regex.exec(url);
    return results == null ? "" : 
           decodeURIComponent(results[1].replace(/\+/g, " "));
}

alert(getParameterByName('access_token', url));

http://jsfiddle.net/yejwF/

$\endgroup$
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.