SIP.php 1.21 KB
Newer Older
okhin's avatar
okhin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

use \Firebase\JWT\JWT;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\RequestException;

class SIP extends Controller {
    function beforeRoute($f3, $args) {
        // Include configuration
        require_once('config.php');
    }

    function afterRoute($f3, $args) {
        // We return only JSON, so no templates
okhin's avatar
okhin committed
17
        echo $f3->get('response');
okhin's avatar
okhin committed
18 19 20 21 22 23 24 25 26 27 28 29
    }
    // Getting a call from the SIP_API
    function feedbacksip($f3, $args) {
        $token = JWT::encode(array('api' => JWT_TOKEN), JWT_KEY);
        $call_id = $args['callid'];
        $data = array('api' => JWT_TOKEN,
            'token' => $token
        );
        $client = new GuzzleHttp\Client(['base_uri' => SIP_API]);
        try {
            $token = JWT::encode(array('api' => JWT_TOKEN), JWT_KEY);
            $res = $client->get("calls/$call_id", ['query' => $data]);
okhin's avatar
okhin committed
30
            $f3->set('response', (GuzzleHttp\Psr7\copy_to_string($res->getBody())));
okhin's avatar
okhin committed
31 32
        } catch (RequestException $e) {
            if ($e->hasResponse()) {
okhin's avatar
okhin committed
33
                $f3->set('response', Psr7\str($e->getResponse()));
okhin's avatar
okhin committed
34
            }
okhin's avatar
okhin committed
35
            $f3->set('response', Psr7\str($e->getRequest()));
okhin's avatar
okhin committed
36 37 38
        }
    }
}