我想要使用Guzzle执行一个具有数组的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());
请注意,这是未经测试的
您不需要在查询数组的键中指定方括号。如果你这样做,Guzzle会自动假设它是纯文本,并且必须转义。
'query' => [
'api_key' => '123456789',
'user_id' => $user_id,
'status' => $not_complete // providing this is an array it should work
]
试试上面的,让Guzzle为你做格式化。