PHP code example of runet-id / api-client-bundle

1. Go to this page and download the library: Download runet-id/api-client-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

runet-id / api-client-bundle example snippets




namespace AppBundle\Controller;

use RunetId\ApiClient\Exception\ApiException;
use RunetId\ApiClient\Model\User;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

/**
 * @Route("/auth")
 */
class AuthController extends Controller
{
    /**
     * @Route("/token", name="auth.token")
     * @param Request $request
     * @return Response
     * @throws HttpException
     */
    public function tokenAction(Request $request)
    {
        $token = $request->query->get('token');

        try {
            // содержит все данные о пользователе, полученные с RunetId
            $apiUser = $this->get('api')->user()->auth($token);
        } catch (ApiException $e) {
            throw new HttpException(403, $e->getMessage());
        }

        // регистрируем пользователя на мероприятие со статусом "Участник"
        $this->get('api')->event()->register($apiUser->RunetId, User\Status::ROLE_PARTICIPANT);

        // здесь авторизуем пользователя средствами Symfony

        return new Response('
            <script>
                window.onunload = function () {
                    window.opener.location.reload();
                };
                setTimeout(window.close, 400);
            </script>
        ');
    }
}