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

时间:2017-09-08 12:00:06

标签: php laravel laravel-5.3

我很难从Fitbit的重定向网址中获取网址参数我在这里试图在不使用社交网站或任何其他软件包的情况下整合Fitbit。

我有以下网址:

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

我希望在#之后获取所有信息,就像我想要user_id这里

一样

我试过这个来完成

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::get('fitbitIntegration', 'BackEnd\fitbit@fitbitIntegration');

但是我没有得到user_id这里有我的完整性约束错误

如何在我的案例中获取user_id

3 个答案:

答案 0 :(得分:1)

  

Demo_Link: http://phpfiddle.org/main/code/hdcu-3axu

将动态网址存储在控制器中的任何变量中并执行此类操作。

<?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

?>
  

已更新

注意:#之后将url的一部分片段从未发送到服务器端,所以..它不可能在php中使用任何$_SERVER[..]来获取它但我们可以读它。所以现在我们可以使用JAVA SCRIPT MAGIC执行此操作:

----------------------------------------------- --------------------------------

<?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'];
}

?>

输出

enter image description here

答案 1 :(得分:0)

如果您希望在用户浏览器中显示的哈希标记或锚点之后获取值:使用"standard" HTTP无法实现此值,因为此值永远不会发送到服务器(因此它无法在$_SERVER["REQUEST_URI"]或类似的预定义变量中使用)。您需要在客户端使用某种JavaScript魔法,例如将此值包含为POST参数。

该部分被称为&#34;片段&#34;。如果您有静态网址,那么您可以通过以下方式获取:

$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

答案 2 :(得分:0)

你可以定义一个类似$scope.DoSomthing= function () { var rows = $scope.Grid.selection.getSelectedRows(); angular.forEach(rows, function (row, key) { angular.forEach(row, function (column, colKey) { if (colKey == "Your Column binding string") { console.log(column); } }); }); 的路由,在你的函数fitbitIntegration中,你可以添加像fitbitIntegration这样的参数(Request $ req,$ param);那么你可以在你的params上使用正则表达式来获得你想要的