Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wunderlist API
<?php
require_once './WunderList.php';
$client_id = '/* Client Id */';
$client_secret = '/* Client Secret */';
$redirect_url = '/* Auth Callback URL */';
$state = '/* STATE */';
$targetlist = '/* タスクを登録するリスト名 */';
$title = '/* 登録したいタスク名 */';
$due_date = '/* 期限日 */';
$wunderlist = new WunderList($client_id, $client_secret, $redirect_url, $state);
// リスト取得
$list_url = 'https://a.wunderlist.com/api/v1/lists';
$list = $wunderlist->request($list_url, 'GET');
$list_id = '';
foreach($list as $row){
if($row['title'] == $targetlist){
$list_id = $row['id'];
break;
}
}
if($list_id == ''){
exit('該当のリストは見つかりませんでした');
}
// タスクの追加
$task_url = 'https://a.wunderlist.com/api/v1/tasks';
$params = array(
'list_id' => $list_id,
'title' => $title,
'due_date' => $due_date
);
$ret = $wunderlist->request($task_url, 'POST', $params);
<?php
class WunderList
{
protected $client_id;
protected $client_secret;
protected $redirect_url;
protected $state;
protected $access_token;
protected $header = array();
public function __construct($client_id, $client_secret, $redirect_url, $state, $access_token='')
{
// 設定値の代入
$this->client_id = $client_id;
$this->client_secret = $client_secret;
$this->redirect_url = $redirect_url;
$this->state = $state;
$this->access_token = $access_token;
if($this->access_token == ''){
$this->setAccessToken();
}
// API通信用のヘッダーを設定
$this->header = array(
"X-Client-ID: {$this->client_id}",
"X-Access-Token: {$this->access_token}"
);
}
// アクセストークンの取得
private function setAccessToken()
{
$code = isset($_GET['code']) ? $_GET['code'] : '';
$state = isset($_GET['state']) ? $_GET['state'] : '';
if($code == ''){
// 初回リクエスト(code値を取得)
$params = array(
'client_id' => $this->client_id,
'state' => $this->state
);
header("Location: https://www.wunderlist.com/oauth/authorize?redirect_uri={$this->redirect_url}&".http_build_query($params));
exit();
}
else{
// アクセストークン取得リクエスト(リダイレクト後)
if($state != $this->state){
exit('リダイレクトされたstateの値が設定のものと一致しません');
}
$token_url = 'https://www.wunderlist.com/oauth/access_token';
$params = array(
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'code' => $code
);
$response = $this->request($token_url, 'POST', $params, false);
$this->access_token = $response['access_token'];
}
}
// APIリクエストのラッパーメソッド
public function request($url,$method,$params=array(),$json=true)
{
$header = $this->header;
if($json){
$header[] = "Content-Type: application/json; charset=utf-8";
$data = json_encode($params);
}
else{
$header[] = "Content-Type: application/x-www-form-urlencoded";
$data = http_build_query($params);
}
$header[] = "Content-Length: ".strlen($data);
$context = array(
"http" => array(
"method" => $method,
"header" => implode("\r\n", $header),
"content" => $data
)
);
$result = file_get_contents($url, false, stream_context_create($context));
return json_decode($result, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.