After authencating through my FB application I am getting access token in redirect url as below -
128.136.227.197/readjson.php#access_token=CAACYyELn7nsBANisLtwVsjibKZC3JSZButoKyO3GC9aPOTXtrWOtEV0lcIMnXGSVPWZCngewl2MtinU9FsJ9hnWdV1yQDVsUFZAZAYsosEVtKwZCR5HBtdAd7CQrsvprUKlDOZALBWmbmhy4HkvW8GCsx9OkwZDZD&expires_in=4527
How to get this accesstoken in my php script ?
I am trying to get it like below -
if($_REQUEST['access_token'])
echo $access_token=$_REQUEST['access_token'];
But not getting it's value.
Please help me to get access_token in my script readjson.php
Thanks in advance
If you're able to parse the URL with the access token in it, you can simply strip away anything but the access token
A simple call to preg_match()
does the trick
$str = "128.136.227.197/readjson.php#access_token=CAACYyELn7nsBANisLtwVsjibKZC3JSZButoKyO3GC9aPOTXtrWOtEV0lcIMnXGSVPWZCngewl 2MtinU9FsJ9hnWdV1yQDVsUFZAZAYsosEVtKwZCR5HBtdAd7CQrsvprUKlDOZALBWmbmhy4HkvW8GCsx9OkwZDZD&expires_in=4527";
preg_match("/access_token=(.*)(?=&)/", $str, $match);
echo "<pre>";
print_r($match);
echo "</pre>";
The above prints:
Array
(
[0] => access_token=CAACYyELn7nsBANisLtwVsjibKZC3JSZButoKyO3GC9aPOTXtrWOtEV0lcIMnXGSVPWZCngewl2MtinU9FsJ9hnWdV1yQDVsUFZAZAYsosEVtKwZCR5HBtdAd7CQrsvprUKlDOZALBWmbmhy4HkvW8GCsx9OkwZDZD
[1] => CAACYyELn7nsBANisLtwVsjibKZC3JSZButoKyO3GC9aPOTXtrWOtEV0lcIMnXGSVPWZCngewl2MtinU9FsJ9hnWdV1yQDVsUFZAZAYsosEVtKwZCR5HBtdAd7CQrsvprUKlDOZALBWmbmhy4HkvW8GCsx9OkwZDZD
)
Since the value is passed as URI Fragment, it will not be accessible using $_REQUEST
There are two ways which I am aware of. One is using Javascript(which i am not familiar with) and the other using PHP.
$url=parse_url("http://128.136.227.197/readjson.php#access_token=CAACYyELn7nsBANisLtwVsjibKZC3JSZButoKyO3GC9aPOTXtrWOtEV0lcIMnXGSVPWZCngewl2MtinU9FsJ9hnWdV1yQDVsUFZAZAYsosEVtKwZCR5HBtdAd7CQrsvprUKlDOZALBWmbmhy4HkvW8GCsx9OkwZDZD&expires_in=4527");
$fragment = $url['fragment'];
$access_token = preg_replace('/&expires_in=(.*)/',"",$fragment); //To obtain access_token part only
P.S The above code is tested only for the given url.
You are using client side without JS SDK authentication for a server side implementation.
Don't do that.
The correct way would be to use a server side authentication flow which allows the code/token to be obtained in $_REQUEST
https://developers.facebook.com/docs/facebook-login/login-flow-for-web-no-jssdk/
It seem the only way is via Javascript w/ Ajax, as the browser does not pass the URL fragment to the server. So with javascript you can pass either the full URL w/ the fragment to the server (via ajax or a redirect with the URL in the GET query) and parse the URL w/
parse_url($url, PHP_URL_FRAGMENT);
Or you can parse the fragment in javascript and pass the information via GET or POST to the server in an ajax call.