Skip to content

Instantly share code, notes, and snippets.

@keoshi
Created March 21, 2018 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keoshi/c5cab219abdd22b93bce4e0bb83e0202 to your computer and use it in GitHub Desktop.
Save keoshi/c5cab219abdd22b93bce4e0bb83e0202 to your computer and use it in GitHub Desktop.
Alfred workflow script for getting shortened URL from Yourls API
<?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 . '&timestamp=' . $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