PhpCurlHttpClient.php in Backup and Migrate 5.0.x
File
src/Core/Service/PhpCurlHttpClient.php
View source
<?php
namespace Drupal\backup_migrate\Core\Service;
use Drupal\backup_migrate\Core\Exception\HttpClientException;
use Drupal\backup_migrate\Core\File\ReadableStreamBackupFile;
class PhpCurlHttpClient implements HttpClientInterface {
public function get($url) {
}
public function post($url, $data) {
$ch = $this
->getCurlResource($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
return $this
->curlExec($ch);
}
public function postFile($url, ReadableStreamBackupFile $file, $data) {
$data['file'] = new \CURLFile($file
->realpath());
$data['file']
->setPostFilename($file
->getFullName());
return $this
->post($url, $data);
}
protected function getCurlResource($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
return $ch;
}
protected function curlExec($ch) {
$body = curl_exec($ch);
if ($msg = curl_error($ch)) {
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (!$code) {
$info['code'] = curl_errno($ch);
}
throw new HttpClientException($msg, [], $code);
}
return $body;
}
}