こんな感じです。
yuis@DESKTOP-UHU8FSH:/mnt/c/pg$ yourls "fumiya.org/hogefuga"
yuis@DESKTOP-UHU8FSH:/mnt/c/pg$ getredirected https://114514.click/20190104220817bp5by
http://fumiya.org/hogefugayuis@DESKTOP-UHU8FSH:/mnt/c/pg$
yuis@DESKTOP-UHU8FSH:/mnt/c/pg$ # https://114514.click/20190104220817bp5by
yuis@DESKTOP-UHU8FSH:/mnt/c/pg$
yuis@DESKTOP-UHU8FSH:/mnt/c/pg$
ここにAPIの雛形があります。
Remote API · YOURLS/YOURLS Wiki
yourlsのインストールは前提です。yourlsのインストールとかは当ブログでも解説記事がありますのでそちらをご参照ください。
では、簡単な解説。
/mnt/c/pg/yourls-api.php
を作ります。で、以下をコピペ。EDIT THIS
のうち、username,password,api_urlを各自入れてください。ユーザー名とパスワードは、user/config.php
に書いてあります。adminにログインするのに必要な情報です。もしパスワードがわからなければ、user/config.php
のpasswordが暗号化されているので、消して新しいパスワードにして上書き保存してやればいいです。
<?php
///////////////////////////////////////////////////////////////////
/////////////////////////// CONFIGURATION /////////////////////////
///////////////////////////////////////////////////////////////////
// C:\pg\yourls-api.php http://fumiya.org/hoge01 keyword title
// php yourls-api.php http://fumiya.org/hoge01 keyword title
// EDIT THIS: your auth parameters
$username = '';
$password = '';
// EDIT THIS: the query parameters
$url = $argv[1] ; // 'http://fumiya.org/'; // URL to shrink $1
$keyword = $argv[2] ; // 'plaindate+random6'; // optional keyword
$title = $argv[3] ; // 'ad::aliexpress'; // $2 (d=no-title) // optional, if omitted YOURLS will lookup title with an HTTP request
$format = 'json'; // output format: 'json', 'xml' or 'simple'
// EDIT THIS: the URL of the API file
$api_url = 'http://yourdomain.com/yourls-api.php';
///////////////////////////////////////////////////////////////////
///////////////////////////// SCRIPTS /////////////////////////////
///////////////////////////////////////////////////////////////////
// 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
'url' => $url,
'keyword' => $keyword,
'title' => $title,
'format' => $format,
'action' => 'shorturl',
'username' => $username,
'password' => $password
) );
// Fetch and return content
$data = curl_exec($ch);
curl_close($ch);
// Do something with the result. Here, we just echo it.
echo $data;
この時点で、cd /mnt/c/pg/ && php yourls-api.php http://fumiya.org/hoge01 keyword title
といった感じで正常に動くかテストしておきましょう。curlの結果としてjsonが返ってきますが、エラーなどが含まれていないか確認しましょう。
Uncaught Error: Call to undefined function curl_init()
ってエラーが出る場合があります。
php – Fatal error: Call to undefined function curl_init() in … on line 9 – Stack Overflow
windowsならphp.iniをいじるらしいですが、ubuntu16.04とかなら、sudo apt install php-curl
してやると僕は治りました。
はい。で、関数定義します。以下。(.bashrc)
先程のメインPHPファイルに引数を渡しつつ、結果として返ってくるjsonをpythonでパースして、shorturlを取得。shorturlは完成した短縮URLになります。でさらに、clip.exeに渡してクリップボードにコピーしています。
yourls(){
url=$1
keyword="$(plaindate)$(random 6)"
title=$2
php /mnt/c/pg/yourls-api.php "$url" "$keyword" "$title" | python3 -c 'import json,sys;print(json.load(sys.stdin)["shorturl"])' | clip.exe
}
使いかた:
yourls [短縮したいURL] [タイトル*1]
1: adminに表示される。検索する時にあると便利。
e.g. yourls "fumiya.org/hogefuga"
e.g. yourls "fumiya.org/hogefuga" "hogefuga"
adminパネルにはこんな感じで表示されます。
ここでは紹介していないplaindate,random,getredirectedなどの関数については僕の.bashrc
に定義してあります。.bashrcは僕の[github]で公開しています。(検索して見つけてください。)