Skip to content

Kirby 4.4.1

Remote::request()

Static method to init this class and send a request

Remote::request(string $url, array $params = [ ]): Kirby\Http\Remote

Parameters

Name Type Default
$url * string
$params array [ ]

Return type

Kirby\Http\Remote

Exceptions

Type Description
Exception when the curl request failed

Parent class

Kirby\Http\Remote

Params array

Default parameters:

Key Type Default Description
agent string null The user agent string to be sent with the HTTP request
basicAuth string null User name and password to use for the Authorization header (formatted as USERNAME:PASSWORD)
body bool true When true returns transfer as string instead of outputting it directly
ca int|bool|string Remote::CA_INTERNAL TLS CA to use, see details
data array|string [] The data to be sent with the request
encoding string utf-8 Accepted values: null, '', identity, gzip, br
file string null Path to file to be uploaded
headers array [] Array of headers to be sent with the request
method string GET HTTP method (GET, POST, PUT, PATCH, DELETE, HEAD)
progress callback null A callback which accepts 5 parameters, if you want to handle upload/download progress
test bool false When set to true, the cURL request is not initiated, and the Remote object returned
timeout int 10 Request/connect timeout

General examples

GET request

<?php
$targetPage = page('notes')->children()->first();

$response = Remote::request(
    'http://placekitten.com/200/300',
    [
        'method' => 'GET',
    ]
);

// Save content from request to file
if ($response->code() === 200) {
    F::write($targetPage->root() . '/kitten.jpg', $response->content());
}

More example see Remote::get()

You can also find examples in other parts of our documentation:

Kirby API Examples

Note that for the API examples to work, HTTP Basic auth must be enabled in your config.

If you are testing this locally without HTTPS enabled, you also have to set the allowInsecure config option.

GET request

<?php
$email    = 'test@example.com';
$password = 'topsecret';

$response = Remote::request('https://yoursite.com/api/pages/notes', [
    'method' => 'GET',
    'headers' => [
        'Authorization: Basic ' . base64_encode($email . ':' . $password)
    ],
]);

if ($response->code() === 200) {
    $data = json_decode($response->content())->data;
    dump($data);
}

POST request

<?php
$email    = 'test@example.com';
$password = 'topsecret';
$data     = [
    'slug'     => Str::slug(Str::random(10, 'alpha')),
    'template' => 'note',
    'model'    => 'note',
    'isDraft'  => false,
    'content'  => [
        'title' => Str::random(40, 'alpha'),
    ]
];
$request = Remote::request('https://yoursite.com/api/pages/notes/children', [
    'method'  => 'POST',
    'headers' => [
        'Content-Type: application/json',
        'Authorization: Basic ' . base64_encode($email . ':' . $password)
    ],
    'data'    => json_encode($data),
]);

if ($request->code() !== 200) {
    echo 'An error occurred: ' . $request->code();
}

PATCH

<?php
$email    = 'test@example.com';
$password = 'topsecret';
$data     = [
    'title' => Str::random(40, 'alpha'),
];
$response = Remote::request('https://yoursite.com/api/pages/notes+exploring-the-universe', [
    'method'  => 'PATCH',
    'headers' => [
        'Content-Type: application/json',
        'Authorization: Basic ' . base64_encode($email . ':' . $password)
    ],
    'data'    => json_encode($data),
]);

dump($response);

DELETE

<?php
$email    = 'test@example.com';
$password = 'topsecret';

$response = Remote::request('https://yoursite.com/api/pages/notes+exploring-the-universe/files/dark-forest.jpg', [
    'method'  => 'DELETE',
    'headers' => [
        'Authorization: Basic ' . base64_encode($email . ':' . $password)
    ],
]);

dump($response);