如何在zend框架2中发送带DELETE请求的参数..?

时间:2014-08-13 10:07:15

标签: php curl zend-framework2

我在Zend Framework 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内部服务器错误和异常

1 个答案:

答案 0 :(得分:1)

要完成此操作,请在第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);
    }

你的功能如下:

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" || $method == "DELETE") {
        $client->setParameterPOST($params);
    } else {
        $client->setParameterGET($params);
    }
    //}

    $response = $client->send();
    return $response;
}

它对我有用.. !!