PhpCurlHttpClient.php in Backup and Migrate 8.4
File
lib/backup_migrate_core/src/Service/PhpCurlHttpClient.php
View source
<?php
namespace BackupMigrate\Core\Service;
use BackupMigrate\Core\Exception\HttpClientException;
use BackupMigrate\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;
}
}