Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Curl.php #7405

Merged
merged 2 commits into from
Jun 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/internal/Magento/Framework/HTTP/Adapter/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,14 @@ public function write($method, $url, $http_ver = '1.1', $headers = [], $body = '
curl_setopt($this->_getResource(), CURLOPT_RETURNTRANSFER, true);
if ($method == \Zend_Http_Client::POST) {
curl_setopt($this->_getResource(), CURLOPT_POST, true);
curl_setopt($this->_getResource(), CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->_getResource(), CURLOPT_POSTFIELDS, $body);
} elseif ($method == \Zend_Http_Client::PUT) {
curl_setopt($this->_getResource(), CURLOPT_CUSTOMREQUEST, 'PUT');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a CURLOPT_PUT constant and with PUT requests you should be using CURLOPT_INFILE and CURLOPT_INFILESIZE.

Copy link
Contributor Author

@redelschaap redelschaap Nov 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CURLOPT_PUT option was deprecated a couple versions of cURL ago (source), but the php.net documentation hasn't been updated yet. We verified this; when using the CURLOPT_PUT option, cURL was using GET instead of PUT.

Also, a lot APIs use the PUT method for other purposes than uploading files (only), for example to update an object rather than creating one. In that case CURLOPT_POSTFIELDS should be used. If uploading files via PUT requests does not use the CURLOPT_POSTFIELDS option, the function Curl::write() method should be modified imho, e.g. with an extra parameter.

I haven't tested this myself, but I think CURLFile can be used in combination with PUT requests too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stand corrected as to the deprecation, but in that case the recommendation is to use CURLOPT_UPLOAD, it will automatically assume the HTTP PUT method unless otherwise specified, and then you then still have the ability to handle CURLOPT_INFILESIZE or CURLOPT_INFILESIZE_LARGE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but keep in mind that PUT has other use cases than uploading files only.

curl_setopt($this->_getResource(), CURLOPT_POSTFIELDS, $body);
} elseif ($method == \Zend_Http_Client::GET) {
curl_setopt($this->_getResource(), CURLOPT_HTTPGET, true);
curl_setopt($this->_getResource(), CURLOPT_CUSTOMREQUEST, 'GET');
}

if (is_array($headers)) {
Expand Down