Yourls API を使って URL を短縮する

自分で立てる短縮 URL サービスとして Yourls を使っている。

API を使って URL を短縮してみた。

(1)Yourls の管理画面で「Tools>Secure passwordless API call」の「Your secret signature token」にかかれている文字列を確認する。

(2)試しに以下のコマンドを実行してみる。

$ curl -X POST -d 'url=https://movies.yahoo.co.jp/' -d 'action=shorturl' -d 'format=json' -d 'signature=[1の secret signature token]' https:/[Yourls の URL])/yourls-api.php | sed 's/\\\u\(....\)/\&#x\1;/g' | nkf -w --numchar-input

これで JSON で短縮した URL やタイトル等が返ってくる。コマンドの最後に sed... と付けないと、タイトルに日本語が含まれる場合に文字化けしてしまう。

参考:
curlのレスポンスをデコードする - Qiita
https://qiita.com/ponsuke0531/items/9f798a9193a347266ff7

YOURLS - Your Own URL Shortener
http://demo.topfreelancer.co.uk/yourls/yourls-api/

Passwordless API | YOURLS Documentation
https://docs.yourls.org/guide/advanced/passwordless-api.html


この記事が気に入ったらサポートをしてみませんか?