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.

...

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