0

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?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
user2485649
  • 235
  • 1
  • 3
  • 13
  • 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. – Rory McCrossan Oct 30 '13 at 13:25
  • Short answer: you can't. (not without [JS](http://stackoverflow.com/questions/6778990/)) – Amal Murali Oct 30 '13 at 13:25
  • Did you mean to tag jQuery? I assume (from the above) that you want to do this at the server. – Reinstate Monica Cellio Oct 30 '13 at 13:26
  • 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? – user2485649 Oct 30 '13 at 13:30
  • 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. – Reinstate Monica Cellio Oct 30 '13 at 13:33

1 Answers1

0

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/

Johan
  • 35,120
  • 54
  • 178
  • 293