Simple wrapper for e621.net API written in PHP, uses Guzzle under the hood.
This class is currently in development, most of the stuff was not tested and not every method in entities is implemented yet.
Install this package through Composer - composer require jacklul/e621-api
.
- Initialize the
E621
object:
require 'vendor/autoload.php';
use jacklul\E621API\E621;
$api = new E621();
- You should specify custom user agent to identify your project:
$options = [
'headers' => [
'User-Agent' => 'My project',
],
];
$api = new E621($options);
- Perform request with parameters:
$request = $api->postIndex(['tags' => 'cat order:score', 'limit' => 25]);
- This will return
Response
object, to get the actual data we make sure the request was successful first before getting result data:
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)
}
- You can easily iterate over the result using
foreach()
and then print every image's URL:
/** @var \jacklul\E621API\Entity\Post $post */
foreach ($results as $post) {
echo $post->getFileUrl() . PHP_EOL;
}
You can pass Guzzle's options applied only for a single request as a second parameter:
$request = $api->postIndex(['tags' => 'cat order:score'], ['timeout' => 10]);
By default library will throw exceptions on connection failures or errors, you can disable this by using:
$api->throwExceptions(false); // Not recommended
When disabled - all error messages will be available via $request->getError()
method and $request->getReason()
will contain a message that you can display to the user.
Some actions require logging in to execute, to authenticate you can either pass login
and password_hash
(API key) parameters with each request or set it globally:
$api = new E621();
$api->login('login', 'api_key'); // Set authentication data
$request = $api->dmailInbox();
$api->logout(); // Remove authentication data
You can set progress handler through Guzzle's options while initializing the object, per single request or after:
$api->setProgressHandler([$this, 'progress']);
$api->setProgressHandler(null); // Unset the handler
Similar with a debug logging function:
$api->setDebugLogHandler([$this, 'logger']);
$api->setDebugLogHandler(null); // Unset the handler
This will write the output to php://temp
until the request finishes and then it will flush it to your handler, if you need to use different approach then set your handler through Guzzle's options.
See official API documentation.
- postCreate (login required)
- postUpdate (login required)
- postShow
- postCheckMd5
- postTags
- postIndex
- postFlag (login required)
- postDestroy (login required)
- postDeletedIndex
- postPopularByDay
- postPopularByWeek
- postPopularByMonth
- postRevertTags (login required)
- postVote (login required)
- tagIndex
- tagShow
- tagUpdate (login required)
- tagRelated
- tagAliasIndex
- tagImplicationIndex
- artistIndex
- artistCreate (login required)
- artistUpdate (login required)
- artistDestroy (login required)
- commentShow
- commentIndex
- commentSearch
- commentCreate (login required)
- commentUpdate (login required)
- commentDestroy (login required)
- commentHide (login required)
- commentUnhide (login required)
- commentVote (login required)
- blipCreate (login required)
- blipUpdate (login required)
- blipIndex
- blipShow
- blipHide (login required)
- blipUnhide (login required)
- wikiIndex
- wikiCreate (login required)
- wikiUpdate (login required)
- wikiShow
- wikiDestroy (login required)
- wikiLock (login required)
- wikiUnlock (login required)
- wikiRevert (login required)
- wikiHistory
- wikiRecentChanges
- noteIndex
- noteSearch
- noteHistory
- noteRevert (login required)
- noteUpdate (login required)
- userIndex
- userShow
- userRecordShow
- dmailCreate (login required)
- dmailInbox (login required)
- dmailShow (login required)
- dmailHide (login required)
- dmailUnhide (login required)
- dmailHideAll (login required)
- dmailUnhideAll (login required)
- dmailMarkAllRead (login required)
- forumCreate (login required)
- forumUpdate (login required)
- forumIndex
- forumSearch
- forumShow
- forumHide (login required)
- forumUnhide (login required)
- poolIndex
- poolShow
- poolUpdate (login required)
- poolCreate (login required)
- poolDestroy (login required)
- poolAddPost (login required)
- poolRemovePost (login required)
- setIndex
- setShow
- setCreate (login required)
- setUpdate (login required)
- setAddPost (login required)
- setRemovePost (login required)
- setDestroy (login required)
- setMaintainers (login required)
- setMaintainerIndex (login required)
- setMaintainerCreate (login required)
- setMaintainerDestroy (login required)
- setMaintainerApprove (login required)
- setMaintainerDeny (login required)
- setMaintainerBlock (login required)
- favoriteListUsers
- favoriteCreate (login required)
- favoriteDestroy (login required)
- postTagHistoryIndex
- postFlagHistoryIndex
- ticketCreate (login required)
- ticketIndex
- ticketShow
Is thrown when connection to e621.net API failed or timed out, in most cases it will also contain GuzzleHttp\Exception\ConnectException
exception which might explain the issue ($e->getPrevious()
).
Is thrown when executed method requires logging in but no login data was provided.
Is thrown when something happens but it's none of the above.
Possible cases:
- HTTP client error, will contain
GuzzleHttp\Exception\GuzzleException
exception ($e->getPrevious()
) - data parsing error, will contain
Psr\Http\Message\ResponseInterface
object ($e->getResponse()
, to get raw body use$e->getResponse()->getBody()
MIT License, for details see LICENSE.