我想执行和API调用,有一个使用Guzzle的数组。
在我的诊断中,我在对数组字符[和]进行转义时遇到了一个问题,url应该是这样的。
https://url-to-app.app/api/v1/resource?api_key=123456789&user_id=123&status[]=New&status[]=In%20Progress
但是URL如下所示
https://url-to-app.app/api/v1/resource?api_key=123456789&user_id=123&status%5B1%5D=New&status%5B1%5D=In%20Progress
我不确定我是否做错了什么,或者是否有解决方法(也许这是一个特性?)但这是我的代码。
$not_complete = [
'New',
'In Progress',
'Waiting for Parts',
'Waiting on Customer',
'Scheduled',
'Customer Reply',
'Parts to be Ordered',
'To be Delivered',
'To be Contacted'
];
$user_id = 123;
$res = $client->request('GET', 'https://url-to-app.app/api/v1/resource', [
'query' => [
'api_key' => '123456789',
'user_id' => $user_id,
'status' => $not_complete
]
]);
$tickets = json_decode($res->getBody());发布于 2016-04-08 20:24:31
请注意,这是未经测试的
您不需要在查询数组的键中指定方括号。如果你这样做,这个猜谜将自动假设它是纯文本,并且必须进行转义。
'query' => [
'api_key' => '123456789',
'user_id' => $user_id,
'status' => $not_complete // providing this is an array it should work
]尝试上面的方法,让Guzzle为您进行格式化。
https://stackoverflow.com/questions/36499126
复制相似问题