1

I am trying to call a curl request for delete method using Magento\Framework\HTTP\Client\Curl.php.

In this class makeRequest method is responsible for sending curl request. But as I can see that this method is protected so I can't call it directly.

makeRequest method in Magento\Framework\HTTP\Client\Curl.php

/**
 * Make request
 * @param string $method
 * @param string $uri
 * @param array $params
 * @return void
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 * @SuppressWarnings(PHPMD.NPathComplexity)
 */
protected function makeRequest($method, $uri, $params = [])
{
    $this->_ch = curl_init();
    $this->curlOption(CURLOPT_URL, $uri);
    if ($method == 'POST') {
        $this->curlOption(CURLOPT_POST, 1);
        $this->curlOption(CURLOPT_POSTFIELDS, http_build_query($params));
    } elseif ($method == "GET") {
        $this->curlOption(CURLOPT_HTTPGET, 1);
    } else {
        $this->curlOption(CURLOPT_CUSTOMREQUEST, $method);
    }

    if (count($this->_headers)) {
        $heads = [];
        foreach ($this->_headers as $k => $v) {
            $heads[] = $k . ': ' . $v;
        }
        $this->curlOption(CURLOPT_HTTPHEADER, $heads);
    }

    if (count($this->_cookies)) {
        $cookies = [];
        foreach ($this->_cookies as $k => $v) {
            $cookies[] = "{$k}={$v}";
        }
        $this->curlOption(CURLOPT_COOKIE, implode(";", $cookies));
    }

    if ($this->_timeout) {
        $this->curlOption(CURLOPT_TIMEOUT, $this->_timeout);
    }

    if ($this->_port != 80) {
        $this->curlOption(CURLOPT_PORT, $this->_port);
    }

    //$this->curlOption(CURLOPT_HEADER, 1);
    $this->curlOption(CURLOPT_RETURNTRANSFER, 1);
    $this->curlOption(CURLOPT_HEADERFUNCTION, [$this, 'parseHeaders']);

    if (count($this->_curlUserOptions)) {
        foreach ($this->_curlUserOptions as $k => $v) {
            $this->curlOption($k, $v);
        }
    }

    $this->_headerCount = 0;
    $this->_responseHeaders = [];
    $this->_responseBody = curl_exec($this->_ch);
    $err = curl_errno($this->_ch);
    if ($err) {
        $this->doError(curl_error($this->_ch));
    }
    curl_close($this->_ch);
}

I have to call one of public method of this class which is calling makeRequest method. When I check this I can see 2 public methods for GET and POST request which are calling protected method makeRequest. No other method for DELETE which is calling method makeRequest.

/**
 * Make GET request
 *
 * @param string $uri uri relative to host, ex. "/index.php"
 * @return void
 */
public function get($uri)
{
    $this->makeRequest("GET", $uri);
}

/**
 * Make POST request
 *
 * @param string $uri
 * @param array $params
 * @return void
 *
 * @see \Magento\Framework\HTTP\Client#post($uri, $params)
 */
public function post($uri, $params)
{
    $this->makeRequest("POST", $uri, $params);
}

Now my question is that how can I solve my problem or is this bug in magento 2.1.7 or may be I am getting incorrectly. Please help me if I am doing anything wrong.

class reference curl.php

1

Use this object instead:

$request = new \Zend\Http\Request();

Then set the method to

$request->setMethod(\Zend\Http\Request::METHOD_DELETE);

For more information, read the tutorial in the following link: http://devdocs.magento.com/guides/v2.1/get-started/gs-web-api-request.html

4
  • hello @Toan Tam, I am not sure if we can use it for curl request ? Aug 19 '17 at 12:24
  • I think it is for rest api calls. Aug 19 '17 at 12:27
  • The main purpose of curl request is to make a http request. And the \Zend\Http\Request() class is suitable in this case.
    – Toan Tam
    Aug 19 '17 at 12:46
  • ok then let me try Aug 19 '17 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.