sumacrm / nylas-php
Nylas API 的 PHP 封装(适用于 lanlin)。
2.2.0
2018-07-16 14:48 UTC
Requires
- php: >=5.6
- guzzlehttp/guzzle: >=6.0
README
Nylas REST API 的 PHP 绑定 https://www.nylas.com
修改
我从 lanlin/nylas-php 分支了这个项目,因为原始包只支持 guzzle 5,而 lanlin 包只支持 PHP 7。现在我进行了一些修改,使其可以支持 guzzle >6.0。
请注意,可能还有一些我没有发现的错误。
安装
您可以使用 composer 安装我的分支库。
composer require "sumacrm/nylas-php"
用法
Nylas REST API 使用服务器端(三脚)OAuth,此库提供了方便的方法来简化 OAuth 过程。以下是其工作方式
- 您将用户重定向到我们的登录页面,并带上您的 App Id 和 Secret
- 您的用户登录
- 她将被重定向到您自己的回调 URL,并带有访问代码
- 您使用此访问代码从 API 获取授权令牌
有关使用 Nylas 进行身份验证的更多信息,请访问 开发者文档。
实际上,Nylas REST API 客户端将此简化为两步。
选项参数
$OPTIONS = [ 'app_id' => 'your app id', 'app_secret' => 'your app secret', 'app_server' => 'default https://api.nylas.com/', 'token' => 'token', 'debug' => 'true or false', ];
身份验证
index.php
$client = new Nylas($OPTIONS); $redirect_url = 'https://:8080/login_callback.php'; $get_auth_url = $client->oauth($OPTIONS)->createAuthURL($redirect_url); // redirect to Nylas auth server header("Location: ".$get_auth_url);
login_callback.php
$access_code = $_GET['code']; $client = new Nylas($OPTIONS); $get_token = $client->oauth($OPTIONS)->getAuthToken($access_code); // save token in session $_SESSION['access_token'] = $get_token;
获取线程
// init client! $client = new Nylas($OPTIONS); // Fetch the first thread $first_thread = $client->threads($OPTIONS)->first(); echo $first_thread->id; // Fetch first 2 latest threads $two_threads = $client->threads($OPTIONS)->all(2); // Fetch threads from offset 30, and limit 50 $part_threads = $client->threads($OPTIONS)->part(30, 50); foreach($two_threads as $thread) { echo $thread->id; } // List all threads with 'ben@nylas.com' $search_criteria = array("any_email" => "ben@nylas.com"); $get_threads = $client->threads($OPTIONS)->where($search_criteria)->items() foreach($get_threads as $thread) { echo $thread->id; }
处理线程
// List thread participants foreach($thead->participants as $participant) { echo $participant->email; echo $participant->name; } // Unread or Read $thread->unread($id, $unread = false); // Move to folder $thread->move($id, $target_id, $type = 'folder'); // Star $thread->starred($id, $starred = true); // Listing messages foreach($thread->messages()->items() as $message) { echo $message->subject; echo $message->body; }
处理文件
$client = new Nylas($OPTIONS); $file_path = '/var/my/folder/test_file.pdf'; $upload_resp = $client->files($OPTIONS)->create($file_path); echo $upload_resp->id;
处理草稿
$client = new Nylas($OPTIONS); $message = [ "to" => [ ["name" => "Nylas", "email" => "nylas@nylas.com"], ["name" => "Goole", "email" => "goole@google.com"], ], "subject" => "Hello, PHP!", "body" => "Test <br> message" ]; $draft = $client->drafts($OPTIONS)->create($message_obj); $send_message = $draft->send( ['id' => $draft->id] ); echo $send_message->id;
直接发送
请注意,直接发送与先创建草稿再发送不同。
$client = new Nylas($OPTIONS); $message = [ "to" => [ ["name" => "Nylas", "email" => "nylas@nylas.com"], ["name" => "Goole", "email" => "goole@google.com"], ], "subject" => "Hello, PHP!", "body" => "Test <br> message" ]; $draft = $client->drafts($OPTIONS)->send($message_obj);
处理事件
$client = new Nylas($OPTIONS); $calendars = $client->calendars($OPTIONS)->all(); $calendar = null; foeach($calendars as $i) { if(!$i->read_only) { $calendar = $i; } } $calendar_data = [ "title" => "Important Meeting", "location" => "Nylas HQ", "participants" => [ ["name" => "nylas", "email" => "nylas@nylas.com"] ] "calendar_id" => $calendar->id, "when" => array("start_time" => time(), "end_time" => time() + (30*60)) ]; // create event $event = $client->events($OPTIONS)->create($calendar_data); echo $event->id; // update $event = $event->update(array("location" => "Meeting room #1")); // delete event $event->delete(); // delete event (alternate) $remove = $client->events($OPTIONS)->find($event->id)->delete();
Webhooks 签名验证
$client = new Nylas($OPTIONS); $is_valid = $client->xSignatureVerification($code, $data, $app_secret)
结束
此 nylas-php 项目由 https://github.com/lanlin 和 https://github.com/samuelfa(支持 PHP 5.6)分支
这不是官方版本。官方版本有很多错误。
并且还不支持 guzzle >6.0。所以我分支了它,并进行了许多修改。
当然,我的分支中可能存在错误,但我没有太多时间在这个项目上。
因此,请帮助修复这些错误,欢迎拉取请求。