2

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?

Mr. Pyramid
  • 3,855
  • 5
  • 32
  • 56
  • 1
    There are `#` in your url, thats why you are not getting url parameters – RAUSHAN KUMAR Sep 08 '17 at 12:01
  • yes but I can't change that '#' because it's third party they have such url scheme now how could I able to get this id? – Mr. Pyramid Sep 08 '17 at 12:03
  • you can use js to get the url parameters – RAUSHAN KUMAR Sep 08 '17 at 12:03
  • https://stackoverflow.com/questions/940905/can-i-read-the-hash-portion-of-the-url-on-my-server-side-application-php-ruby See this answer for more explanation on how this is not possible on the server side. – miln Sep 08 '17 at 13:25

3 Answers3

1

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:

enter image description here

RïshïKêsh Kümar
  • 4,734
  • 1
  • 24
  • 36
0

If you want to get the value after the hash mark or anchor as shown in a user's browser: This isn't possible with "standard" HTTP as this value is never sent to the server (hence it won't be available in $_SERVER["REQUEST_URI"] or similar predefined variables). You would need some sort of JavaScript magic on the client side, e.g. to include this value as a POST parameter.

That part is called "fragment". If you have any static url then you can get it in this way:

$url=parse_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");
echo $url["fragment"]; //This variable contains the fragment
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
0

you can define a route like fitbitIntegration/{param} and in your function fitbitIntegration, you add param like fitbitIntegration ( Request $req, $param); then you can use regex on your params to get want you want

Steve Ruben
  • 1,336
  • 11
  • 20