使用Guzzle删除带参数的请求

时间:2015-09-28 21:13:25

标签: php codeigniter curl guzzle http-delete

我必须在CodeIgnitor平台中使用参数执行DELETE请求。首先,我尝试使用cURL,但我切换到了Guzzle。

控制台中的请求示例如下:

curl -X DELETE -d '{"username":"test"}' http://example.net/resource/id

但是在Guzzle的文档中,他们像GET一样使用参数,比如DELETE http://example.net/resource/id?username=test,我不想这样做。

我尝试过:

$client = new GuzzleHttp\Client();
$client->request('DELETE', $url, $data);

但请求只调用DELETE http://example.com/resource/id而没有任何参数。

3 个答案:

答案 0 :(得分:6)

如果我正确解释了你的curl请求,你试图发送json数据作为删除请求的正文。

// turn on debugging mode.  This will force guzzle to dump the request and response.
$client = new GuzzleHttp\Client(['debug' => true,]);

// this option will also set the 'Content-Type' header.
$response = $client->delete($uri, [
    'json' => $data,
]);

答案 1 :(得分:2)

在得到同样的问题后,这个问题迟到了。 避免调试模式的首选解决方案是将'query'中的params传递为:

$response = $client->request('DELETE', $uri, ['query' => $datas]);

$ datas是一个数组 Guzzle V6

答案 2 :(得分:0)

    $response = json_decode($this->client->delete($uri,$params)->getStatusCode());        
    echo $response;

这还将给出响应状态为204或404

相关问题