Skip to content

Instantly share code, notes, and snippets.

@jazzsequence
Created February 27, 2015 23:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jazzsequence/95e40ec6a2a3faf9e04e to your computer and use it in GitHub Desktop.
Save jazzsequence/95e40ec6a2a3faf9e04e to your computer and use it in GitHub Desktop.
Yourls workflow for Alfred modified to use secure signature token
// config
$yourls_domain = 'CHANGE_ME'; // your yourls domain (like your.ls)
$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