data=%7B%22password%22%3A%221234%22%2C%22username%22%3A%22user1234%22%7D&
我有来自POST请求的这个类型的URL并解码它看起来像这样......
data = {"password":"1234", "username":"user1234"}&
在我的情况下,我试图用Laravel进行解码,但它无法正常工作。我怎样才能做到这一点?
<?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);
}
答案 0 :(得分:1)
这样的事情应该有效:
$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);