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

Allow cURL to save a url's contents to a destination file pointer (available via the CURLOPT_FILE option). #17187

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 31 additions & 2 deletions libraries/src/Http/Transport/CurlTransport.php
Expand Up @@ -188,6 +188,14 @@ public function request($method, Uri $uri, $data = null, array $headers = null,
foreach ($this->options->get('transport.curl', array()) as $key => $value)
{
$options[$key] = $value;

if ($key == CURLOPT_FILE && (bool) $value)
{
$this->headers = '';

$options[CURLOPT_HEADER] = false;
$options[CURLOPT_HEADERFUNCTION] = array($this, 'getHeaders');
}
}

// Authentification, if needed
Expand All @@ -203,8 +211,8 @@ public function request($method, Uri $uri, $data = null, array $headers = null,
// Execute the request and close the connection.
$content = curl_exec($ch);

// Check if the content is a string. If it is not, it must be an error.
if (!is_string($content))
// Check if the content is a string or is boolean true. If it is not, it must be an error.
if (!is_string($content) && !is_bool($content))
{
$message = curl_error($ch);

Expand All @@ -223,6 +231,12 @@ public function request($method, Uri $uri, $data = null, array $headers = null,
// Close the connection.
curl_close($ch);

// If headers are set append to the content.
if (isset($this->headers))
{
$content = $this->headers . (is_bool($content) ? "" : $content);
}

$response = $this->getResponse($content, $info);

// Manually follow redirects if server doesn't allow to follow location using curl
Expand Down Expand Up @@ -367,4 +381,19 @@ private function redirectsAllowed()

return false;
}

/**
* Gets each header returned by cURL, appending it to the headers string.
*
* @param resource $ch The cURL resource.
* @param string $header The header string.
*
* @return int The length of the header.
*/
Comment on lines +390 to +392

This comment was marked as abuse.

private function getHeaders($ch, $header)
{
$this->headers .= $header;

return strlen($header);
}
}