Google Suggestion Engine in Flex


Well, that is not a good title for a post I know, but that is what it got.

This time, after seeing a post on (http://www.aboutflex.net) where the author had a class for AIR (http://www.aboutflex.net/flex/flex-class-that-asks-google -for-suggestions /) who worked with the engine of suggestions google search for the words and suggestions to google, as I found very interesting then decided to try to go through the restriction of the Flash sandbox, which does not allow access to content from other sites that do not the server itself (possible only with a crossdomain.xml, which Google does not like), which resulted in a very simple script (in its final, that the tests were many) that allows serve as a gateway to this service from Google.

Look at the simple code:

Download: send.php
<? php
" http://www.google.com " ; $ host = "http://www.google.com";
$_GET [ ' q ' ] ; $ text = $ _GET [ 'q'];
$_GET [ ' cp ' ] ; $ len = $ _GET [ 'cp'];

str_replace ( " $ text = str_replace ( " " %20 " , $text ) ; ","% 20 ", $ text);

" /complete/search?hl=en&client=suggest&js=true&q= " . $text . " &cp= " . $len ; $ instruction = "/ complete / search? hl = en & client = suggest & js = & q = true." $ text. "& cp =". $ len;
$host . $instruction ; $ url = $ host. $ instruction;

if ) phpversion () >= 4.2 ) ((Double) phpversion ()> = 4.2)
(
' allow_url_fopen ' , ' 1 ' ) ; ini_set ( 'allow_url_fopen', '1');
)

file_get_contents ( $url ) ; $ s = @ file_get_contents ($ url);
if )) (Empty ($ s))
(
echo "";
)
else
(
echo $ s;
)
?>
Download: send.php

Simple huh?

The level of Flex, get the suggestions only class with the following:

Download: gsug.as
/ ** EN: Implementation of google sugestions class is from AIR
* Http://www.aboutflex.net/flex/flex-class-that-asks-google-for-suggestions/
*
* PT: Implementation of the class of google sugestions for AIR
* Http://www.aboutflex.net/flex/flex-class-that-asks-google-for-suggestions/
*
* Http://www.msdevstudio.com
*
* The send.php sould be placed on the same path of main. Swf file
* * /


package msdevstudio com. msdevstudio
(
import display . Sprite ; flash. display. Sprite;
import events . Event ; flash. events. Event;
import events . EventDispatcher ; flash. events. EventDispatcher;

import collections . ArrayCollection ; mx. collections. ArrayCollection;
import rpc . events . ResultEvent ; mx. rpc. events. ResultEvent;
import rpc . http . HTTPService ; mx. rpc. http. HTTPService;

( name = " suggested " , type = " flash.events.Event " )] [Event (name = "suggested", type = "flash.events.Event")]

public class gsug extends Sprite
(
public const String = ' suggested ' Suggested: String = 'suggested'
private var ArrayCollection = new _dataProvider: ArrayCollection = new ; ArrayCollection ();

public function gsug ()
(
)

public function string : String ) : void { findSuggests (string: String): void (
var HTTPService = new pm: HTTPService = new ; HTTPService ();
/ * use our server-side script send.php to get suggestions * /

url = " send.php?q= " + string + " &cp= " + string . length ; pm. url = "send.php? q =" string + + "& cp =" + string. length;

addEventListener ( ResultEvent . RESULT , parseResults ) pm. addEventListener (ResultEvent. RESULT, parseResults)

send () pm. send ()
)

protected function e : ResultEvent ) : void { parseResults (e: ResultEvent): void (

var Array = e . result . split ( ' new Array ' )[ 1 ] . split ( ' , " ' ) ; tmp: Array = e. result. Split ( 'new Array'). [1] split ( '');
new _dataProvider = new ; ArrayCollection ();
is (Var int = 1 ; i < tmp . length - 1 ; i += 2 ){ i: int = 1; i <tmp. length - 1; i + = 2) (

i ] = tmp [ i ] . replace ( ' " ' , "" ) ; tmp [i] = tmp [i]. replace ( '', "");
i ] = tmp [ i ] . replace ( ' \\ x27 ' , " ' " ) ; tmp [i] = tmp [i]. replace ( '\ \ x27', " '");
addItem ( tmp [ i ]) ; _dataProvider. AddItem (tmp [i]);

)
dispatchEvent (new ' suggested ' )) ; Event ( 'suggested'));
)

public function get : ArrayCollection { DataProvider (): ArrayCollection (
return _dataProvider;
)

)
)
Download: gsug.as

They can see the example to work here with source code available

For use in applications simply replace AIR:

hs.url = "send.php? q =" string + + "& cp =" + string.length;

by:

hs.url = "http://www.google.com/complete/search?hl=en&client=suggest&js=true&q =" string + + "& cp =" + string.length;

Perhaps it is useful in some applications!

Hug.

pdf version temporarily unavailable.
Leave a comment or Leave a Trackback

3 Comments

  1. July 22, 2008 at 12:30 pm | Permalink

    Hi Mario,
    PHP using the bridge is a great idea!
    I'll use it for my next example!

  2. July 23, 2008 at 10:13 a.m. am | Permalink

    Hi,
    As I said, I've written a little application that uses your bridge and the Adobe AutoComplete component;
    this is the link

    Let me know what is your impression

    Cheers,
    Fedele

  3. July 23, 2008 at 11:37 a.m. am | Permalink

    Hi, i left you a comment, but i repeat, now it really seems to be professional and very useful component! :)

    Cheers!

Leave a comment

Your email is never published or shared. Required fields are marked with an *