I'm facing difficulty in fetching URL parameters from redirect URL of Fitbit I'm here trying to integrate Fitbit without using socialite or any other package.
I have the following URL:
http://localhost/abc/public/portal/fitbit/fitbitIntegration#access_token=eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1WjVLUzUiLCJhdWQiOiIyMjhMNjUiLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3NfdG9rZW4iLCJzY29wZXMiOiJ3aHIgd251dCB3cHJvIHdzbGUgd3dlaSB3c29jIHdzZXQgd2FjdCB3bG9jIiwiZXhwIjoxNTA1NDU5MTM2LCJpYXQiOjE1MDQ4NzE1MjF9.iQ9nxbzmvar2DlG_848b3MTefq7q0wxyXByTb1Bb2o4&user_id=5Z5KS5&scope=sleep+settings+nutrition+activity+social+heartrate+profile+weight+location&token_type=Bearer&expires_in=587615
All I want to fetch all the information after the #
like I want to get user_id
here
I have tried this to accomplish
public function fitbitIntegration(Request $request)
{
try{
$authId = Auth::user()->id;
$fitbit = new FitbitMaster();
$fitbit->user_id = $authId;
$fitbit->fitbit_client_id = $request->user_id;
$fitbit->save();
flash('Connected', 'success');
return redirect()->back();
}
catch (QueryException $exception)
{
echo $exception->getMessage();
}
}
Route
Route::get('fitbitIntegration', 'BackEnd\fitbit@fitbitIntegration');
But I'm not getting the user_id
here which got me Integrity constraint error
How to get user_id
here in my case?
Demo_Link: http://phpfiddle.org/main/code/hdcu-3axu
Store Your Dynamic Url in any variable, in your controller and Do Like This.
<?php
//store your dynamic url in any variable
$url = "http://localhost/abc/public/portal/fitbit/fitbitIntegration#access_token=eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1WjVLUzUiLCJhdWQiOiIyMjhMNjUiLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3NfdG9rZW4iLCJzY29wZXMiOiJ3aHIgd251dCB3cHJvIHdzbGUgd3dlaSB3c29jIHdzZXQgd2FjdCB3bG9jIiwiZXhwIjoxNTA1NDU5MTM2LCJpYXQiOjE1MDQ4NzE1MjF9.iQ9nxbzmvar2DlG_848b3MTefq7q0wxyXByTb1Bb2o4&user_id=5Z5KS5&scope=sleep+settings+nutrition+activity+social+heartrate+profile+weight+location&token_type=Bearer&expires_in=587615";
$access_token = substr($url, strpos($url, "#") + 1);
echo $access_token; // get the string of acess token with parametes
?>
UPDATED
NOTE: fragment part of url after the #
It's never sent to the server side, So.. its not possible to get the that using any $_SERVER[..]
in php but we can read it. So Now we can use JAVA SCRIPT MAGIC
do this:
<?php
//Here you Can See save.php page inside an $url where its redirect with your access_token after clicking on `Click Here` link (in your case its redirect routes `fitbitIntegration` as you mentioned ). So i try to create the situation as your routes after its redirect with token, Fine !!
$url = "http://localhost/save.php/fitbitIntegration#access_token=eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1WjVLUzUiLCJhdWQiOiIyMjhMNjUiLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3NfdG9rZW4iLCJzY29wZXMiOiJ3aHIgd251dCB3cHJvIHdzbGUgd3dlaSB3c29jIHdzZXQgd2FjdCB3bG9jIiwiZXhwIjoxNTA1NDU5MTM2LCJpYXQiOjE1MDQ4NzE1MjF9.iQ9nxbzmvar2DlG_848b3MTefq7q0wxyXByTb1Bb2o4&user_id=5Z5KS5&scope=sleep+settings+nutrition+activity+social+heartrate+profile+weight+location&token_type=Bearer&expires_in=587615";
?>
<a target="_blank" href="<?php echo $url; ?>"> Click Here </a>
<?php
echo '<br><br><br>';
?>
<!-- Now after redirect you have to use an javascript inside controller or inside an routes directly to aceess the url an getting an access_token, here i try to store inside the cookies of using js and retrieve it using php and echo -->
<?php
echo "<script language='javascript'>
token = window.location.hash.split('#')
document.cookie='access_token='+token[1];
document.cookie='token[1]';
alert(token[1]);
document.cookie='token[1]';
if (token[1] != '') {
//window.location.reload()
//location.reload(true);
}
else{
//document.cookie='token[1]';
}
</script>";
//token_name=$_COOKIE['access_token'];
// Here We get the access token inside the $_COOKIE['access_token'];
if(isset($_COOKIE['access_token'])){
echo $_COOKIE['access_token'];
}
?>
Output: