Laravel 5解码发布方法URL [英] Laravel 5 Decode Post Method URL

查看:55
本文介绍了Laravel 5解码发布方法URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

data=%7B%22password%22%3A%221234%22%2C%22username%22%3A%22user1234%22%7D&

我从POST请求中获得了这种类型的url,并对其进行了解码,看起来像这样...

I have this type url from a POST request and decoded it looks like this...

data = {"password":"1234", "username":"user1234"}&

就我而言,我正在尝试使用Laravel解码,但无法正常工作.我该如何实现?

In my case, I'm trying to decode with Laravel but it doesnt work properly. How can I achieve this?

<?php

function login(Request $request)
{
    $requestData = $request->request->get('data');

    $data = json_decode($requestData, true);

    $username = $data['username'];
    $password = $data['password'];

    return new JsonResponse($username);
}

推荐答案

类似的方法应该起作用:

Something like this should work:

$url = "data=%7B%22password%22%3A%221234%22%2C%22username%22%3A%22user1234%22%7D&";
$decode = urldecode($url);
$sub = substr($decode, strpos($decode,'{')+strlen('{'),strlen($decode));
$sub2 = substr($sub,0,strpos($sub,'}'));
$explode = explode(',', $sub2);
$data = array();
foreach($explode as $arrayData)
{
    $arrayData = explode(":",$arrayData);
    $key = substr($arrayData[0], 1, -1);
    $value = substr($arrayData[1], 1, -1);
    $data[$key] = $value;
}
print_r($data);

这篇关于Laravel 5解码发布方法URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆