Created
March 21, 2018 21:38
-
-
Save keoshi/c5cab219abdd22b93bce4e0bb83e0202 to your computer and use it in GitHub Desktop.
Alfred workflow script for getting shortened URL from Yourls API
This file contains hidden or 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 | |
// Costumize your params here | |
$domain = 'CHANGE_ME'; | |
$token = 'CHANGE_ME'; | |
// Don't change anything below this line | |
$timestamp = time(); | |
$signature = md5( $timestamp . $token ); | |
if ( $domain == 'CHANGE_ME' || $token == 'CHANGE_ME' ) | |
die( 'Yourls Workflow is not configured' ); | |
$q = '{query}'; | |
function make_short_url( $url, $domain, $timestamp, $signature ) { | |
//force add http:// | |
$url = 'http://'.$url; | |
//remove doubles | |
$url = str_replace( 'http://http', 'http', $url ); | |
//use star as separator, 2nd segment is explicit keyword for short url | |
$url = explode( '*', $url ); | |
//if keyword, set it | |
$keyword = isset( $url[1] ) ? $url[1] : ''; | |
//long url should always be first in the exploded array | |
$url = array_shift( $url ); | |
//create the URL | |
$api = 'http://' . $domain . '/yourls-api.php?action=shorturl&keyword=' . $keyword . '×tamp=' . $timestamp . '&signature=' . $signature . '&url=' . urlencode( $url ) . '&format=json'; | |
$response = file_get_contents($api); | |
$json = @json_decode($response,true); | |
return print_r( $json['shorturl'], true ); | |
} | |
$short = make_short_url( $q, $domain, $timestamp, $signature ); | |
die( $short ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment