Search Google in Flex 2

Important: The Google SOAP Search API is deprecated. You may not be able to use the source code for this project anymore! Check out the Google Search REST API for a replacement API to Google’s search results. The documentation includes examples in ActionScript 3 for using the JSON returned from this new API.

In my early days of learning Flash, I put together a cool little demo that used Google’s Search API. The SWF is lost somewhere in the depths of my hard drive, but I remembered it the other day when I spent some time brainstorming my next cool Flex 2 experiment. Thanks to Flex’s robust framework, and my improved Flash-fu skills, I implemented a working Google search component pretty quickly. As a result, I spent some extra time on layout and styles. You’ll notice the distinctive Google simplicity.

Before you check out my Flex 2 Google Search experiment, take note that a Google API key only allows 1,000 searches per day. If the demo doesn’t seem to be working for you, please bookmark this page so that you can come back to try it again tomorrow.

The main API implementation can be found in the GoogleSearchEngine class. By default, GoogleSearchEngine uses Flex’s HTTPService class to connect directly to Google’s SOAP service. In the future, Apollo applications should be able to use this functionality directly. For now, it’s important to note that Flash restricts cross-domain access to SWFs served from the web. To work around this restriction, I added the target parameter which takes an URL for some sort of proxy that can communicate between Flash and the web service. The live demo uses a nice little proxy written in PHP that I found at mattism.com. You’ll find it in the source code.

Originally, I intended to make the live demo send searches through an AJAX system. I used a slightly modified version of the Prototype library. Unfortunately, I discovered that Javascript has limited cross-domain access too. If you’re interested, AjaxExtended allows you to bypass this restriction. It overrides the browser’s regular XmlHttpRequest object to route the messages through a PHP script. Sounds familiar, doesn’t it? In the end, I figured it would be pointless to go through the AJAX subsystem, and I made the Flex application speak to the proxy directly.

Source code is available for download. You are free to use and distribute it under the terms of an MIT-style license. Certainly, there’s no reason why you or I couldn’t use the interfaces I created to build a similar system for Yahoo’s Search API. Does anyone know if other search engines provide a public API?

About Josh Tynjala

Josh Tynjala is a frontend developer, open source contributor, bowler hat enthusiast, and karaoke addict. You might be familiar with his project, Feathers UI, an open source user interface library for Starling Framework that is included in the Adobe Gaming SDK.

Discussion

  1. pasindu

    hi thanks for ur API

    but i try to use it in my personal web site after geting your code uing flex builder2 i compile it, after run it from builder it work , but when i copy the bin folder in to any other place and try run but it gave error not serching

    can you help me for this

    thanks
    pasindu

  2. Josh Tynjala

    Are you pointing to the correct PHP proxy URL? In the source code I provided, it points to my server, but you need the PHP proxy on your own server.

    <search:GoogleSearchEngine id="google"
    developerKey="{this.devKey}"
    target="http://www.zeuslabs.us/demos/google/
    xml_proxy.php?url=http://api.google.com/search/beta2"/>

    You probably only need to change the target to point to your own server’s URL rather than zeuslabs.us.

  3. Josh Tynjala

    I’m not sure what you’re asking. Are you wondering if there’s a Google News API? Or are you wondering if I or anyone else has built a Flex application that somehow shows Google News?

  4. Josh Tynjala

    I see from your webpage that you seem to have found some sort of news solution, but I’ll add what I know about Google News:

    As far as I know, Google doesn’t actually have a Google News API. I believe that websites like Newsmap and others that leverage Google News data actually subscribe to the RSS or Atom feeds. I could be wrong though.

    One more thing. I see that your website is based on the style from my Google demo. Google can be protective of their design, so you should considering making your site look less like theirs. You might also want to consider making a Flex preloader to match the rest of the design. Ted Patrick has a good introduction to custom preloaders in Flex to get your started.

  5. Sandeep George

    hey there
    i worked on your google search app…
    it works great offline
    that is when the php is on a server somewhere far far away…
    but once when i get the code onto the server that is the swf files..
    it seems to be nonfunctional at once

    any idea why this might have happend

    can teh reply be posted back to me by my email?

  6. Frank Trying

    Hey, your Google project looked very nice and the description was clear too. Your demo is running, I have the xml_proxy test on my won server (with cnn headlines example). The assets are showing allright. But every time I hit search it generates a failure (that I probably used it too much). But thats not true. API is working is html. I am using your exact code, except xml-proxy and api-key of course. I would love to see this thingie run.

  7. Tom

    Very cool! Thanks for this article. I’m still looking around hoping that Google releases something for Flash9… I noticed that Yahoo released a Flash API (as2 AND as3) for their tools. Can only imagine that it’s a matter of time before Google does something better in response.

  8. Josh Tynjala

    That might be interesting Tom, but I don’t expect to see much for Flash coming from Google. They’re very focused on AJAX, almost to an extreme. The new Google Analytics has a lot of Flash stuff, though, so maybe we’ll see some sort of toolkit for their charts or something.

  9. Charly

    Hello Josh,

    Nice app !
    If sometimes you had few minutes, could you explain how you get more than ten results ?
    I had a glance to your code but didn’t figure out yet how you do this… I’m kind of gettin’ lost with all these Classes 🙁
    Or is there any way to bypass this “maxResults” with just a function ? (guess that maybe I’m asking the moon 🙂

    Thx by advance

  10. Tony

    Hello Josh,

    I try to use this nice code you have build but even if I have change the targets and use my API key I can’t make a search (“Search failed…”).
    Have you used some libs I haven’t got? Or is there others changes to make in your code?

    Thanks by advance

  11. Asha

    Hi,
    I have developed similar kind of google API on flex.
    But I am getting an error saying that “Definition SearchResultRenderer could not be found.”

    Any help would be appreciated.
    Thanks,
    Asha

  12. Nuwan

    Hi Josh, nice work!

    but i got a problem that,

    i got a new API from google i have put it to the appication, then i compile it, then when i search it says
    “Search Failed! This Google API account may have reached its search limit for today.”

    please help me with this,
    Thank you.