Jump to content

Script Filter and JSON


Recommended Posts

Posted (edited)

I am trying to use a JSON encoded PHP array to supply autosuggestion results to Alfred from a PHP file I have control over. This is the JSON data I am returning from my a request such as this:

 

http://myserver.com/search/autocomplete.php?artist=The

{"The Bulletproof Tiger":"245461","The Good Earth Project":"245462","The Skywalker Symphony Chorus":"891858","The Gaslamp Killer":"46450","The Buchanans":"684229","The High Llamas":"17170","The Pastels":"19504","The Lower Mills":"589814","The Head and the Heart":"392379","The Super 3":"697422"}

This what my Alfred script looks like so far

require_once('workflows.php');
$wf = new Workflows();

$orig = "{query}";
$transport = $wf->request("http://http://myserver.com/search/autocomplete.php?artist=".urlencode($orig));
$suggestions = json_decode($transport);

foreach($suggestions as $name => $id):
	$wf->result($id, $id, $name, $name, 'icon.png');
endforeach;

$results = $wf->results();
if ( count( $results ) == 0 ) {
	$wf->result( 'googlesuggest', $orig, 'No Suggestions', 'No search suggestions found. Search Google for '.$orig, 'icon.png' );

echo $wf->toxml();

This is my first time really dealing with PHP or Alfred scripts, I feel like I am close but just missing something tiny. What am I missing? Thanks for the help!

Edited by nickmorri
Posted

Do you know you have "http://" twice in your PHP's URL?

 

By the way, you don't need the last bit of code to produce the Google Search result. If Alfred gets nothing back, he will automatically fall back to a Google Search (or whatever your fallback searches are set to)

Posted

Oh boy, certainly didn't notice that till just now. I seem to have got it to work with this. Thank you for the Google tip as well.

require_once('workflows.php');
$wf = new Workflows();

$orig = "{query}";
$transport = $wf->request("http://myserver.com/whatsearch/autocomplete.php?artist=".urlencode($orig));
$suggestions = json_decode($transport);

foreach($suggestions as $name => $id) {
	$wf->result($id, $id, $name, 'Search Site for '.$name, 'icon.png');
}

echo $wf->toxml();

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...