SIP.php 1.15 KB
Newer Older
okhin's avatar
okhin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
<?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
        return;
    }
    // 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]);
            return (GuzzleHttp\Psr7\copy_to_string($res->getBody()));
        } catch (RequestException $e) {
            if ($e->hasResponse()) {
                return Psr7\str($e->getResponse());
            }
            return Psr7\str($e->getRequest());
        }
    }
}