blua-blue / blua-blue-php-sdk
blua.blue 的 PHP SDK
Requires
- ext-json: *
- guzzlehttp/guzzle: ^7.4
Requires (Dev)
- phpunit/phpunit: ^9.5.10
This package is auto-updated.
Last update: 2024-09-19 22:35:24 UTC
README
本包的目的是实现快速、安全且简单的将 blua.blue 内容集成到自己的网站域名中。 注意:此库也适用于 blua.blue 的开源安装
安装
composer require blua-blue/blua-blue-php-sdk
使用
require __DIR__ . 'vendor/autoload.php'; $client = new BluaBlue\Client('yourPublicKey', 'yourAPIkey'); try{ $client->authenticate(); $myArticles = $client->getOwnArticles(); foreach ($myArticles as $article){ echo $article->getName(); echo $article->getContentHtml(); } } catch (Exception $e) { ... }
客户端方法
构造函数
$bluaBlue = new Client($publicKey, $apiKey=null, $apiEndpoint = 'https://blua.blue')
getArticle($articleSlugOrID)
此方法接受特定文章的唯一 ID 或唯一文章-slug。
getOwnArticles()
检索所有拥有的文章,无论发布状态如何
getArticlesByKeywords($keywords)
以逗号分隔或单个关键字,例如。
$phpTutorials = getArticlesByKeywords('php,tutorial');
getCategories()
检索可用分类列表
getImages()
检索存储的图像
registerImage($pathOrBase64, $mode='external')
模式:外部 | 上传
要么注册外部图像以供参考,要么接受 base64 编码的图像字符串。(所有浏览器原生内容类型,最大 600kb)
createArticle($article)
创建新文章。虽然你可以传入一个简单的数组,但我们建议使用包装器
$bluaBlue = new Client(getenv('publicKey'), getenv('apiKey')); $newArticle = new \BluaBlue\Article(); $newArticle->setName('My awesome article'); $newArticle->setTeaser('What you always wanted to know about me'); $newArticle->setCategoryId('F7A3D7DFA54C11EB9242D83BBF2ADDD8'); $newArticle->setKeywords('biography'); $newArticle->addArticleContent([ 'sort'=>1, 'content_type'=>'markdown', 'content'=>'## hi there' ]) //... $bluaBlue->createArticle($newArticle);
updateArticle($article)
与 createArticle 类似,但操作现有文章
$bluaBlue = new Client(getenv('publicKey'), getenv('apiKey')); $myArticle = $bluaBlue->getArticle('my-awesome-article') $myArticle->addArticleContent([ 'sort'=>count($myArticle->getArticleContent())+1, 'content_type'=>'markdown', 'content'=>'## chapter 2 ...' ]); //... $bluaBlue->updateArticle($myArticle);
文章包装器
文章包装器可以使用数组构建
new Article(['name'=>'如何设置 x'])
或空 new Article()
无论如何,包装器都有以下属性的设置器和获取器
- $id (仅获取器)
- $name
- $slug (仅获取器)
- $teaser
- $image_id
- $author_user_id (设置器将被端点覆盖)
- $category_id
- $is_public
- $keywords
- $publish_date
- $insert_date (仅获取器)
- $update_date
- $delete_date
- $article_content
- $article_store
示例
$new = new Article(); $new->setName('Title of my article'); echo $new->getName(); // prints 'Title of my article'
注意:您可以让端点根据 neoan3-db 逻辑确定某些值
$article->setDeleteDate('.')
将在请求接收时转换为当前时间点。换句话说,SQL 等价于 NOW()
图像包装器
图像包装器可以使用数组构建
new Image(['format'=>'image/png']) 或空 new Image()
无论如何,包装器都有以下属性的设置器和获取器
- id (仅获取器)
- path
- format
- inserterUserId (仅获取器)
示例
$new = new Image(); $new->setPath('https://s3.aws.com/234.asdf'); echo $new->getPath(); // prints 'https://s3.aws.com/234.asdf'
文章和图像上的其他方法
toArray()
将对象导出为关联数组。
文章上的其他方法
getContentHtml()
导出所有内容的组合 HTML 字符串。
$article = $blueAblue->getArticle('best-article-I-wrote'); $allContent = $article->getContentHtml(); // equivalent to: $allContent = ''; foreach($article->getArticleContent() as $content){ $allContent .= $content['html']; }