{"id":21558758,"url":"https://github.com/yaroslavche/phptdlib","last_synced_at":"2025-04-10T10:42:57.563Z","repository":{"id":29858850,"uuid":"122892495","full_name":"yaroslavche/phptdlib","owner":"yaroslavche","description":"PHP Extension for tdlib/td written with PHP-CPP","archived":false,"fork":false,"pushed_at":"2023-07-11T04:43:48.000Z","size":9333,"stargazers_count":107,"open_issues_count":4,"forks_count":27,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-03-24T09:38:21.579Z","etag":null,"topics":["cpp","json","php-cpp","php-extension","php7","tdlib"],"latest_commit_sha":null,"homepage":"https://yaroslavche.github.io/phptdlib/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yaroslavche.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-26T00:42:34.000Z","updated_at":"2025-02-08T03:24:16.000Z","dependencies_parsed_at":"2024-11-24T10:03:15.040Z","dependency_job_id":null,"html_url":"https://github.com/yaroslavche/phptdlib","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaroslavche%2Fphptdlib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaroslavche%2Fphptdlib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaroslavche%2Fphptdlib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaroslavche%2Fphptdlib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yaroslavche","download_url":"https://codeload.github.com/yaroslavche/phptdlib/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248199939,"owners_count":21063790,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","json","php-cpp","php-extension","php7","tdlib"],"created_at":"2024-11-24T08:15:44.250Z","updated_at":"2025-04-10T10:42:57.540Z","avatar_url":"https://github.com/yaroslavche.png","language":"C++","readme":"[![Cmake workflow](https://github.com/yaroslavche/phptdlib/actions/workflows/cmake.yml/badge.svg)](https://github.com/yaroslavche/phptdlib/actions/workflows/cmake.yml)\n\nThe PHP extension `tdlib` allows you to work with the [Telegram database library](https://core.telegram.org/tdlib).\nIf simple, this is the usual [functions wrapper](include/td_json_client_func.hpp) for working with the `tdlib/td` json client. You can:\n - create a JSON client `$client = td_json_client_create()`\n - execute the synchronous request `$result = td_json_client_execute($client, $json);`\n - perform an asynchronous request `td_json_client_send($client, $json);` *\n - get all the responses at the moment `$response = td_json_client_receive($client, $timeout);`\n - and destroy client `td_json_client_destroy($client);`\n\n`*` you must use `td_json_client_receive` to get a response from an asynchronous request.\n\n[Getting started with TDLib](https://core.telegram.org/tdlib/getting-started)\n\n[td_json_client.h File Reference](https://core.telegram.org/tdlib/docs/td__json__client_8h.html)\n\n[Video tutorial how to install and simple usage](https://www.youtube.com/watch?v=gtQOjSVlvlI)\n\n[phptdlib Documentation](https://yaroslavche.github.io/phptdlib/)\n\n## Example\n[https://github.com/tdlib/td/blob/master/example/cpp/tdjson_example.cpp](https://github.com/tdlib/td/blob/master/example/cpp/tdjson_example.cpp)\n```php\n\u003c?php\n$client = td_json_client_create();\ntd_json_client_execute($client, json_encode(['@type' =\u003e 'setLogVerbosityLevel', 'new_verbosity_level' =\u003e '0']));\n$waitTimeout = 10;\nwhile(true)\n{\n    $result = td_json_client_receive($client, $waitTimeout);\n    if(!empty($result)) {\n        echo $result;\n        break;\n    }\n}\ntd_json_client_destroy($client);\n```\n\n## TDLib\\JsonClient\n\n```php\n$client = new \\TDLib\\JsonClient();\n$client-\u003esetDefaultTimeout(10);\n$result = $client-\u003eexecute($stringQuery);\n$client-\u003esend($stringQuery);\n$response = $client-\u003ereceive($floatTimeout);\n$response = $client-\u003equery($stringQuery, $floatTimeout);\n$responses = $client-\u003egetReceivedResponses();\n$client-\u003edestroy();\n```\n\nThanks 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-\u003egetReceivedResponses();` \n\nExample. Simple workflow:\n - create a client\n - `setTdlibParameters`\n - if need, `setDatabaseEncryptionKey`\n - if `getAuthorizationState` returns `authorizationStateWaitPhoneNumber`, then `setAuthenticationPhoneNumber`\n - if `getAuthorizationState` returns `authorizationStateWaitCode`, then `checkAuthenticationCode`\n - if `getAuthorizationState` returns `authorizationStateReady`, then you are allowed to do what you want with `tdlib/td` ([Function class reference](https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1_function.html)).\n\n```php\n\u003c?php\nError_Reporting(E_ALL);\nini_set('display_errors', 1);\n\n$api_id = 11111; // must be an integer\n$api_hash = 'abcdef1234567890abcdef1234567890';\n$phone_number = '+380991234567';\n\ntry {\n    \\TDApi\\LogConfiguration::setLogVerbosityLevel(\\TDApi\\LogConfiguration::LVL_ERROR);\n    \n    $client = new \\TDLib\\JsonClient();\n    \n    $tdlibParams = new \\TDApi\\TDLibParameters();\n    $tdlibParams\n        -\u003esetParameter(\\TDApi\\TDLibParameters::USE_TEST_DC, true)\n        -\u003esetParameter(\\TDApi\\TDLibParameters::DATABASE_DIRECTORY, '/var/tmp/tdlib')\n        -\u003esetParameter(\\TDApi\\TDLibParameters::FILES_DIRECTORY, '/var/tmp/tdlib')\n        -\u003esetParameter(\\TDApi\\TDLibParameters::USE_FILE_DATABASE, false)\n        -\u003esetParameter(\\TDApi\\TDLibParameters::USE_CHAT_INFO_DATABASE, false)\n        -\u003esetParameter(\\TDApi\\TDLibParameters::USE_MESSAGE_DATABASE, false)\n        -\u003esetParameter(\\TDApi\\TDLibParameters::USE_SECRET_CHATS, false)\n        -\u003esetParameter(\\TDApi\\TDLibParameters::API_ID, $api_id)\n        -\u003esetParameter(\\TDApi\\TDLibParameters::API_HASH, $api_hash)\n        -\u003esetParameter(\\TDApi\\TDLibParameters::SYSTEM_LANGUAGE_CODE, 'en')\n        -\u003esetParameter(\\TDApi\\TDLibParameters::DEVICE_MODEL, php_uname('s'))\n        -\u003esetParameter(\\TDApi\\TDLibParameters::SYSTEM_VERSION, php_uname('v'))\n        -\u003esetParameter(\\TDApi\\TDLibParameters::APPLICATION_VERSION, '0.0.10')\n        -\u003esetParameter(\\TDApi\\TDLibParameters::ENABLE_STORAGE_OPTIMIZER, true)\n        -\u003esetParameter(\\TDApi\\TDLibParameters::IGNORE_FILE_NAMES, false);\n    $result = $client-\u003esetTdlibParameters($tdlibParams);\n\n    $result = $client-\u003esetDatabaseEncryptionKey();\n    \n    $state = $client-\u003egetAuthorizationState();\n    \n    // you must check the state and follow workflow. Lines below is just for an example.\n    // $result = $client-\u003esetAuthenticationPhoneNumber($phone_number, 3); // wait response 3 seconds. default - 1.\n    // $result = $client-\u003equery(json_encode(['@type' =\u003e 'checkAuthenticationCode', 'code' =\u003e 'xxxxx', 'first_name' =\u003e 'dummy', 'last_name' =\u003e 'dummy']), 10);\n    \n    $result = $client-\u003equery(json_encode(['@type' =\u003e 'searchPublicChat', 'username' =\u003e 'telegram']), 10);\n    \n    $allNotifications = $client-\u003egetReceivedResponses();\n} catch (\\Exception $exception) {\n    echo sprintf('something goes wrong: %s', $exception-\u003egetMessage());\n}\n```\n\n## Required\n\nPlease 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](https://github.com/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.\n\nThe most of dependencies are installed via git submodules currently.\n\nBut PHP-CPP currently has to be built separately.\n \n - [PHP-CPP v2.0.0][2]\n \n```bash\ngit clone https://github.com/CopernicaMarketingSoftware/PHP-CPP.git\n\ncd PHP-CPP\nmake\nsudo make install\n```\n\nIf you want to link other dependencies as dynamic libraries, you can pass\n some options to cmake. Look through CMakeLists.txt comments to find out how.\n\n## install extension\n```bash\ngit clone --recurse-submodules https://github.com/yaroslavche/phptdlib.git\ncd phptdlib \u0026\u0026 mkdir build \u0026\u0026 cd build\ncmake ..\nmake\nsudo make install\n```\n### check\n```bash\nphp -i | grep tdlib\nphp ../php_examples/func.php\n```\n\n---\n\nFeel free to `$client-\u003equery(json_encode(['@type' =\u003e 'sendMessage', 'chat' =\u003e '@yaroslavche']));` if you have any questions.\n\n[1]: https://github.com/tdlib/td\n[2]: https://github.com/CopernicaMarketingSoftware/PHP-CPP/\n[3]: https://github.com/nlohmann/json\n[td_dependencies]: https://github.com/tdlib/td#dependencies\n[td_ram_issue]: https://github.com/tdlib/td/issues/67\n[phptdlib_docker_image]: https://hub.docker.com/r/yaroslavche/phptdlib/\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaroslavche%2Fphptdlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyaroslavche%2Fphptdlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaroslavche%2Fphptdlib/lists"}