M
M
Mikhail Solovyov2017-07-28 18:42:32
PHP
Mikhail Solovyov, 2017-07-28 18:42:32

How to work with JSON-RPC 2.0?

Hello. I need to call the calculateShipping function from the API, but I don't understand how to implement it. Here is the API:
https://shiptor.ru/doc/
As I understand it, the JSON-RPC 2.0 standard is used. To do this, I connected the library:
https://github.com/fguillot/JsonRPC
1). How can I pass the headers?

Content-Type: application/json
x-authorization-token: your_API_key

2). How should I pass parameters?
I will be very grateful for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Immortal_pony, 2017-07-28
@Mihairu

$apiUrl = "https://api.shiptor.ru/public/v1";
$apiKey = "YOUR_KEY";
$client = new \JsonRPC\Client($apiUrl);

// Set headers
$client->getHttpClient()->withBeforeRequestCallback(function(HttpClient $client, $payload) {
    $client->withHeaders([
        "Content-Type: application/json",
        "x-authorization-token: {$apiKey}"
    ]);
});

// Fetch something
$method = "calculateShipping";
$params = ['paramName'=>"paramValue"];
$result = $client->execute($method, $params);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question