nullform/zerobounce-client

ZeroBounce API 客户端

v0.1.3 2019-12-27 04:39 UTC

This package is auto-updated.

Last update: 2024-09-27 15:45:26 UTC


README

非官方的 ZeroBounce API PHP 客户端。

安装

composer require nullform/zerobounce-client

使用

基本

use Nullform\ZeroBounce;

$client = new ZeroBounce\Client($api_key);

try {
    $result = $client->validate($email);
} catch (ZeroBounce\Exceptions\AbstractException $exception) {
    echo $exception->getMessage();
}

缓存

如果您在项目中使用了 PSR-6 或 PSR-16 缓存,则可以缓存 API 响应。只需将您的缓存存储库实例、TTL 和前缀传递给 Client::caching()

如果传递了缓存实例,则每个成功的响应都将存储到缓存中,时间为 $ttl 秒。对于具有相同参数的重复请求,将使用缓存中的响应。

// Set PSR-6 or PSR-16 cache instance, TTL (60) and cache keys prefix (zerobounce_)
$client->caching($cache, 60, 'zerobounce_');

日志记录

您可以通过将自定义函数传递给 Client::logFunction() 来记录您的 API 调用。传递的函数将在每次 API 调用时调用。该函数接受一个 \Nullform\ZeroBounce\Client 实例作为参数。例如

$log_func = function (ZeroBounce\Client $client) {
    file_put_contents('zerobounce-client.log', print_r($client->lastResponse(), true));
};

$client = new ZeroBounce\Client($api_key);
$client->logFunction($log_func);

$usage = $client->getUsage('2019-01-01');

或者,您可以简单地覆盖每次 API 调用时都会调用的 Client::log() 方法。

方法

Client::timeout()

Client::timeout( [ int $timeout = null ] ) : int

超时(请求允许的最大时间)。获取或设置值。

Client::caching()

Client::caching( $cache, int $ttl [, string $prefix = '' ] ) : bool

API 响应的缓存。

您可以通过传递 null 来禁用缓存(默认)。

Client::logFunction()

Client::logFunction( ?callable $func ) : bool

在每次 ZeroBounce API 调用后调用的日志记录函数。

该函数接受一个客户端实例作为参数。

Client::lastRequest()

Client::lastRequest( void ) : Http\Request

获取最后一个请求。

Client::lastResponse()

Client::lastResponse( void ) : Http\Response

获取最后一个响应。

Client::getCredits()

Client::getCredits( void ) : int

获取信用余额。如果返回 -1,则表示您的 API 密钥无效。

验证 API:信用余额

Client::validate()

Client::validate( string $email [, ?string $ip_address = null ] ) : Models\Email

验证电子邮件。

验证 API:验证电子邮件

Client::getUsage()

Client::getUsage( mixed $start_date [, mixed $end_date = 'now' ] ) : Models\Usage

获取 API 使用情况。

$start_date$end_date - 时间戳或字符串,它由 strtotime()、DateTime 和 date_create() 解析器理解。

验证 API:API 使用情况

Client::bulkSendFile()

Client::bulkSendFile( string $filename, string $type, Params\BulkSendFileParams $params ) : Models\BulkFile

发送 csv 或 txt 文件进行批量电子邮件验证/评分。

$client = new Client($api_key);
$client->timeout(60);
$params = new Params\BulkSendFileParams();
$params->email_address_column = 2;
$file = $client->bulkSendFile($filename, Models\BulkFile::TYPE_VALIDATION, $params);

验证 API:发送文件 | AI 评分 API:发送文件

Client::bulkFileStatus()

Client::bulkFileStatus( string $file_id, string $type ) : Models\BulkFile

上传文件状态。

验证 API:文件状态 | AI 评分 API:文件状态

Client::bulkGetFile()

Client::bulkGetFile( string $file_id, string $type [, ?string $output_filename = null ] ) : string

获取使用 sendfile API 提交的文件的验证/评分结果的 csv(或 zip)文件。

验证API:获取文件 | AI评分API:获取文件

Client::bulkDeleteFile()

Client::bulkDeleteFile( string $file_id, string $type ) : bool

删除使用sendfile API提交的文件。文件只有在状态为完成时才能被删除。

验证API:删除文件 | AI评分API:删除文件

门面

您可以使用门面进行快速电子邮件验证和余额检查。

use Nullform\ZeroBounce\Facade as ZeroBounce;

try {
    $result = ZeroBounce::validate($api_key, $email);
    $credits = ZeroBounce::getCredits($api_key);
} catch (\Exception $exception) {
    $error = $exception->getMessage();
}