Web API の機能
- ランダムもしくはカスタムキーワードによる短縮 URL の登録・確認
- 出力フォーマット: JSON, XML, simple raw text
- ユーザごとのシグネチャ・トークンによる認証
使用方法
Web API の URL はhttps://u.kyoto-u.jp/yourls-api.php
です.
GET
か POST
で下記パラメータを指定してアクセスすることで,目的の機能を実行可能です.
(GET の場合,パラメータ値の URL エンコードをすること)
signature
: 認証用のシグネチャ・トークン (短縮 URL 管理ページのメニューにある
Tools)
の「Secure passwordless API call」を参照してください)
email
: 全学メールアドレス
action
:
"shorturl" (短縮 URL の登録),
"expand" (短縮前の URL の取得),
"url-stats" (ある短縮 URL の統計情報の取得),
- action = "shorturl" の追加パラメータ:
url
: 短縮前のURL
format
: 出力フォーマット"jsonp", "json", "xml", "simple" (省略した場合: "xml")
keyword
(オプション): ('http(s)://u.kyoto-u.jp/hoge' として登録したい場合,'hoge')
title
(オプション): 短縮 URL に対するタイトル (管理ページに表示される)
action = "expand" の追加パラメータ:
shorturl
対象の短縮 URL のキーワード ('http(s)://u.kyoto-u.jp/hoge' の場合,"hoge")
format
: 出力フォーマット"jsonp", "json", "xml", "simple" (省略した場合: "xml")
action = "url-stats" の追加パラメータ:
shorturl
: 対象の短縮 URL のキーワード ('http(s)://u.kyoto-u.jp/hoge' の場合,"hoge")
format
: 出力フォーマット"jsonp", "json", "xml" (省略した場合: "xml")
使用例
JavaScript (jQuery) を使用した GET メソッドによる 'http://hoge.kyoto-u.ac.jp/id=xxxxxxxxxxxxxxxxxxxx' に対する短縮 URL の登録
var api_url = 'https://u.kyoto-u.jp/yourls-api.php';
var response = $.get( api_url, {
signature: "xxxxxxx",
email: "xxxxxxx@kyoto-u.ac.jp",
action: "shorturl",
format: "json",
url: "http://hoge.kyoto-u.ac.jp/id=xxxxxxxxxxxxxxxxxxxx"
},
// callback function that will deal with the server response
function( data) {
// for instance, return short URL
return data.shorturl;
});
PHP を使用した POST メソッドによる対象短縮 URL ('hoge') の短縮前の URL 取得
<?php
$signature = 'xxxxxxx';
$email = 'xxxxxxx@kyoto-u.ac.jp';
$api_url = 'httsp://u.kyoto-u.jp/yourls-api.php';
// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST
'shorturl' => 'hoge',
'format' => 'json',
'action' => 'expand',
'signature' => $signature,
'email' => $email
));
// Fetch and return content
$data = curl_exec($ch);
curl_close($ch);
// Do something with the result. Here, we echo the long URL
$data = json_decode( $data );
echo $data->longurl;
出力例
JSON 形式での出力例 (action="shorturl"
)
{
"url": {
"keyword": "hoge",
"url": "http:\/\/hoge.kyoto-u.ac.jp\/id=xxxxxxxxxxxxxxxxxxxx",
"title": "hoge",
"date": "2016-08-24 16:01:39",
"ip": "127.0.0.1"
},
"status": "success",
"message": "http:\/\/hoge.kyoto-u.ac.jp\/id=xxxxxxxxxxxxxxxxxxxx added to database",
"keyword": "hoge",
"shorturl": "http:\/\/u.kyoto-u.jp\/hoge",
"statusCode": 200
}
XML 形式での出力例 (action="expand"
)
<result>
<keyword>hoge</keyword>
<shorturl>http://u.kyoto-u.jp/hoge</shorturl>
<longurl>http://hoge.kyoto-u.ac.jp/id=xxxxxxxxxxxxxxxxxxxx</longurl>
<message>success</message>
<statusCode>200</statusCode>
</result>