Created
May 26, 2015 03:04
-
-
Save faryne/fabf40bda393a1051ce6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$api_key = 'xxxxxxxxxxxxx'; // 在註冊帳號後進入dashboard就可以看到了 | |
$collection = 'sample'; // 資料放置/讀取的容器 | |
// 存取API的路徑 | |
$base_url = 'https://api.orchestrate.io/v0/'.$collection; | |
// 塞入資料 | |
$key = 'sample'; // 存放資料時作為識別的key | |
$data_insert = array('a'=>'1', 'b'=>'2', 'c'=>'3'); | |
$ch = curl_init($base_url.'/'.$key); | |
curl_setopt_array($ch, array( | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_CUSTOMREQUEST => 'PUT', // 因為是更新資料所以要用PUT 不過如果是部份資料更新時可以用PATCH | |
CURLOPT_POSTFIELDS => json_encode($data_insert), // 資料要用json_encode編過才丟過去 | |
CURLOPT_HTTPHEADER => array('Content-Type: application/json'), // 將檔頭設為json | |
CURLOPT_USERPWD => ($api_key.':'), // 因為使用HTTP驗證,所以必須這麼寫。也可以把驗證部份寫到Header內 | |
)); | |
$result = curl_exec($ch); | |
$info = curl_getinfo($ch); // 因為要判斷是否新增/修改成功,所以必須藉此取回回傳的HTTP狀態。當拿到的http_code為201時代表新增成功 | |
curl_close($ch); | |
// 取出指定key的資料 | |
$key = 'sample'; // 存放資料時作為識別的key | |
$ch = curl_init($base_url.'/'.$key); | |
curl_setopt_array($ch, array( | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_USERPWD => ($api_key.':'), // 因為使用HTTP驗證,所以必須這麼寫。也可以把驗證部份寫到Header內 | |
)); | |
$result = curl_exec($ch); | |
$info = curl_getinfo($ch); // 因為要判斷是否讀得到,所以必須藉此取回回傳的HTTP狀態。當拿到的http_code為404時代表找不到 | |
curl_close($ch); | |
// -- 取回的資料會是一個json,所以要記得作json_decode | |
// 搜尋 | |
$params = array( | |
'query' => '*:*', // 代表搜索全部,妳可以根據妳塞入資料的欄位作為搜尋條件,這裡支援Lucene的搜尋語法 | |
'sort' => 'a:DESC', // 以哪個欄位作排序,格式「[欄位]:[ASC|DESC]」 | |
'offset' => '0', // 搜尋資料的起點 | |
'limit' => 10, // 每頁列出幾筆, | |
'aggregate' => '', // 根據指定的欄位記統計方式,將資料群組化,這部份可以參閱官方文件 | |
); | |
$ch = curl_init($base_url.'?'.http_build_query($params)); | |
curl_setopt_array($ch, array( | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_USERPWD => ($api_key.':'), // 因為使用HTTP驗證,所以必須這麼寫。也可以把驗證部份寫到Header內 | |
)); | |
$result = curl_exec($ch); | |
$info = curl_getinfo($ch); | |
curl_close($ch); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment