dream890110 2017-09-08 12:00
浏览 1129
已采纳

如何在Laravel中获取URL参数的值?

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?

  • 写回答

3条回答 默认 最新

  • doue1925 2017-09-29 05:32
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程