承接 nullform/telegraphus 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nullform/telegraphus

Composer 安装命令:

composer require nullform/telegraphus

包简介

PHP SDK for Telegraph API

README 文档

README

The PHP package to interact with 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

composer require nullform/telegraphus

Usage examples

First you need to create an account and get an access token:

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.

use Nullform\Telegraphus\ApiClient;
use Nullform\Telegraphus\Parser;

$token = 'token';
$client = new ApiClient($token);
$html = '<h1>Title</h1>'
    . '<div>First paragraph</div>'
    . '<div>Second <span>paragraph</span></div>'
    . '<footer>Copyright</footer>';

// 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 <h1> to <h3>
    'div'    => 'p',   // Convert <div> to <p>
    'span'   => 'b',   // Convert <span> to <b>
    'footer' => false, // Remove <footer>
]);

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:

<h3>Title</h3>
<p>First paragraph</p>
<p>Second <b>paragraph</b></p>

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:

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:

use Nullform\Telegraphus\ApiClient;

$token = 'token';
$client = new ApiClient($token);
$html = '<p>New page content</p>';
$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.

php composer.phar test tests

nullform/telegraphus 适用场景与选型建议

nullform/telegraphus 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「api」 「client」 「parser」 「telegraph」 「telegra.ph」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nullform/telegraphus 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nullform/telegraphus 我们能提供哪些服务?
定制开发 / 二次开发

基于 nullform/telegraphus 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-17