cURL helper for PHP

1.1.0 2019-12-08 21:33 UTC

This package is auto-updated.

Last update: 2024-11-09 14:47:55 UTC


README

cURL helper for PHP

Build Status Latest Stable Version Total Downloads License

Instalation

  1. Download using composer
composer require wernerdweight/curler
  1. Use in your project
use WernerDweight\Curler\Curler;
use WernerDweight\Curler\Request;

$curler = new Curler();
$request = (new Request())
    ->setEndpoint('https://some-website.tld')
    ->setMethod('POST')
    ->setPayload(['key' => 'value'])
    ->setHeaders(['Accept: text/html', 'Accept-Encoding: gzip'])
    ->setAuthentication('user', 'password')
;
$response = $curler->request($request);
echo $response->text();  // '<html>...</html>'
var_dump($response->getMetaData()); // array of response metadata (content-type, status...)

API

Curler

  • request(Request $request): Response
    Allows to fetch data according to given $request.

Request

  • setEndpoint(string $endpoint): self
  • getEndpoint(): ?string
  • setMethod(string $method): self
  • getMethod(): ?string
  • setPayload(array $payload): self
  • getPayload(): ?array
  • setHeaders(array $headers): self
  • addHeader(string $header): self
  • removeHeader(string $header): bool
  • getHeaders(): ?array
  • setAuthentication(string $user, string $password): self
  • getAuthentication(): ?array
  • setBearerAuthorization(string $token): self

Response

  • getMetaData(): WernerDweight\RA\RA
    cURL info (see here).
  • ok(): bool
    Returns a boolean stating whether the response was successful (status in the range 200-299) or not.
  • redirected(): bool
    Returns whether or not the response is the result of a redirect; that is, redirect count is more than zero.
  • status(): int
    Returns the status code of the response (e.g., 200 for a success).
  • contentType(): string
    Returns the content type of the response (e.g., text/html).
  • url(): string
    Returns the URL of the response.
  • text(): string
    Returns the response as text.
  • json(): WernerDweight\RA\RA Returns the response as RA (see here).