unione / unione-php
UniOne SDK 包,提供与 UniOne API 交互的方法。
v1.2.0
2023-05-11 11:13 UTC
Requires
- php: ^7.4 || ^8.0
- guzzlehttp/guzzle: ^6.5 || ^7
- webmozart/assert: ^1.9.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.15
- dev-master
- v1.2.0
- v1.1.0
- v1.0.0
- v0.0.1
- dev-release-please--branches--master
- dev-PSUO-22-set-the-endpoint-as-required
- dev-PSUO-25-add-config-file-path-and-GuzzleHttp-load-fix
- dev-change_versiot_to__1_1_0
- dev-PSUO-22_add_unione_cli_script
- dev-PSUO-24_add_httpRequest_method_documentation
- dev-PSUO-22_Add_Webhooks_API
- dev-PSUO-21_allow_empty_fields_when_template_sends
- dev-PSUO-7_add_readme_md_file
- dev-PSUO-19_check_that_all_api_method_gets_array
- dev-PSUO-20_Made_endpoint_optional
- dev-PSUO-20_Make_endpoint_optional
- dev-PSUO-17_Add_platform_name_to_GET_param
- dev-PSUO-14_Implement_Template_API_handlers
- dev-Change_SDK_version
- dev-PSUO-18_Add_subscribe_method_in_Email_API
- dev-PSUO-12_API_response_processing
- dev-PSUO-11_Introduce_Api_Email_class
- dev-PSUO-13_Implement_debug_mode
- dev-PSUO-6-Create_email_class
- dev-PSUO-10-Configure_unit_tests
- dev-PSUO-9-Fix_release-please_action
- dev-PSUO-4-Expose_Guzzle_config
- dev-reviewdog-reporter
- dev-releases/1.0.0
- dev-release-please--branches--releases/1.0.0
- dev-releases/0.0.0
- dev-fix-style-and-add-timeout
This package is not auto-updated.
Last update: 2024-09-26 17:34:27 UTC
README
此 SDK 包含与 UniOne API 交互的方法:[https://docs.unione.io/en/web-api-ref#web-api](https://docs.unione.io/en/web-api-ref#web-api)
安装
使用 Composer 安装此包
composer require unione/unione-php
用法
发送邮件
$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME'); // Example for EU instance. $client = new Unione\UnioneClient('YOUR-API-KEY', 'eu1.unione.io'); $recipients = [ [ "email" => 'john@example.com', "substitutions" => [ "to_name" => "John Smith" ], ], [ "email" => 'liza@example.com', "substitutions" => [ "to_name" => "Liza Frank" ], ] ]; $body = [ "html" => "<b>Test mail, {{to_name}}</b>", "plaintext" => "Hello, {{to_name}}", "amp" => "<!doctype html><html amp4email><head> <meta charset=\"utf-8\"><script async src=\"https://cdn.ampproject.org/v0.js\"></script> <style amp4email-boilerplate>body[visibility:hidden]</style></head><body> Hello, AMP4EMAIL world.</body></html>" ]; // You can use email object can be used to prepare the message array. // But the email send method accepts an array, that can be composed without // SDK utils. $mail = new Unione\Model\Email($recipients, $body); $mail->setFromEmail('user@example.com'); $mail->setSubject('test letter'); $response = $client->emails()->send($mail->toArray());
查看 API 文档 以获取更多详细信息。
查看 模板引擎文档 以了解替换细节。
发送订阅邮件
$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME'); $params = [ "from_email" => "john@example.com", "from_name" => "John Smith", "to_email" => "user@example.com" ]; $response = $client->emails()->subscribe($params);
API 文档。
设置模板
$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME'); $params = [ "template" => [ "name" => "First template", "body" => [ "html" => "<b>Hello, {{to_name}}</b>", "plaintext" => "Hello, {{to_name}}", "amp" => "<!doctype html><html amp4email><head> <meta charset=\"utf-8\"><script async src=\"https://cdn.ampproject.org/v0.js\"></script> <style amp4email-boilerplate>body[visibility:hidden]</style></head><body> Hello, AMP4EMAIL world.</body></html>" ], "subject" => "Test template mail", "from_email" => "test@example.com", "from_name" => "Example From", ] ]; $response = $client->templates()->set($params);
API 文档。
获取模板
$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME'); $response = $client->templates()->get('YOUR-TEMPLATE-ID');
API 文档。
获取模板列表
$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME'); $params = [ "limit" => 50, "offset" => 0 ]; $response = $client->templates()->list($params);
API 文档。
删除模板
$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME'); $response = $client->templates()->delete('YOUR-TEMPLATE-ID');
API 文档。
设置 webhook
$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME'); $params = [ "url" => "https://yourhost.example.com/unione-webhook", "events" => [ "email_status" => [ "delivered", "opened", "clicked", "unsubscribed", "soft_bounced", "hard_bounced", "spam" ] ] ]; $response = $client->webhooks()->set($params);
API 文档。
指定的 URL 将从 Unione 接收请求。有关请求数据的更多信息,请参阅 API 文档。
这是如何在回调处理程序中检查消息完整性的方法
$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME'); // $body contains a callback request body. if ($client->webhooks()->verify($body) === TRUE) { // The webhook is confirmed, result can be processed. }
获取 webhook
$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME'); $response = $client->webhooks()->get('YOUR-WEBHOOK-URL');
API 文档。
获取所有或部分 webhook 列表
$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME'); $response = $client->webhooks()->list();
API 文档。
删除 webhook
$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME'); $response = $client->webhooks()->delete('YOUR-WEBHOOK-URL');
API 文档。
附加信息
通用 API 方法
对于 SDK 中尚未实现的 API 方法,您可以使用 UnioneClient::httpRequest()
。以下是一个“设置”抑制方法的示例
$client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME'); $response = $client->httpRequest('suppression/set.json', ["email" => "user@example.com", "cause" => "unsubscribed"]);
设置 Guzzle HTTP 客户端配置
Unione 客户端接受一个数组,该数组包含 Guzzle 配置作为第三个参数。在创建客户端时,您可以传递一些额外的选项(例如 connect_timeout),以便将其应用于所有请求。
这是一个添加历史处理程序以保存入站请求和响应的更高级示例。
$container = []; $history = Middleware::history($container); $handlerStack = HandlerStack::create(); $handlerStack->push($history); $config = ['handler' => $handlerStack]; $client = new Unione\UnioneClient('YOUR-API-KEY', 'YOUR-HOST-NAME', $config);
查看 Guzzle 文档。