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?

4
  • 1
    There are # in your url, thats why you are not getting url parameters Commented Sep 8, 2017 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? Commented Sep 8, 2017 at 12:03
  • you can use js to get the url parameters Commented Sep 8, 2017 at 12:03
  • stackoverflow.com/questions/940905/… See this answer for more explanation on how this is not possible on the server side. Commented Sep 8, 2017 at 13:25

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

7 Comments

bhai mere controller function mein implement kr de isko kaise krna hai
problem lies here how to get url value in $url
I think You can get the URL from the routes in Laravel and then you can use/store it inside your controller.
I don't have that url in route it is a third party url
Happy to Help Bhai :)
|
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

6 Comments

how could I achieve this in controller any illustration?
i don't think it is possible in controller methods using php
hey that link is dynamic I need a I dynamic solution here bro :)
yeah, that whay i mentioned that in my answer
@RAUSHANKUMAR At least Mention the Link of Copied ANS ;)
|
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

2 Comments

not getting as parameter got not Not Found Exception
share your web or api file

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.