o323 / nylas-php
Nylas API的PHP封装
0.1.2
2016-02-09 10:01 UTC
Requires
- php: >=5.4
- guzzlehttp/guzzle: >=4
Requires (Dev)
- phpunit/phpunit: ~4
This package is not auto-updated.
Last update: 2024-09-14 19:19:56 UTC
README
Nylas REST API的PHP绑定 https://www.nylas.com
安装
您可以通过运行以下命令来安装库
composer require o323/nylas-php
用法
Nylas REST API使用服务器端(三脚)OAuth,并且这个库提供了简化OAuth过程的便利方法。以下是工作原理:
- 您将用户重定向到我们的登录页面,并带上您的应用ID和密钥
- 用户登录
- 她将被重定向到您自己的回调URL,并带有访问码
- 您使用此访问码从API获取授权令牌
有关使用Nylas进行身份验证的更多信息,请访问开发者文档。
在实际应用中,Nylas REST API客户端将这个过程简化为两步。
身份验证
index.php
$client = new Nylas(CLIENT, SECRET); $redirect_url = 'https://:8080/login_callback.php'; $get_auth_url = $client->createAuthURL($redirect_url); // redirect to Nylas auth server header("Location: ".$get_auth_url);
login_callback.php
$access_code = $_GET['code']; $client = new Nylas(CLIENT, SECRET); $get_token = $client->getAuthToken($access_code); // save token in session $_SESSION['access_token'] = $get_token;
获取账户信息
$client = new Nylas(CLIENT, SECRET, TOKEN); $account = $client->account(); echo $account->email_address; echo $account->provider;
获取线程
$client = new Nylas(CLIENT, SECRET, TOKEN); // Fetch the first thread $first_thread = $client->threads()->first(); echo $first_thread->id; // Fetch first 2 latest threads $two_threads = $client->threads()->all(2); 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()->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; } // Mark as Read $thread->markAsRead(); // Mark as Seen $thread->markAsSeen(); // Archive $thread->archive(); // Unarchive $thread->unarchive(); // Trash $thread->trash(); // Star $thread->star(); // Unstar $thread->unstar(); // Add or remove arbitrary tags $to_add = array('cfa1233ef123acd12'); $to_remove = array('inbox'); $thread->addTags($to_add); $thread->removeTags($to_remove); // Listing messages foreach($thread->messages()->items() as $message) { echo $message->subject; echo $message->body; }
处理文件
$client = new Nylas(CLIENT, SECRET, TOKEN); $file_path = '/var/my/folder/test_file.pdf'; $upload_resp = $client->files()->create($file_path); echo $upload_resp->id;
处理草稿
$client = new Nylas(CLIENT, SECRET, TOKEN); $person_obj = new \Nylas\Models\Person('Kartik Talwar', 'kartik@nylas.com'); $message_obj = array( "to" => array($person_obj), "subject" => "Hello, PHP!", "body" => "Test <br> message"); $draft = $client->drafts()->create($message_obj); $send_message = $draft->send(); echo $send_message->id;
处理事件
$client = new Nylas(CLIENT, SECRET, TOKEN); $calendars = $client->calendars()->all(); $calendar = null; foeach($calendars as $i) { if(!$i->read_only) { $calendar = $i; } } $person_obj = new \Nylas\Models\Person('Kartik Talwar', 'kartik@nylas.com'); $calendar_obj = array("title" => "Important Meeting", "location" => "Nylas HQ", "participants" => array($person_obj), "calendar_id" => $calendar->id, "when" => array("start_time" => time(), "end_time" => time() + (30*60))); // create event $event = $client->events()->create($calendar_obj); echo $event->id; // update $event = $event->update(array("location" => "Meeting room #1")); // delete event $event->delete(); // delete event (alternate) $remove = $client->events()->find($event->id)->delete();
开源同步引擎
Nylas Sync Engine是开源的,您也可以使用PHP库与开源API一起使用。由于开源API不提供身份验证或安全措施,连接到它很简单。当您实例化Nylas对象时,提供null作为应用ID、应用密钥和API令牌,并传递同步引擎副本的完全限定地址
$client = new Nylas(CLIENT, SECRET, TOKEN, 'https://:5555/');
贡献
我们非常欢迎您的帮助,使Nylas变得更好。加入Google Group以获取项目更新和功能讨论。我们还在irc.freenode.net的#nylas频道闲逛,或者您可以发送邮件至support@nylas.com。
在提交pull请求之前,请签署贡献者许可协议。(它与NodeJS或Meteor等项目类似。)