toarupg0318 / hatena-blog-client
一款 Hatena 博客客户端软件包。
1.0.0
2023-05-20 06:40 UTC
Requires
- php: ^8.1
- ext-simplexml: *
- guzzlehttp/guzzle: ^7.5
Requires (Dev)
- mockery/mockery: ^1.5
- pestphp/pest: ^2.4
- phpstan/phpstan: ^1.10
- rector/rector: ^0.15.24
- symfony/var-dumper: ^6.2
- vlucas/phpdotenv: ^5.5
This package is auto-updated.
Last update: 2024-09-28 10:29:46 UTC
README
用法
PHP 8.1+ 是必需的
准备
首先,请从 详细设置 获取用于连接是てな博客的认证信息。
请使用以下命令使用 composer 安装。
composer require toarupg0318/hatena-blog-client
按照以下方式获取客户端实例。
use Toarupg0318\HatenaBlogClient\HatenaClient; ... $hatenaClient = HatenaClient::getInstance( 'あなたのはてなID', 'あなたのブログID', 'あなたのAPIキー' );
文章发布
- HTML 模式(所见即所得模式)
- はてな记法模式
可以发布文章。 ※ 目前尚未实现 Markdown 语法。
以下列出各模式的示例代码。
HTML 模式(所见即所得模式)
请先在 基本设置 中将编辑模式切换为"所见即所得模式"。
$content = <<<CONTENT <main> <style> h2 { font-size: 24px; color: red; } a { color: #1a73e8; text-decoration: none; } a:hover { text-decoration: underline; } </style> <section> <h2>見出し1</h2> <p>これは<em>サンプルの文章</em>です。 この記事では、簡単なHTMLサンプルを紹介しています。詳細については、<a href="#">こちら</a>をクリックしてください。</p> </section> <section> <h2>見出し2</h2> <p>この<em>サンプル記事</em>は、HTMLタグをいくつか使用しています。これにより、より見栄えの良いコンテンツを作成できます。</p> <ul> <li>リスト項目1</li> <li>リスト項目2</li> <li>リスト項目3</li> </ul> </section> </main> CONTENT; $response = $hatenaClient ->post( content: $content, title: 'HTML投稿', draft: false, categories: ['foo', 'bar'] // はてなカテゴリ編集画面で存在しないカテゴリは新規追加されます ); // $response->getParsedData(); // レスポンス取得 // $response->getEntryId(); // ブログのエントリID(記事のユニークID)の取得
はてな记法模式
请先在 基本设置 中将编辑模式切换为"はてな记法模式"。
$hatenaSyntaxContent = HatenaDOMDocument::create() ->appendTableOfContents() ->appendH3('見出し h3') ->appendH4('見出し h4') ->appendH5('見出し h5') ->appendText( <<<TEXT はてなブログは、あなたの思いや考えを残したり、 さまざまな人が綴った多様な価値観に触れたりできる場所です。 TEXT ); $response = $hatenaClient ->post( content: $hatenaSyntaxContent, title: 'はてな記法投稿', draft: false, categories: ['foo', 'bar'] );
有关其他はてな记法的功能,请参阅 此处。
获取文章列表
可以获取最新 10 篇。
$getListResponse = $hatenaClient->getList(); // レスポンスを連想配列形式で取得 $getListResponse->getParsedData(); // また、記事一覧取得に限らず、以下のようにレスポンスを取得することもできます // この場合、XML形式の文字列となります // $getListResponse // ->getBody() // ->getContents();
要获取最新 10 篇之后的较旧帖子,请指定"下一页 URL"作为参数执行。
$hatenaClient ->getList('https://blog.hatena.ne.jp/foo765/foo765.hatenablog.com/atom/entry?page=1695039555'); ->getParsedData();
"下一页 URL"可以按以下方式获取。
$getListResponse = $hatenaClient->getList(); $getListResponse->getNextPageUrl(); // 'https://blog.hatena.ne.jp/foo765/foo765.hatenablog.com/atom/entry?page=1695039555'
获取文章
// 最新のエントリIDを取得する $latestEntryId = $hatenaClient ->getList() ->getParsedEntries()[0]['entryId'] ?? null; $getEntryResponse = $hatenaClient->getPostByEntryId($latestEntryId);
编辑文章
$hatenaSyntaxContent = HatenaDOMDocument::create() ->appendTableOfContents() ->appendH3('見出し h3') ->appendH4('見出し h4'); $editResponse = $hatenaClient ->edit( entryId: '4207575167685628272', content: $contentToEdit, draft: false, customUrl: 'happy new year 2023' ) ->getParsedData();
※ 无法将现有公开文章更改为草稿。
删除文章
可以通过指定获取的条目 ID 来删除文章。
$hatenaClient->deletePostByEntryId('4207575167685628272');
面向开发者
参与开发
开发方向尚未确定,但我们会适当接受 pull request。
执行测试
将项目根目录的 .env.example 复制为 .env。
cp .env.example .env
将新创建的 .env 中添加自己的はてな博客 AtomPub 连接信息。
HATENA_ID=hoge6789 HATENA_BLOG_ID=hoge6789.hatenablog.com HATENA_API_KEY=foo78bar90
然后,可以使用以下命令执行 Pest 测试。
./vendor/bin/pest
静态分析
PHPStan
可以使用以下命令执行 PHPStan 的静态分析。
./vendor/bin/phpstan analyse
Rector
可以使用以下命令执行 Rector 的静态分析。
./vendor/bin/rector process src # dry run # ./vendor/bin/rector process src --dry-run



