Skip to content

Home

Build Status StyleCI Scrutinizer Code Quality Code Coverage Packagist Version Packagist Downloads

PlinkerRPC PHP client/server makes it really easy to link and execute generic PHP components on remote systems, while maintaining the feel of a local method call.

Docs: https://plinker-rpc.github.io/core

New changes in version 3 include:

  • Now compaible with PHP extension.
  • Built-in core components and info method added so components can be discovered.
  • Only one client instance is now needed, made use of __get() to dynamically set component.
  • User defined components/classes, so you can call your own code.
  • Both request and response is encrypted and signed.

Install

Require this package with composer using the following command:

$ composer require plinker/core

Additional Setup

This component does not require any additional setup.

Client

Creating a client instance is done as follows:

<?php
require 'vendor/autoload.php';

/**
 * Initialize plinker client.
 *
 * @param string $server - URL to server listener.
 * @param string $config - server secret, and/or a additional component data
 */
$client = new \Plinker\Core\Client(
    'http://example.com/server.php',
    [
        'secret' => 'a secret password',
    ]
);

// or using global function
$client = plinker_client('http://example.com/server.php', 'a secret password');

Server

Creating a server listener is done as follows:

Optional features:

  • Set a secret, which all clients will require.
  • Lock down to specific client IP addresses for addtional security.
  • Define your own classes in the classes array then access like above $client->class->method(), which can interface out of scope components or composer packages.
  • Define addtional key values for database connections etc, or you could pass the parameters through the client connection.
<?php
require 'vendor/autoload.php';

/**
 * Initialize plinker server.
 */
if (isset($_SERVER['HTTP_PLINKER'])) {
    // init plinker server
    echo (new \Plinker\Server([
        'secret' => 'a secret password',
        'allowed_ips' => [
            '127.0.0.1'
        ],
        'classes' => [
            'test' => [
                // path to file
                'classes/test.php',
                // addtional key/values
                [
                    'key' => 'value'
                ]
            ],
            // you can use namespaced classes
            'Foo\\Demo' => [
                // path to file
                'some_class/demo.php',
                // addtional key/values
                [
                    'key' => 'value'
                ]
            ],
            // ...
        ]
    ]))->listen();
}

Exceptions

Exceptions can be thrown by components, the client will catch them and throw a \Plinker\Core\Exception\Server exception which you can catch as normal.

try {

    $client->component->method();

} catch (\Exception | \Plinker\Core\Exception\Server $e) {
    exit(get_class($e).': '.$e->getMessage());
}

Non exception errors, including failing to decode or verify response will be returned as an array structure.

Array
(
    [body]  => {"body": "", "token": "abc"} # Raw response, for debugging.
    [code]  => 401                          # HTTP error code.
    [error] => "Error message"              # An error message if applicable.
)

Methods

Once setup, you call the class though its namespace to its method.

Info

The info method returns defined endpoint methods and their parameters.

Call

$result = $client->info();

Response

Array
(
    [class] => Array
        (
            [Foo\Demo] => Array
                (
                    [config] => Array
                        (
                            [key] => value
                        )

                    [methods] => Array
                        (
                            [config] => Array
                                (
                                )

                            [this] => Array
                                (
                                )

                            [test] => Array
                                (
                                    [0] => x
                                    [1] => y
                                )

                        )

                )

        )

)

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please contact me via https://cherone.co.uk instead of using the issue tracker.

Credits

Development Encouragement

If you use this code and make money from it and want to show your appreciation, please feel free to make a donation https://www.paypal.me/lcherone, thanks.

Sponsors

Get your company or name listed here.

License

The MIT License (MIT). Please see License File for more information.

See organisations page for additional components.