You are here

class PhpCurlHttpClient in Backup and Migrate 8.4

Class PhpCurlHttpClient.

@package BackupMigrate\Core\Service

Hierarchy

Expanded class hierarchy of PhpCurlHttpClient

File

lib/backup_migrate_core/src/Service/PhpCurlHttpClient.php, line 13

Namespace

BackupMigrate\Core\Service
View source
class PhpCurlHttpClient implements HttpClientInterface {

  /**
   * Get the body of the given resource.
   *
   * @param $url
   *
   * @return mixed
   */
  public function get($url) {

    // @TODO: Implement if needed.
  }

  /**
   * Post the given data (as a string or an array) to the given URL.
   *
   * @param $url
   * @param $data
   *
   * @return mixed
   */
  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);
  }

  /**
   * Post a file along with other data (as an array).
   *
   * @param $url
   * @param \BackupMigrate\Core\File\ReadableStreamBackupFile $file
   * @param $data
   *
   * @return mixed
   */
  public function postFile($url, ReadableStreamBackupFile $file, $data) {
    $data['file'] = new \CURLFile($file
      ->realpath());
    $data['file']
      ->setPostFilename($file
      ->getFullName());
    return $this
      ->post($url, $data);
  }

  /**
   * Get the CURL Resource with default options.
   *
   * @param $url
   *
   * @return resource
   */
  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;
  }

  /**
   * Perform the http action and return the body or throw an exception.
   *
   * @param $ch
   *
   * @return mixed
   *
   * @throws \BackupMigrate\Core\Exception\HttpClientException
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpCurlHttpClient::curlExec protected function Perform the http action and return the body or throw an exception.
PhpCurlHttpClient::get public function Get the body of the given resource. Overrides HttpClientInterface::get
PhpCurlHttpClient::getCurlResource protected function Get the CURL Resource with default options.
PhpCurlHttpClient::post public function Post the given data (as a string or an array) to the given URL. Overrides HttpClientInterface::post
PhpCurlHttpClient::postFile public function Post a file along with other data (as an array). Overrides HttpClientInterface::postFile