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条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵