librestful is a virion for PocketMine servers that make easier, readable code and for async http requests.

Related tags

HTTP librestful
Overview

librestful

librestful is a virion for PocketMine servers that make easier, readable code for async rest requests.

Example

"Bearer API_KEY" ] ); $client->post() ->endpoint("player/ban") ->field("username", "eren5960") ->field("reason", "hack") ->result(fn(Response $result) => var_dump($result)) ->fail(fn(RequestErrorException $error) => var_dump($error)) ->async(); ">
use redmc\librestful\exceptions\RequestErrorException;
use redmc\librestful\librestful;
use redmc\librestful\Response;

$client = librestful::create(
    $plugin,
    "http://api.redmc.me",
    [
        "Authorization" => "Bearer API_KEY"
    ]
);

$client->post()
    ->endpoint("player/ban")

    ->field("username", "eren5960")
    ->field("reason", "hack")

    ->result(fn(Response $result) => var_dump($result))
    ->fail(fn(RequestErrorException $error) => var_dump($error))

    ->async();

Usage

Requirements

  • PHP 7.4+
  • PocketMine 4.0.0 API
  • Devirion

Download

Download files and put to virions/ directory.

GET Requests

"Bearer API_KEY"], // addinational headers for all requests 2, // thread count true // logging requests ); $player = Player; // a player $get = $client->get() ->endpoint("player/can_access/" . $playerName) //endpoint ->param("server", "SkyBlock") // one parameter usage ->params([ "response" => "json", "param2" => "param2" ]) // multiple parameter usage ->result(function(Response $result) use($player){ if (!$player->isOnline()) return; if ($result->code() === Status::UNAUTHORIZED) { // unauthorized, this an example $player->kick("unauthorized"); } }) // handle result ->fail(fn(RequestErrorException $error) => var_dump($error)) // handle error ->finally(fn() => var_dump("It's over.")) // on finish ->header("Cookie", "Key=value") // one header usage ->headers(["Connection" => "keep-alive"]) // multiple headers usage ->timeout(5); // timeout, default 10 $get->async(); // async run $client->waitAll(); // wait to finish requests //or $get->run(); // sync run (wait compilation) ">
// import
use redmc\librestful\exceptions\RequestErrorException;
use redmc\librestful\librestful;
use redmc\librestful\Response;
use redmc\librestful\Status;

// create librestful client
$client = librestful::create(
    $plugin, // plugin instance (\pocketmine\plugin\Plugin)
    "http://api.redmc.me", // base url 
    ["Authorization" => "Bearer API_KEY"], // addinational headers for all requests
    2, // thread count
    true // logging requests
);

$player = Player; // a player
$get = $client->get()
    ->endpoint("player/can_access/" . $playerName) //endpoint

    ->param("server", "SkyBlock") // one parameter usage
    ->params([
        "response" => "json",
        "param2" => "param2"
    ]) // multiple parameter usage

    ->result(function(Response $result) use($player){
        if (!$player->isOnline()) return;
        
        if ($result->code() === Status::UNAUTHORIZED) { // unauthorized, this an example
            $player->kick("unauthorized");
        }
    }) // handle result
    ->fail(fn(RequestErrorException $error) => var_dump($error)) // handle error
    ->finally(fn() => var_dump("It's over.")) // on finish

    ->header("Cookie", "Key=value") // one header usage
    ->headers(["Connection" => "keep-alive"]) // multiple headers usage

    ->timeout(5); // timeout, default 10

$get->async(); // async run
$client->waitAll(); // wait to finish requests
//or
$get->run(); // sync run (wait compilation)

POST Requests

"Bearer API_KEY"], // addinational headers for all requests 2, // thread count true // logging requests ); // post $post = $client->post() ->endpoint("player/ban") //endpoint ->field("username", "eren5960") // one field usage ->fields([ "reason" => "hack", "time" => strtotime("+1 months") ]) // multiple field usage ->header("Cookie", "Key=value") // one header usage ->headers(["Connection" => "keep-alive"]) // multiple headers usage ->result(fn(Response $result) => var_dump($result)) // handle result ->fail(fn(RequestErrorException $error) => var_dump($error)) // handle error ->finally(fn() => var_dump("It's over.")) // on finish ->timeout(5); // timeout, default 10 $post->async(); // async run $client->waitAll(); // wait to finish requests //or $post->run(); // sync run (wait compilation) ">
// import
use redmc\librestful\exceptions\RequestErrorException;
use redmc\librestful\librestful;
use redmc\librestful\Response;

// create librestful client
$client = librestful::create(
    $plugin, // plugin instance (\pocketmine\plugin\Plugin)
    "http://api.redmc.me", // base url 
    ["Authorization" => "Bearer API_KEY"], // addinational headers for all requests
    2, // thread count
    true // logging requests
);

// post
$post = $client->post()
    ->endpoint("player/ban") //endpoint

    ->field("username", "eren5960") // one field usage
    ->fields([
        "reason" => "hack",
        "time" => strtotime("+1 months") 
    ]) // multiple field usage

    ->header("Cookie", "Key=value") // one header usage
    ->headers(["Connection" => "keep-alive"]) // multiple headers usage

    ->result(fn(Response $result) => var_dump($result)) // handle result
    ->fail(fn(RequestErrorException $error) => var_dump($error)) // handle error
    ->finally(fn() => var_dump("It's over.")) // on finish

    ->timeout(5); // timeout, default 10

$post->async(); // async run
$client->waitAll(); // wait to finish requests
//or
$post->run(); // sync run (wait compilation)

More Examples

Minecraft Pocket Server Get List of Voters

get() ->param("object", "servers") ->param("element", "voters") ->param("key", "Your API Key") ->param("month", "current") ->param("format", "json") ->result(fn(Response $response) => var_dump(json_decode($response->body(), true))) ->fail(fn(RequestErrorException $error) => var_dump("unable to get monthly voters: " . $error->getMessage())) ->timeout(15) ->async(); ">
use redmc\librestful\exceptions\RequestErrorException;
use redmc\librestful\librestful;
use redmc\librestful\Response;

$client = librestful::create($plugin, "https://minecraftpocket-servers.com/api");

$client
    ->get()

    ->param("object", "servers")
    ->param("element", "voters")
    ->param("key", "Your API Key")
    ->param("month", "current")
    ->param("format", "json")

    ->result(fn(Response $response) => var_dump(json_decode($response->body(), true)))
    ->fail(fn(RequestErrorException $error) => var_dump("unable to get monthly voters: " . $error->getMessage()))

    ->timeout(15)
    ->async();
You might also like...
Declarative HTTP Clients using Guzzle HTTP Library and PHP 8 Attributes

Waffler How to install? $ composer require waffler/waffler This package requires PHP 8 or above. How to test? $ composer phpunit Quick start For our e

A library that makes the management of WordPress file headers easier.

Pronamic WordPress File Header Many WordPress plugins contain bash scripts with sed and awk commands to update WordPress file headers. Because sed and

Simple handler system used to power clients and servers in PHP (this project is no longer used in Guzzle 6+)

RingPHP Provides a simple API and specification that abstracts away the details of HTTP into a single PHP function. RingPHP be used to power HTTP clie

Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.
Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests.

This is a port of the VCR Ruby library to PHP. Record your test suite's HTTP interactions and replay them during future test runs for fast, determinis

A simple PHP Toolkit to parallel generate combinations, save and use the generated terms to brute force attack via the http protocol.
A simple PHP Toolkit to parallel generate combinations, save and use the generated terms to brute force attack via the http protocol.

Brutal A simple PHP Toolkit to parallel generate combinations, save and use the generated terms to apply brute force attack via the http protocol. Bru

Pushpin is a publish-subscribe server, supporting HTTP and WebSocket connections.
Pushpin is a publish-subscribe server, supporting HTTP and WebSocket connections.

Pushpin and 1 million connections Pushpin is a publish-subscribe server, supporting HTTP and WebSocket connections. This repository contains instructi

Composer package providing HTTP Methods, Status Codes and Reason Phrases for PHP

HTTP Enums For PHP 8.1 and above This package provides HTTP Methods, Status Codes and Reason Phrases as PHP 8.1+ enums All IANA registered HTTP Status

Event-driven, streaming HTTP client and server implementation for ReactPHP

HTTP Event-driven, streaming HTTP client and server implementation for ReactPHP. This HTTP library provides re-usable implementations for an HTTP clie

A simple yet powerful HTTP metadata and assets provider for NFT collections using Symfony

Safe NFT Metadata Provider A simple yet powerful HTTP metadata and assets provider for NFT collections using Symfony.

Releases(v1)
Owner
RedMC Network
We are making game servers for Minecraft: Bedrock Edition.
RedMC Network
Async HTTP/1.1+2 client for PHP based on Amp.

This package provides an asynchronous HTTP client for PHP based on Amp. Its API simplifies standards-compliant HTTP resource traversal and RESTful web

AMPHP 641 Dec 19, 2022
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.

Requests for PHP Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python librar

null 3.5k Dec 31, 2022
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.

Requests for PHP Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python librar

null 3.5k Dec 31, 2022
PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs

PHP Curl Class: HTTP requests made easy PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs. Installation Requirements Quic

null 3.1k Jan 5, 2023
Application for logging HTTP and DNS Requests

Request Logger Made by Adam Langley ( https://twitter.com/adamtlangley ) What is it? Request logger is a free and open source utility for logging HTTP

null 13 Nov 28, 2022
🐼 Framework agnostic package using asynchronous HTTP requests and PHP generators to load paginated items of JSON APIs into Laravel lazy collections.

Framework agnostic package using asynchronous HTTP requests and generators to load paginated items of JSON APIs into Laravel lazy collections.

Andrea Marco Sartori 61 Dec 3, 2022
↪️ Bypass for PHP creates a custom HTTP Server to return predefined responses to client requests

Bypass for PHP provides a quick way to create a custom HTTP Server to return predefined responses to client requests.Useful for tests with Pest PHP or PHPUnit.

CiaReis 101 Dec 1, 2022
Provides an easy interface for performing Hyper-Text Transfer Protocol (HTTP) requests

laminas-http provides the HTTP message abstraction used by laminas-mvc, and also provides an extensible, adapter-driven HTTP client library.

Laminas Project 33 Aug 27, 2022
Requests - a HTTP library written in PHP, for human beings

Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python library. Requests is ISC Licensed (similar to the new BSD license) and has no dependencies, except for PHP 5.6+.

WordPress 3.5k Jan 6, 2023
PHP Curl - This package can send HTTP requests to a given site using Curl.

PHP Curl This package can send HTTP requests to a given site using Curl. It provides functions that can take several types of parameters to configure

Mehmet Can 1 Oct 27, 2022