0

I have a function in Zend Framework 2 which sends CURL requests to an API and return results like below :

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;
}

and calling it as :

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

But it is unable to send parameter along with the request and API returning 500 internal server error with Exception.

3
  • If the API is giving you a 500, that suggests an issue on the API side. But you should probably check the API's documentation to see how you should be supplying parameters along with the DELETE request. It's unusual to use parameters with a DELETE, but if you do, normally it would be as part of the request body. Commented Aug 13, 2014 at 12:51
  • I have checked the API and found that it throwing Exception of missing parameters and i need to pass the arguments as i have defined some conditions to delete data according to the arguments passed. Commented Aug 14, 2014 at 8:40
  • Fair enough. Seems weird for the API to return a 500 for missing parameters (400 would be typical), but if it's a third party API that's outside your control. Commented Aug 14, 2014 at 9:11

1 Answer 1

1

To get this done please update file Zend\Http\Client\Adapter\Curl.php on line 396 please add another elseif statement :

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

then it'll look like :

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);
    }

and then update the if statement of your function :

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

your function will look like :

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;
}

It worked for me..!!

Sign up to request clarification or add additional context in comments.

4 Comments

You should never be editing the ZF2 source code within your own application, and the ZF2 curl adapter already supports including a request body with DELETE requests.
To add some background, it looks like this feature was added in 2.3.2 which was only just released. But you can achieve this in earlier versions using other adapters, or by extending the curl adapter to make the modification.
My App is at delivering stage so i can't migrate it to another version and i know ZF2 curl supports delete request but you can try to send the parameters with delete request to check the result. If we talk about editing the library there is nothing bad in this as it is a benefit of using open source that you can update the libraries as per your requirements.
If you just update your library then you can get the DELETE method in the Zend framework using zf1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.