PHP削减GET请求参数

时间:2018-11-20 08:24:43

标签: php arrays json object request

在我的PHP应用程序中,我使用此代码来获取请求

$this->request->get

这将返回所有数据

Array
(
[rt] => extension/dump
[type] => xls
[controller] => list/product
[s] => administration
[token] => s7KO9PkoPmgpQmZjPQOimTbdKMB85uGa
[filters] => {"_search":true,"nd":1542700897278,"rows":20,"page":1,"sidx":"date_modified","sord":"desc","filters":"{\"groupOp\":\"AND\",\"rules\":[{\"field\":\"name\",\"op\":\"cn\",\"data\":\"Lemon 
[Eucalyptus\"}]}"}] => 
)

但是下一个代码会剪切数据

$this->request->get['filters']

仅返回部分数据(请参见“过滤器”的末尾已删除)

{"_search":true,"nd":1542700897278,"rows":20,"page":1,"sidx":"date_modified","sord":"desc","filters":"
{\"groupOp\":\"AND\",\"rules\":[{\"field\":\"name\",\"op\":\"cn\",\"data\":\"Lemon

是否有任何解决方法可在“过滤器”中获取完整数据? 将对象转换为数组没有帮助

更新: js控制台输出

{_search: false, nd: 1542700894743, rows: 20, page: 1, sidx:    "date_modified", …}
filters: "{"groupOp":"AND","rules":[{"field":"name","op":"cn","data":"Lemon & Eucalyptus"}]}"
nd: 1542700897278
page: 1
rows: 20
sidx: "date_modified"
sord: "desc"
_search: true

Request URL: http://blablabla&filters={%22_search%22:true,%22nd%22:1542700897278,%22rows%22:20,%22page%22:1,%22sidx%22:%22date_modified%22,%22sord%22:%22desc%22,%22filters%22:%22{\%22groupOp\%22:\%22AND\%22,\%22rules\%22:[{\%22field\%22:\%22name\%22,\%22op\%22:\%22cn\%22,\%22data\%22:\%22Lemon%20&%20Eucalyptus\%22}]}%22}

请求方法:GET

与&符号有某种联系吗?

1 个答案:

答案 0 :(得分:1)

假设您正在JS中创建URL和查询参数,则需要encode所有在URL中使用的参数。

您的情况...

const filters = {
  "groupOp": "AND",
  "rules": [
    {
      "field": "name",
      "op": "cn",
      "data": "Lemon & Eucalyptus"
    }
  ]
}

const url = `http://blablabla?filters=${encodeURIComponent(JSON.stringify(filters))}`

document.write(`<pre>${url}</pre>`)

请注意,&中的"Lemon & Eucalyptus"已变成%26


一个额外的问题是,某些东西正在对请求参数进行HTML编码。除非您专门在HTML文档中显示信息,否则不要这样做。