zored/speech

JSON-RPC 2.0 for PHP. With Symfony 2.0 to 3.0 support.

1.1 2017-04-14 13:50 UTC

This package is not auto-updated.

Last update: 2024-11-24 03:29:51 UTC


README

JSON-RPC 2.0 for PHP

Build Status Latest Stable Version

Bundle for JSON-RPC 2.0. You can use as standalone library. Or with Symfony.

Features

Use

Symfony

  • Install package: composer require zored/speech.
  • Add bundle new Zored\SpeechBundle\ZoredSpeechBundle() to your Symfony kernel app/AppKernel.php.
  • Add route to default endpoint: routing.yml:
    json_rpc:
        defaults: { _controller: ZoredSpeechBundle:Default:jsonRpc }
        methods: [POST] # Or GET for Zored RPC.
        path: /json-rpc
  • Add service with your JSON-RPC methods in services.yml:
    services:
        your.service:
            # Replace with your own class:
            class: 'Zored\SpeechBundle\Test\JsonRpc\Greeter'
  • Allow this service to run in default controller via parameters.yml:
    parameters:
      zored.speech.controller.context.service_ids: ['your.service']
  • Clear caches.
  • Test:
    curl http://example.com/json-rpc/ \
        --data '{"id":"1", "method":"your.service:greet", "params":{"person":{"name":"bob","age":19}},"jsonrpc":"2.0"}'

Standalone library

  • See example in web/index.php
  • Run there server with php -S 127.0.0.1:8080
  • Test:
    curl http://127.0.0.1:8080 \
        --data '{"id":"1", "method":"your.service:greet", "params":{"person":{"name":"bob","age":19}},"jsonrpc":"2.0"}'

What's next?

Inside

  • Your request JSON string goes to endpoint which handles it and returns Symfony response.
  • Endpoint chooses which request passer can pass JSON string to request handler.
    • There are single for {} and batch for [{},{}] passers enabled by default.
  • Request passer also deserializes JSON to object and validates it.
  • Request passer passes request object to request handler that pushes request somewhere to get response.
  • Request then passed back and serialized to array.

Events

You can see available events here.