DEV Community

Cover image for Example of fast creation of PHP client for Jira REST API.
alexdodonov
alexdodonov

Posted on • Updated on

Example of fast creation of PHP client for Jira REST API.

Hi! In this article I want to demostrate the way you can create PHP clients to the REST APIs. And the example will be for Jira REST API.
First of all I shall use my package wich will help me in this quest.

First of all we need class wich will represent connection to tha API. IT will not be a real connection but it will contain all necessary information for performing requests to the API:

class Connection
{

    private $connection = null;

    /**
     * Connection to the Jira REST API
     *
     * @param string $url
     *            jira server URL
     * @param string $login
     *            user login
     * @param string $token
     *            access token. See this page https://confluence.atlassian.com/cloud/api-tokens-938839638.html for more information
     */
    public function __construct(string $url = '', string $login = '', string $token = '')
    {
        if ($url !== '') {
            $this->connect($url, $login, $token);
        }
    }

    /**
     * Connection to the Jira REST API
     *
     * @param string $url
     *            jira server URL
     * @param string $login
     *            user login
     * @param string $token
     *            access token. See this page https://confluence.atlassian.com/cloud/api-tokens-938839638.html for more information
     */
    public function connect(string $url, string $login, string $token): void
    {
        $this->connection = new CustomClient($url, [
            'Authorization: Basic ' . base64_encode($login . ':' . $token)
        ]);
    }

    /**
     * Method sends GET request to Jira server
     *
     * @param string $endpoint
     *            endpoint to REST method
     * @return object result
     */
    public function sendGetRequest(string $endpoint): object
    {
        // trait string as JSON object
        return json_decode($this->connection->sendGetRequest($endpoint), false);
    }
}
Enter fullscreen mode Exit fullscreen mode

Quite simple yeah? )

Now let's fetch some projects as a proof that our class works fine:

class ProjectsRepository extends ArrayObject
{

    /**
     * Connection to Jira
     *
     * @var Connection
     */
    private $connection = null;

    /**
     * Constructor
     *
     * @param Connection $connection
     *            connection to Jira
     */
    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }

    /**
     * Method loads list of projects from server
     */
    public function loadProjects(): void
    {
        // initial setup
        $result = [];
        $startAt = 0;

        // main loop
        do {
            // reading data
            $page = $this->connection->sendGetRequest('/project/search/?startAt=' . $startAt);

            // and here we read all projects in loop
        } while (! $page->isLast);
    }
}
Enter fullscreen mode Exit fullscreen mode

As I've said earlier it is quite easy )

PS you can download the full package with the example of Jira Client via this link.

Learn more

More information can be found here:

Twitter
Mezon Framework

Top comments (0)