Last active
December 20, 2015 13:19
-
-
Save leblanc-simon/6137511 to your computer and use it in GitHub Desktop.
Shaarli API usage and test
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 | |
/* | |
Usage of API : | |
- Search / Get links : | |
* method: GET | |
* URL : ?do=api (add &token=[hash_hmac(sha256, api_key, api_hash)] to add private link in search) | |
* return: JSON (object or array) | |
* &key=[linkdate]: | |
- search by linkdate | |
- return unique link (JSON object) or 404 status code | |
* &hash=[smallHash]: | |
- search by the small hash | |
- return unique link (JSON object) or 404 status code | |
* &url=[url]: | |
- search by URL saved | |
- return unique link (JSON object) or 404 status code | |
* &tags=[tag tag2]: | |
- search by tags | |
- return an array of JSON object | |
* &search=[free text]: | |
- search by free text | |
- return an array of JSON object | |
* &day=[Ymd]: | |
- search by date | |
- return an array of JSON object | |
- Add a new link: | |
* method: POST | |
* URL: ?do=api&token=[hash_hmac(sha256, api_key, api_hash)] | |
* POST parameters: | |
- url (required) | |
- title | |
- description | |
- private (0 for public, else for private) | |
- tags | |
* return status: | |
- 201: link added | |
- 400: bad parameter (url is missing) | |
- 403: bad authentification (bad token in url) | |
* return content: JSON object of the new link | |
- Edit a link | |
* method: POST | |
* URL: ?do=api&key=[linkdate]&token=[hash_hmac(sha256, api_key, api_hash)] | |
* POST parameters: | |
- url (required) | |
- title | |
- description | |
- private (0 for public, else for private) | |
- tags | |
* return status: | |
- 200: link edited | |
- 400: bad parameter (url is missing) | |
- 403: bad authentification (bad token in url) | |
- 404: impossible to find the link with the $_GET['key'] | |
* return content: JSON object of the edited link | |
- Delete a link | |
* method: DELETE | |
* URL: ?do=api&key=[linkdate]&token=[hash_hmac(sha256, api_key, api_hash)] | |
* return status: | |
- 200: link deleted | |
- 403: bad authentification (bad token in url) | |
- 404: impossible to find the link with the $_GET['key'] | |
*/ | |
include __DIR__.'/data/config.php'; | |
function getData($url) | |
{ | |
return json_decode(file_get_contents($url)); | |
} | |
$token = hash_hmac('sha256', $GLOBALS['api_key'], $GLOBALS['api_hash']); | |
$url_public = 'http://localhost/Shaarli/?do=api'; | |
$url_private = 'http://localhost/Shaarli/?do=api&token='.$token; | |
// List all links (only public) | |
echo "All public links\n"; | |
print_r(getData($url_public)); | |
// List all links (public and private) | |
echo "All links\n"; | |
print_r(getData($url_private)); | |
// Search links | |
$searchs = array( | |
'key' => '20110914_190000', // Search by key (Ymd_His) | |
'hash' => 'kaJ9Rw', // Search by hash (small hash) | |
'url' => 'http://sebsauvage.net/wiki/doku.php?id=php:shaarli', // Search by url (URL save in Shaarli) | |
'tags' => 'secretstuff', // Search by tags | |
'search' => 'delete', // Search by free text | |
'day' => '20110914', // Search by date | |
); | |
echo "Search ...\n"; | |
foreach ($searchs as $key => $value) { | |
// In public | |
echo "Public search for $key ...\n"; | |
print_r(getData($url_public.'&'.$key.'='.urlencode($value))); | |
// In private | |
echo "Private search for $key ...\n"; | |
print_r(getData($url_private.'&'.$key.'='.urlencode($value))); | |
} | |
// Post a new link | |
$curl = curl_init($url_private); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, array( | |
'url' => 'https://github.com/sebsauvage/Shaarli', | |
'title' => 'Shaarli on Github', | |
'description' => 'View Shaarli on Github', | |
'private' => 1, | |
'tags' => 'shaarli', | |
)); | |
$output = curl_exec($curl); | |
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
if ($status_code == 201) { | |
$data = json_decode($output); | |
echo "New link posted\n"; | |
print_r($data); | |
} else { | |
echo "Fail to post link, status code : $status_code\n"; | |
die($output); | |
} | |
// Edit a link | |
$curl = curl_init($url_private.'&key='.urlencode($data->linkdate)); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, array( | |
'url' => 'https://github.com/sebsauvage/Shaarli', | |
'title' => 'Shaarli on Github 2', | |
'description' => 'View Shaarli on Github 2', | |
'private' => 0, | |
'tags' => 'shaarli', | |
)); | |
$output = curl_exec($curl); | |
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
if ($status_code == 200) { | |
$data = json_decode($output); | |
echo "Link edited\n"; | |
print_r($data); | |
} else { | |
echo "Fail to edit link, status code : $status_code\n"; | |
die($output); | |
} | |
// Delete a link | |
$curl = curl_init($url_private.'&key='.urlencode($data->linkdate)); | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
$output = curl_exec($curl); | |
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
if ($status_code == 200) { | |
$data = json_decode($output); | |
echo "Link deleted\n"; | |
} else { | |
echo "Fail to delete link, status code : $status_code\n"; | |
die($output); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment