Skip to content

Composer basics

Camilo Sperberg edited this page Jun 21, 2017 · 10 revisions

What is Composer?

Composer is a kind-of package manager that installs packages should you need them.

What is Packagist?

It is the default repository of Composer.

Installing composer

Please check https://getcomposer.org/ for more information on that. The authors of composer should have a pretty decent how-to, way better than what I could write.

How do I install this project?

A basic example to install the Telegram API would be to create a new project. Then, require the package and finally execute the install.

mkdir ~/myProject
cd ~/myProject
composer.phar require unreal4u/telegram-api:~2.4

This will install the PHP 7 Telegram Bot API with all its dependencies within a vendor folder. To be able to use it, create an example.php file with the following:

<?php

declare(strict_types = 1);

include 'vendor/autoload.php';

use GuzzleHttp\Exception\ClientException;
use unreal4u\TelegramAPI\TgLog;
use unreal4u\TelegramAPI\Telegram\Methods\GetMe;

$tgLog = new TgLog(BOT_TOKEN);
try {
    $response = $tgLog->performApiRequest(new GetMe());
    echo '<pre>';
    var_dump($response);
    echo '</pre>';
} catch (ClientException $e) {
    // Do whatever you want, function below contains exact JSON output from Telegram
    echo 'Exception catched, error is: <pre>';
    print_r(json_decode((string)$e->getResponse()->getBody()));
    echo '</pre>';
}

Don't forget to define the constant BOT_TOKEN with your actual token.

If you get some information back, congratulations! You are ready to use this package as you please.

Clone this wiki locally