[gapi_search]

Description

Link: [gapi_search]
Author: Kyle Jessup
Category: Utility
Version: 8.x
License: Public Domain
Posted: Apr. 27, 2006
Updated: Apr. 27, 2006
More by this author...
This tag sends a query to Google via their Search API and parses the results.  Requires a valid Google API Key. The sample code below demonstrates how the tag could be used to search the contents of a specific site. For more information about what parameters can be supplied to the tag, and how to use the results, see <http://www.google.com/apis/reference.html#1_1>.

Parameters

-filter boolean, optional Activates or deactivates automatic results filtering.
-key string, required A valid Google API Key.
-lr string, optional Restricts the search to documents within one or more languages.
-maxResults integer, required Number of results desired per query. The maximum value per query is 10.
-q string, required The search string.
-restricts string, optional Restricts the search to a subset of the Google Web index.
-safeSearch boolean, optional Enables filtering of adult content in the search results.
-start integer, required Zero-based index of the first desired result.

Sample Usage

if(action_param('kwd'));
	// set defaults specific to this site
	var(
		'gapi_site' = 'www.example.com',
		'gapi_kwd' = action_param('kwd'),
		'gapi_max' = 10,
		'gapi_key' = 'a valid Google API key'
	);

	// set skip value	
	if(action_param('skip'));
		var('gapi_start') = integer(action_param('skip'));
	else;
		var('gapi_start' = 0);
	/if;

	// retrieve the results	
	protect;
		local('result' = gapi_search( 
			-q=($gapi_kwd + ' site:' + $gapi_site),
			-start=$gapi_start,
			-key=$gapi_key, 
			-maxResults=$gapi_max
		));

		var('gapi_found') = integer(#result->estimatedTotalResultsCount);
		
		if($gapi_start + $gapi_max < $gapi_found);
			var('gapi_shown' = $gapi_start + $gapi_max);
		else;
			var('gapi_shown' = $gapi_found);
		/if;
		
		'Displaying results <strong>' + ($gapi_start + 1) + '</strong> through <strong>' 
			+ $gapi_shown + '</strong> of <strong>' + $gapi_found + '</strong> for <strong>"'
			+ $gapi_kwd + '."</strong><br>\n<br>\n';
	
		iterate(#result->resultElements, local('i'));
			'
			<strong><a href="' + #i->url + '">' + #i->title + '</a></strong><br>
			' + #i->snippet + '<br>
			<br>
			';
		/iterate;
		
		'<div align="center"><small>';
		
		if($gapi_start > 0);
			'<a href="' + response_filepath + '?kwd=' + $gapi_kwd + '&skip=' + ($gapi_start - $gapi_max) + '">Previous</a>';
		/if;
		
		'   ';
		
		if($gapi_start + $gapi_max < $gapi_found);
			'<a href="' + response_filepath + '?kwd=' + $gapi_kwd + '&skip=' + ($gapi_start + $gapi_max) + '">Next</a>';
		/if;
		
		'</small></div>\n';
		
		handle_error;
			'<p>Oops! We had trouble interpreting the response from Google. Please try again.</p>';
		/handle_error;
	/protect;
/if;
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
soap_definetag(
	'search', 
	-namespace='gapi_',
	-wsdl=xml(include_url('http://api.google.com/GoogleSearch.wsdl')),
	-operationname='doGoogleSearch',
	-defaults=map(
		'filter'=false, 
		'restrict'='',
		'safeSearch'=false, 
		'lr'='',
		'ie'='',
		'oe'=''
	),
	-procs=array(
		proc_extractOne('*[1]/*[1]'), 
		\proc_convert
	)
);

 

Related Tags



Comments

08/04/2007, Dominique Guardiola
Error on startup
on OSX/Lasso 8.5.2, I get this errors after restarting with this tag : -9948 ERROR while running startup script "gapi_search.inc". No tag, type or constant was defined under the name null->find with arguments: array: (urn:GoogleSearch) at: loadSchema with params:
Email:


Password:



Newest

Most Popular