All Projects → yaroslavche → Phptdlib

yaroslavche / Phptdlib

Licence: mit
PHP Extension for tdlib/td written with PHP-CPP

Programming Languages

cpp
1120 projects

Projects that are alternatives of or similar to Phptdlib

Weacast
Weacast demo application
Stars: ✭ 55 (-6.78%)
Mutual labels:  json
Parsrs
CSV, JSON, XML text parsers and generators written in pure POSIX shellscript
Stars: ✭ 56 (-5.08%)
Mutual labels:  json
Http Prompt
An interactive command-line HTTP and API testing client built on top of HTTPie featuring autocomplete, syntax highlighting, and more. https://twitter.com/httpie
Stars: ✭ 8,329 (+14016.95%)
Mutual labels:  json
Jsonapi
[Bolt Extension] JSON API for Bolt CMS
Stars: ✭ 55 (-6.78%)
Mutual labels:  json
Barely json
A Python parser for data that only looks like JSON
Stars: ✭ 56 (-5.08%)
Mutual labels:  json
Diver.js
Dives deep into the dom and dumps it in the object literal notation.
Stars: ✭ 57 (-3.39%)
Mutual labels:  json
Easyjson
Provides an unified JSON access API, you can adapter any JSON library to Gson, Jackson, FastJson with easyjson。 提供了一个JSON门面库,就像slf4j一样。easyjson本身不做json的操作,完全依赖于底层实现库。可以直接使用Easyjson的API,底层的JSON库随时可切换。也可以使用其中某个json的API,然后通过easyjson适配给其他的json库
Stars: ✭ 54 (-8.47%)
Mutual labels:  json
Jsonschema Key Compression
Compress json-data based on its json-schema while still having valid json
Stars: ✭ 59 (+0%)
Mutual labels:  json
Jsmnsol
A JSON parser for solidity
Stars: ✭ 56 (-5.08%)
Mutual labels:  json
Gjson
Get JSON values quickly - JSON parser for Go
Stars: ✭ 9,453 (+15922.03%)
Mutual labels:  json
Gulp Json Editor
A gulp plugin to edit JSON objects
Stars: ✭ 55 (-6.78%)
Mutual labels:  json
Jebena
Lightweight JSON validation library
Stars: ✭ 56 (-5.08%)
Mutual labels:  json
Unify Jdocs
A new way of working with JSON documents without using model classes or JSON schemas
Stars: ✭ 58 (-1.69%)
Mutual labels:  json
Lazyjson
A very fast, very lazy JSON parser for Java.
Stars: ✭ 55 (-6.78%)
Mutual labels:  json
Ngx Excel Export
Angular6 application with export data to excel file functionality.
Stars: ✭ 58 (-1.69%)
Mutual labels:  json
React Cute
Render cute JSON in React
Stars: ✭ 55 (-6.78%)
Mutual labels:  json
Feedr
Use feedr to fetch the data from a remote url, respect its caching, and parse its data. Despite its name, it's not just for feed data but also for all data that you can feed into it (including binary data).
Stars: ✭ 56 (-5.08%)
Mutual labels:  json
Jsonfield
A reusable Django model field for storing ad-hoc JSON data
Stars: ✭ 1,101 (+1766.1%)
Mutual labels:  json
Re Txt
converts text-formats from one to another, it is very useful if you want to re-format a json file to yaml, toml to yaml, csv to yaml, ... etc
Stars: ✭ 59 (+0%)
Mutual labels:  json
Rumble
⛈️ Rumble 1.11.0 "Banyan Tree"🌳 for Apache Spark | Run queries on your large-scale, messy JSON-like data (JSON, text, CSV, Parquet, ROOT, AVRO, SVM...) | No install required (just a jar to download) | Declarative Machine Learning and more
Stars: ✭ 58 (-1.69%)
Mutual labels:  json

Build Status

The PHP extension tdlib allows you to work with the Telegram database library. If simple, this is the usual functions wrapper for working with the tdlib/td json client. You can:

  • create a JSON client $client = td_json_client_create()
  • execute the synchronous request $result = td_json_client_execute($client, $json);
  • perform an asynchronous request td_json_client_send($client, $json); *
  • get all the responses at the moment $response = td_json_client_receive($client, $timeout);
  • and destroy client td_json_client_destroy($client);

* you must use td_json_client_receive to get a response from an asynchronous request.

Getting started with TDLib

td_json_client.h File Reference

Video tutorial how to install and simple usage

phptdlib Documentation

Example

https://github.com/tdlib/td/blob/master/example/cpp/tdjson_example.cpp

<?php
$client = td_json_client_create();
td_json_client_execute($client, json_encode(['@type' => 'setLogVerbosityLevel', 'new_verbosity_level' => '0']));
$waitTimeout = 10;
while(true)
{
    $result = td_json_client_receive($client, $waitTimeout);
    if(!empty($result)) {
        echo $result;
        break;
    }
}
td_json_client_destroy($client);

TDLib\JsonClient

$client = new \TDLib\JsonClient();
$client->setDefaultTimeout(10);
$result = $client->execute($stringQuery);
$client->send($stringQuery);
$response = $client->receive($floatTimeout);
$response = $client->query($stringQuery, $floatTimeout);
$responses = $client->getReceivedResponses();
$client->destroy();

Thanks to @maxvgi, phptdlib has a really good implementation of query method. You no longer need to use send and receive (but you can if you want). An additional field will be added to query, and will wait for this additional field in the responses. Since this is for asynchronous requests, before you get an answer to the request, you can get others. And you can get all the responses to the last request with $client->getReceivedResponses();

Example. Simple workflow:

  • create a client
  • setTdlibParameters
  • if need, setDatabaseEncryptionKey
  • if getAuthorizationState returns authorizationStateWaitPhoneNumber, then setAuthenticationPhoneNumber
  • if getAuthorizationState returns authorizationStateWaitCode, then checkAuthenticationCode
  • if getAuthorizationState returns authorizationStateReady, then you are allowed to do what you want with tdlib/td (Function class reference).
<?php
Error_Reporting(E_ALL);
ini_set('display_errors', 1);

$api_id = 11111; // must be an integer
$api_hash = 'abcdef1234567890abcdef1234567890';
$phone_number = '+380991234567';

try {
    \TDApi\LogConfiguration::setLogVerbosityLevel(\TDApi\LogConfiguration::LVL_ERROR);
    
    $client = new \TDLib\JsonClient();
    
    $tdlibParams = new \TDApi\TDLibParameters();
    $tdlibParams
        ->setParameter(\TDApi\TDLibParameters::USE_TEST_DC, true)
        ->setParameter(\TDApi\TDLibParameters::DATABASE_DIRECTORY, '/var/tmp/tdlib')
        ->setParameter(\TDApi\TDLibParameters::FILES_DIRECTORY, '/var/tmp/tdlib')
        ->setParameter(\TDApi\TDLibParameters::USE_FILE_DATABASE, false)
        ->setParameter(\TDApi\TDLibParameters::USE_CHAT_INFO_DATABASE, false)
        ->setParameter(\TDApi\TDLibParameters::USE_MESSAGE_DATABASE, false)
        ->setParameter(\TDApi\TDLibParameters::USE_SECRET_CHATS, false)
        ->setParameter(\TDApi\TDLibParameters::API_ID, $api_id)
        ->setParameter(\TDApi\TDLibParameters::API_HASH, $api_hash)
        ->setParameter(\TDApi\TDLibParameters::SYSTEM_LANGUAGE_CODE, 'en')
        ->setParameter(\TDApi\TDLibParameters::DEVICE_MODEL, php_uname('s'))
        ->setParameter(\TDApi\TDLibParameters::SYSTEM_VERSION, php_uname('v'))
        ->setParameter(\TDApi\TDLibParameters::APPLICATION_VERSION, '0.0.10')
        ->setParameter(\TDApi\TDLibParameters::ENABLE_STORAGE_OPTIMIZER, true)
        ->setParameter(\TDApi\TDLibParameters::IGNORE_FILE_NAMES, false);
    $result = $client->setTdlibParameters($tdlibParams);

    $result = $client->setDatabaseEncryptionKey();
    
    $state = $client->getAuthorizationState();
    
    // you must check the state and follow workflow. Lines below is just for an example.
    // $result = $client->setAuthenticationPhoneNumber($phone_number, 3); // wait response 3 seconds. default - 1.
    // $result = $client->query(json_encode(['@type' => 'checkAuthenticationCode', 'code' => 'xxxxx', 'first_name' => 'dummy', 'last_name' => 'dummy']), 10);
    
    $result = $client->query(json_encode(['@type' => 'searchPublicChat', 'username' => 'telegram']), 10);
    
    $allNotifications = $client->getReceivedResponses();
} catch (\Exception $exception) {
    echo sprintf('something goes wrong: %s', $exception->getMessage());
}

Required

Please note that TDLib itself requires a lot of resources. The extension will be built in a couple of seconds. But the extension requires compiled tdlib/td. A minimum of 4GB of RAM is recommended. But I know that it is possible to build on a VPS with g++ and 2GB RAM + swap.

The most of dependencies are installed via git submodules currently.

But PHP-CPP currently has to be built separately.

git clone https://github.com/CopernicaMarketingSoftware/PHP-CPP.git

cd PHP-CPP
make
sudo make install

If you want to link other dependencies as dynamic libraries, you can pass some options to cmake. Look through CMakeLists.txt comments to find out how.

install extension

git clone --recurse-submodules https://github.com/yaroslavche/phptdlib.git
cd phptdlib && mkdir build && cd build
cmake ..
make
sudo make install

check

php -i | grep tdlib
php ../php_examples/func.php

Feel free to $client->query(json_encode(['@type' => 'sendMessage', 'chat' => '@yaroslavche'])); if you have any questions.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].