February 27, 2012

In codeigniter, How to get the token returned from google login as parameter to controller?

Question by kamal

I am working with AuthSub to view portfolios of google finance api on codeigniter framework.

after successful login of google it redirects to the url we provide.

I have provided url like: www.finance.mysite.com/google/token/

google will append its token like:

www.finance.mysite.com/google/token/?token=1/gyXbtvKT4XaIuUIhEBAsuxyDgATMDk3ztAG3MocNkKI

How can I get it inside a function token() inside google controller.

Answer by Starx

Just extract the token, and route it to controller of your choice.
You can extract the params like this

$params = "http://www.finance.mysite.com/google/token/?token=1/gyXbtvKT4XaIuUIhEBAsuxyDgATMDk3ztAG3MocNkKI";
$parsed = parse_url($params);
$pieces = explode("=", $parsed['query']);
$searchIndex = array_search("token", $pieces);
if($searchIndex) {
    $token = $pieces[$searchIndex+1];
    //now use it as you need
    redirect("controller/google/$token");
}

Note: The code above will only work, if there is only single parameter on the url, or else not.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!