https://github.com/nullform/telegraphus
PHP SDK for Telegraph API
https://github.com/nullform/telegraphus
php telegraph telegraph-api
Last synced: 5 months ago
JSON representation
PHP SDK for Telegraph API
- Host: GitHub
- URL: https://github.com/nullform/telegraphus
- Owner: nullform
- License: mit
- Created: 2024-12-17T19:35:04.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-12-24T16:50:28.000Z (over 1 year ago)
- Last Synced: 2025-08-16T18:43:45.740Z (8 months ago)
- Topics: php, telegraph, telegraph-api
- Language: PHP
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Telegraphus
The PHP package to interact with [Telegra.ph](https://telegra.ph/).
> Telegra.ph is a minimalist publishing tool that allows you to create richly formatted posts
> and push them to the Web in just a click.
## Requirements
- PHP >= 5.6
## Installation
```shell
composer require nullform/telegraphus
```
## Usage examples
First you need to create an account and get an access token:
```php
use Nullform\Telegraphus\ApiClient;
use Nullform\Telegraphus\Types\Account;
$client = new ApiClient();
$account = new Account();
$account->author_name = 'John Doe';
$account->author_url = 'https://www.google.com/';
$account->short_name = 'john';
try {
$client->createAccount($account);
$token = $account->access_token;
$client->setToken($token);
// ...
} catch (\Exception $exception) {
// ...
}
```
Next, you can create pages using the parser to convert your HTML code into *Telegra.ph* format.
```php
use Nullform\Telegraphus\ApiClient;
use Nullform\Telegraphus\Parser;
$token = 'token';
$client = new ApiClient($token);
$html = '
Title
'
. 'First paragraph'
. 'Second paragraph
'
. 'Copyright';
// Note that not all HTML tags are available in Telegra.ph.
// Available tags: a, aside, b, blockquote, br, code, em, figcaption, figure, h3, h4,
// hr, i, iframe, img, li, ol, p, pre, s, strong, u, ul, video.
$parser = new Parser();
// You can replace or delete tags
$parser->addTagReplaceRules([
'h1' => 'h3', // Convert
to
'div' => 'p', // Convert to
'span' => 'b', // Convert to
'footer' => false, // Remove
]);
try {
$page = $client->createPage(
'Test page',
$parser->htmlToTelegraphContent($html)
);
// ...
} catch (\Exception $exception) {
// ...
}
```
After a successful request, *Telegra.ph* will create a page with the title "Test Page" and the following content:
```html
Title
First paragraph
Second paragraph
```
You can pass HTML code as content to the createPage() method.
But then the HTML code will be converted to *Telegra.ph* format "as is", without replacing tags.
Don't forget that *Telegra.ph* doesn't support all tags, only the following:
a, aside, b, blockquote, br, code, em, figcaption, figure, h3, h4, hr, i, iframe, img, li, ol, p, pre, s, strong, u, ul, video.
And now you can get a list of all your pages:
```php
use Nullform\Telegraphus\ApiClient;
$token = 'token';
$client = new ApiClient($token);
try {
$pageList = $client->getPageList();
foreach ($pageList->pages as $page) {
// ...
}
} catch (\Exception $exception) {
// ...
}
```
If you want to edit one:
```php
use Nullform\Telegraphus\ApiClient;
$token = 'token';
$client = new ApiClient($token);
$html = '
New page content
';
$path = 'page path';
try {
$page = $client->editPage($path, 'Test page', $html);
// ...
} catch (\Exception $exception) {
// ...
}
```
## Methods
### ApiClient
- ApiClient::**__construct**(*?string* $token = null)
- ApiClient::**setToken**(*?string* $token): *ApiClient*
- ApiClient::**getToken**(): *?string*
- ApiClient::**getCurlOptions**(): *array*
- ApiClient::**setCurlOptions**(*array* $curlOptions): *ApiClient*
- ApiClient::**setCurlOption**(*int* $option, *mixed* \$value): *ApiClient*
- ApiClient::**getCurlInfo**(): *array*
- ApiClient::**createAccount**(*Account* $account): *Account*
- ApiClient::**editAccountInfo**(*Account* $account): *Account*
- ApiClient::**getAccountInfo**(): *Account*
- ApiClient::**revokeAccessToken**(): *Account*
- ApiClient::**createPage**(*string* \$title, *NodeElement[]|string* \$content, *?string* \$authorName = null, *?string* \$authorUrl = null): *Page*
- ApiClient::**editPage**(*string* \$path, *string* \$title, *NodeElement[]|string* \$content, *?string* \$authorName = null, *?string* \$authorUrl = null): *Page*
- ApiClient::**getPage**(*string* $path): *Page*
- ApiClient::**getPageList**(*int* \$offset = 0, *int* \$limit = 50): *PageList*
- ApiClient::**getViews**(*string* $path, *?GetViewsParams* \$params): *PageViews*
### Parser
- Parser::**addTagReplaceRule**(*string* $tag, *string* \$toTag): *Parser*
- Parser::**addTagReplaceRules**(*array* $rules): *Parser*
- Parser::**hasTagReplaceRule**(*string* $tag): *bool*
- Parser::**getTagReplaceRule**(*string* $tag): *string|false|null*
- Parser::**setAllowedAttributes**(*?string[]* $attributes): *Parser*
- Parser::**setDisallowedAttributes**(*string[]* $attributes): *Parser*
- Parser::**htmlToTelegraphContent**(*string* $html): *NodeElement[]|string[]*
- Parser::**telegraphContentToHtml**(*NodeElement[]* $content): *string*
- Parser::**decodeTelegraphContent**(*string* $json): *NodeElement[]*
## Tests
Tested on PHP 5.6, 7.2 and 8.0.
```shell
php composer.phar test tests
```