如何发送参数与删除请求在zend框架2..


How to send parameters with DELETE request in zend framework 2..?

我在Zend框架2中有一个函数,它将CURL requests发送到API并返回如下结果:

use Zend'Http'Client as HttpClient;
public function curl($url, array $params, $method = "POST"){
    $client = new HttpClient();
    $client->setAdapter('Zend'Http'Client'Adapter'Curl');
    $client->setUri($url);
    $client->setOptions(array(
        'maxredirects' => 0,
        'timeout' => 30
    ));
    $client->setMethod($method);
    $client->setHeaders(array(
        'username: xxxxxxx',
        'password: xxxxxxx',
    ));
    //if(!empty($params)) {
    if ($method == "POST" || $method == "PUT") {
        $client->setParameterPOST($params);
    } else {
        $client->setParameterGET($params);
    }
    //}
    $response = $client->send();
    return $response;
}

并调用为:

 $response = $this->api->curl($api_url, array('paramName' => "value"), "DELETE");

但是它无法发送参数与请求和API返回500内部服务器错误异常

要完成此操作,请在第396行更新文件Zend'Http'Client'Adapter'Curl.php,请添加另一个elseif语句:

elseif ($method == 'DELETE') {
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
    }

那么它就会变成:

if ($method == 'POST') {
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
    } elseif ($curlMethod == CURLOPT_UPLOAD) {
        // this covers a PUT by file-handle:
        // Make the setting of this options explicit (rather than setting it through the loop following a bit lower)
        // to group common functionality together.
        curl_setopt($this->curl, CURLOPT_INFILE, $this->config['curloptions'][CURLOPT_INFILE]);
        curl_setopt($this->curl, CURLOPT_INFILESIZE, $this->config['curloptions'][CURLOPT_INFILESIZE]);
        unset($this->config['curloptions'][CURLOPT_INFILE]);
        unset($this->config['curloptions'][CURLOPT_INFILESIZE]);
    } elseif ($method == 'PUT') {
        // This is a PUT by a setRawData string, not by file-handle
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
    } elseif ($method == 'PATCH') {
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
    } elseif ($method == 'DELETE') {    
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
    }

,然后更新函数的if语句:

if ($method == "POST" || $method == "PUT" || $method == "DELETE") {
        $client->setParameterPOST($params);
    } else {
        $client->setParameterGET($params);
    }

你的函数看起来像:

公共函数curl($url,数组$params, $method = "POST"){

    $client = new HttpClient();
    $client->setAdapter('Zend'Http'Client'Adapter'Curl');
    $client->setUri($url);
    $client->setOptions(array(
        'maxredirects' => 0,
        'timeout' => 30
    ));
    $client->setMethod($method);
    $client->setHeaders(array(
        'username: xxxxxxx',
        'password: xxxxxxx',
    ));
    //if(!empty($params)) {
    if ($method == "POST" || $method == "PUT" || $method == "DELETE") {
        $client->setParameterPOST($params);
    } else {
        $client->setParameterGET($params);
    }
    //}
    $response = $client->send();
    return $response;
}

它为我工作…!!