nylas/nylas-php

Nylas API的PHP包装器

dev-master 2016-02-05 21:49 UTC

This package is not auto-updated.

Last update: 2024-10-02 09:19:30 UTC


README

Nylas REST API的PHP绑定 https://www.nylas.com

注意:Nylas PHP SDK目前未积极维护,可能需要一些关注。然而,我们的RubyNodePython SDK已完全支持。

请随意使用它,如果您修复了任何问题或添加了新功能,也请向我们发送pull request。:)

安装

您可以通过运行以下命令安装库:

cd nylas-php
composer install

用法

Nylas REST API使用服务器端(三腿)OAuth,这个库提供了简化OAuth过程的便利方法。以下是工作原理:

  1. 您将用户重定向到我们的登录页面,并附带您的App Id和Secret
  2. 用户登录
  3. 她将被重定向到您的回调URL,并附带一个访问码
  4. 您使用这个访问码来获取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对象时,请为App ID、App Secret和API Token提供null,并传递您同步引擎副本的完全限定地址

$client = new Nylas(CLIENT, SECRET, TOKEN, 'https://:5555/');

贡献

我们很乐意您帮助我们改进Nylas。加入Google Group以获取项目更新和功能讨论。我们还在irc.freenode.net#nylas频道闲逛,或者您可以通过support@nylas.com发送邮件。

在提交pull request之前,请签署贡献者许可协议。(它与其他项目类似,如NodeJS或Meteor。)