jacklul/e621-api

此包已被放弃,不再维护。没有建议的替代包。

e621.net API 的包装器

0.5.1 2019-01-10 21:51 UTC

This package is auto-updated.

Last update: 2020-07-10 09:45:46 UTC


README

e621 API 构建状态

使用 PHP 编写的 e621.net API 的简单包装器,底层使用 Guzzle

此类目前处于开发中,大部分内容尚未测试,并且实体中并非所有方法都已实现。

目录

说明

安装

通过 Composer 安装此包 - composer require jacklul/e621-api

用法

  • 初始化 E621 对象
    require 'vendor/autoload.php';
    
    use jacklul\E621API\E621;
    
    $api = new E621();
  • 您应该指定自定义用户代理来识别您的项目
    $options = [
        'headers'  => [
            'User-Agent' => 'My project',
        ],
    ];
    
    $api = new E621($options);
  • 使用参数执行请求
    $request = $api->postIndex(['tags' => 'cat order:score', 'limit' => 25]);
  • 这将返回 Response 对象,在获取结果数据之前,我们确保请求成功
    if ($request->isSuccessful()) {
        $results = $request->getResult();    // Result data (usually array of objects, empty array means no results)
    } else {
        echo $request->getReason();     // Failure reason
        echo $request->getMessage();    // Failure descriptive message (when available)
        echo $request->getRawResult();  // Raw response (when available)
    }
  • 您可以使用 foreach() 简单地迭代结果,然后打印每个图像的 URL
    /** @var \jacklul\E621API\Entity\Post $post */
    foreach ($results as $post) {
        echo $post->getFileUrl() . PHP_EOL;
    }

您可以将仅应用于单个请求的 Guzzle 选项作为第二个参数传递

    $request = $api->postIndex(['tags' => 'cat order:score'], ['timeout' => 10]);

默认情况下,库将在连接失败或错误时抛出异常,您可以通过以下方式禁用此功能

    $api->throwExceptions(false);   // Not recommended

禁用后 - 所有错误消息都可通过 $request->getError() 方法获取,而 $request->getReason() 将包含可以显示给用户的消息。

登录

某些操作需要登录才能执行,为了认证,您可以在每次请求中传递 loginpassword_hash(API密钥)参数,或者全局设置

    $api = new E621();
    $api->login('login', 'api_key');  // Set authentication data
    $request = $api->dmailInbox();
    $api->logout();                   // Remove authentication data

调试

您可以通过初始化对象时设置进度处理程序,针对单个请求或之后设置 Guzzle 的选项

    $api->setProgressHandler([$this, 'progress']);
    $api->setProgressHandler(null);     // Unset the handler

类似于调试日志功能

    $api->setDebugLogHandler([$this, 'logger']);
    $api->setDebugLogHandler(null);     // Unset the handler

这将在请求完成之前将输出写入 php://temp,然后将其刷新到您的处理程序,如果您需要不同的方法,则通过 Guzzle 的选项设置处理程序。

API 方法

请参阅 官方 API 文档

帖子

  • postCreate(需要登录)
  • postUpdate(需要登录)
  • postShow
  • postCheckMd5
  • postTags
  • postIndex
  • postFlag(需要登录)
  • postDestroy (需要登录)
  • postDeletedIndex
  • postPopularByDay
  • postPopularByWeek
  • postPopularByMonth
  • postRevertTags (需要登录)
  • postVote (需要登录)

标签

  • tagIndex
  • tagShow
  • tagUpdate (需要登录)
  • tagRelated
  • tagAliasIndex
  • tagImplicationIndex

艺术家

  • artistIndex
  • artistCreate (需要登录)
  • artistUpdate (需要登录)
  • artistDestroy (需要登录)

评论

  • commentShow
  • commentIndex
  • commentSearch
  • commentCreate (需要登录)
  • commentUpdate (需要登录)
  • commentDestroy (需要登录)
  • commentHide (需要登录)
  • commentUnhide (需要登录)
  • commentVote (需要登录)

闪点

  • blipCreate (需要登录)
  • blipUpdate (需要登录)
  • blipIndex
  • blipShow
  • blipHide (需要登录)
  • blipUnhide (需要登录)

维基

  • wikiIndex
  • wikiCreate (需要登录)
  • wikiUpdate (需要登录)
  • wikiShow
  • wikiDestroy (需要登录)
  • wikiLock (需要登录)
  • wikiUnlock (需要登录)
  • wikiRevert (需要登录)
  • wikiHistory
  • wikiRecentChanges

笔记

  • noteIndex
  • noteSearch
  • noteHistory
  • noteRevert (需要登录)
  • noteUpdate (需要登录)

用户

  • userIndex
  • userShow
  • userRecordShow

Dmail

  • dmailCreate (需要登录)
  • dmailInbox (需要登录)
  • dmailShow (需要登录)
  • dmailHide (需要登录)
  • dmailUnhide (需要登录)
  • dmailHideAll (需要登录)
  • dmailUnhideAll (需要登录)
  • dmailMarkAllRead (需要登录)

论坛

  • forumCreate (需要登录)
  • forumUpdate (需要登录)
  • forumIndex
  • forumSearch
  • forumShow
  • forumHide (需要登录)
  • forumUnhide (需要登录)

  • poolIndex
  • poolShow
  • poolUpdate (需要登录)
  • poolCreate (需要登录)
  • poolDestroy (需要登录)
  • poolAddPost (需要登录)
  • poolRemovePost (需要登录)

集合

  • setIndex
  • setShow
  • setCreate (需要登录)
  • setUpdate (需要登录)
  • setAddPost (需要登录)
  • setRemovePost (需要登录)
  • setDestroy (需要登录)
  • setMaintainers (需要登录)

维护者

  • setMaintainerIndex (需要登录)
  • setMaintainerCreate (需要登录)
  • setMaintainerDestroy (需要登录)
  • setMaintainerApprove (需要登录)
  • setMaintainerDeny (需要登录)
  • setMaintainerBlock (需要登录)

收藏

  • favoriteListUsers
  • favoriteCreate (需要登录)
  • favoriteDestroy (需要登录)

标签历史

  • postTagHistoryIndex

标记历史

  • postFlagHistoryIndex

票据

  • ticketCreate (需要登录)
  • ticketIndex
  • ticketShow

异常

jacklul\E621API\Exception\ConnectException

当连接到 e621.net API 失败或超时时抛出,在大多数情况下,它也会包含 GuzzleHttp\Exception\ConnectException 异常,这可能会解释问题 ($e->getPrevious())。

jacklul\E621API\Exception\LoginRequiredException

当执行的方法需要登录但未提供登录数据时抛出。

jacklul\E621API\Exception\E621Exception

当发生某些事情,但不是上述任何一种情况时抛出。

可能的情况

  • HTTP 客户端错误,将包含 GuzzleHttp\Exception\GuzzleException 异常 ($e->getPrevious())
  • 数据解析错误,将包含 Psr\Http\Message\ResponseInterface 对象 ($e->getResponse(),要获取原始体使用 $e->getResponse()->getBody()

许可

MIT 许可证,详情请见 LICENSE