Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Yourls workflow for Alfred modified to use secure signature token
// config
$yourls_domain = 'CHANGE_ME'; // your yourls domain (like
$signature = 'CHANGE_ME'; // secure signature token for passwordless api call
// stop config
if ( $yourls_domain == 'CHANGE_ME' || $signature == 'CHANGE_ME' )
die( 'Yourls Workflow is not configured' );
$q = '{query}'; //the query from Alfred
function make_short_url( $url, $domain, $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?signature=' . $signature . '&action=shorturl&keyword='. $keyword .'&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, $yourls_domain, $signature );
die( $short );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment