fcosrno / sendgrid-report
PHP 封装库,用于通过 SendGrid Web API 查看和管理 SendGrid 报告。
Requires
- php: >=5.3
- sendgrid/sendgrid: 2.*
Requires (Dev)
- phpunit/phpunit: 4.1.*
This package is not auto-updated.
Last update: 2024-09-24 08:02:46 UTC
README
PHP 封装库,用于通过 Web API 查看和管理 SendGrid 报告。
此库扩展了 官方 sendgrid-php 库。为了保持一致性,它遵循相同的编码风格、依赖项和约定。
快速使用
$sendgrid = new Fcosrno\SendGridReport\SendGrid('username', 'password'); $report = new Fcosrno\SendGridReport\Report(); $report->spamreports(); $result = $sendgrid->report($report);
安装
将 SendGrid Report 添加到您的 composer.json
文件中。如果您没有使用 Composer,您应该使用它。它是管理 PHP 应用程序依赖项的绝佳方式。
{ "require": { "fcosrno/sendgrid-report": "1.*" } }
然后在您的 PHP 脚本顶部引入自动加载器
require 'vendor/autoload.php';
如果您不希望使用 Composer,您可以通过 下载最新版本。然后将 src 文件夹中的两个库文件包含在内。
require_once('/path/to/SendGrid.php'); require_once('/path/to/Report.php');
示例应用
在 doc/example.php
中有一个示例,可以帮助您快速开始开发。
example.php
文件需要包含您的 SendGrid 凭据的 example_params.json
文件。该 json 文件位于 .gitignore
中,所以不用担心意外提交。为了方便您,我们还在 doc/example_params_placeholder.json
中提供了一个此类 json 文件的示例,您可以简单地复制/粘贴该文件,并将其重命名为 example_params.json
。
使用方法
要开始使用此库,请使用您的 SendGrid 凭据初始化 SendGrid 对象。
$sendgrid = new Fcosrno\SendGridReport\SendGrid('your_sendgrid_username', 'your_sendgrid_password');
创建一个新的 SendGrid Report 对象并添加方法详情。
$report = new Fcosrno\SendGridReport\Report(); $report->spamreports()->email('foo@bar.com'); $result = $sendgrid->report($report);
您可以根据 SendGrid Web API 中定义的获取垃圾报告、阻止、退订、无效电子邮件和取消订阅。动作和参数可链接到方法。默认情况下,推断出 get
动作。
例如,此 GET 请求有一个日期参数。
https://api.sendgrid.com/api/spamreports.get.json?api_user=your_sendgrid_username&api_key=your_sendgrid_password&date=1
等效的请求如下所示
$sendgrid = new Fcosrno\SendGridReport\SendGrid('your_sendgrid_username', 'your_sendgrid_password'); $report = new Fcosrno\SendGridReport\Report(); $report->spamreports()->date(); $result = $sendgrid->report($report);
您可以继续链接参数
$sendgrid = new Fcosrno\SendGridReport\SendGrid('your_sendgrid_username', 'your_sendgrid_password'); $report = new Fcosrno\SendGridReport\Report(); $report->spamreports()->date()->days(1)->startDate('2014-01-01')->email('foo@bar.com'); $result = $sendgrid->report($report);
如果您想使用其他动作如 delete
、add
或 count
,只需将其添加到链中,如下所示
$sendgrid = new Fcosrno\SendGridReport\SendGrid('your_sendgrid_username', 'your_sendgrid_password'); $report = new Fcosrno\SendGridReport\Report(); $report->blocks()->delete()->email('foo@bar.com'); $result = $sendgrid->report($report);
阻止
返回具有状态、原因和电子邮件的阻止列表
$report = new Fcosrno\SendGridReport\Report(); $report->blocks(); $result = $sendgrid->report($report);
退订
返回具有状态、原因和电子邮件的退订列表。
$report = new Fcosrno\SendGridReport\Report(); $report->bounces(); $result = $sendgrid->report($report);
带有参数,可选的搜索电子邮件地址。
$report = new Fcosrno\SendGridReport\Report(); $report->bounces()->email('foo@bar.com'); $result = $sendgrid->report($report);
无效电子邮件
返回具有原因和电子邮件的无效电子邮件列表。
$report = new Fcosrno\SendGridReport\Report(); $report->invalidemails(); $result = $sendgrid->report($report);
垃圾报告
返回具有 IP 和电子邮件的垃圾报告列表。
$report = new Fcosrno\SendGridReport\Report(); $report->spamreports(); $result = $sendgrid->report($report);
取消订阅
取消订阅需要 API 访问。
$report = new Fcosrno\SendGridReport\Report(); $report->unsubscribes(); $result = $sendgrid->report($report);
贡献
- 分支
- 创建您的功能分支(
git checkout -b my-new-feature
) - 提交您的更改(
git commit -am 'Added some feature'
) - 推送到分支(
git push origin my-new-feature
) - 创建新的 Pull Request
测试
测试是用 PHPUnit 构建的。
请确保您已安装了开发需求。
composer install
转到项目根目录,然后在终端中输入以下内容以运行所有测试
phpunit