Archive forGoogle

Google AJAX Feed API

Google has just launched their AJAX Feed API:

With the AJAX Feed API, you can download any public Atom or RSS feed using only JavaScript, so you can easily mash up feeds with your content and other APIs.

It requires JavaScript knowledge to integrate with any website, but it's all documented by Google and not hard to do.

Comments

Google AdSense API

Google has just opened for all developers their AdSense API introduced last year:

If you're a web developer or host, you may remember that we introduced you to the AdSense API last May. With the AdSense API, your users can create their own AdSense accounts on your site, and display ads alongside the content that they have created. They can also manage their accounts and view ad performance and earnings reports, all on your site.

Today, we're pleased to announce that the AdSense API is now open for all developers, with the release of our open development sandbox. This is a replica of the live service with some additional support to help you test and debug your applications. Once you implement the AdSense API in the development sandbox, we can go live with your implementation.

Using this API you can share advertising revenues with creating content online users without webnasters worrying too much about accounting and money, all managed by Google. It looks liek a win-win situation for everybody.

Some already fear that AdSense API will skyrocket click-fraud. However this is a sample account creatin using PHP straight from their online documentation:

<?php

// Copyright 2005, Google Inc. All rights reserved.

/**
 *  sample code to create an Adsense account through Adsense API
 */

require_once('lib/nusoap.php');
require_once('common.php');

$server = 'https://www.google.com';
$namespace = 'http://www.google.com/api/adsense/v2';

// Set up the authentication headers
$email = '<impl:developer_email>REPLACE WITH DEVELOPER EMAIL</impl:developer_email>';
$password = '<impl:developer_password>REPLACE WITH DEVELOPER PASSWORD</impl:developer_password>';
$client_id = '<impl:client_id>NOT RELEVANT</impl:client_id>';

$header = $email . $password . $client_id;

// creating soap client
$wsdl = $server . '/api/adsense/v2/AccountService?wsdl';
$client = new soapclient($wsdl, true);
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}
$client->soap_defencoding = 'UTF-8';

// Set the headers; they are needed for authentication
$client->setHeaders($header);
if ($client->fault) {
  showMyErrors($client);
  return;
}
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}

// setting the parameter
$param = '<loginEmail>users_address_here@example.com</loginEmail>';
$param .= '<entityType>Individual</entityType>';
$param .= '<websiteUrl>http://test.aaa.com</websiteUrl>';
$param .= '<websiteLocale>en</websiteLocale>';
$param .= '<usersPreferredLocale>en_US</usersPreferredLocale>';
$param .= '<emailPromotionPreferences>true</emailPromotionPreferences>';
$param .= '<synServiceTypes>ContentAds</synServiceTypes>';
$param .= '<hasAcceptedTCs>true</hasAcceptedTCs>';
$param = '<createAdSenseAccount>' . $param . '</createAdSenseAccount>';

// invoke web service
showCall('createAdSenseAccount', $param);
$response = $client->call('createAdSenseAccount', $param, $namespace);
if ($client->fault) {
  showMyErrors($client);
  return;
}
$err = $client->getError();
if ($err) {
  showSoapClientError($err);
  return;
}

// get back the response
$response = $response['createAdSenseAccountReturn'];
showCreatedPublisher($response);

// showing the soap
showRequestResponse($client);

?>

Comments

Blogger API Updated

I've just read about Blogger API updates you might be interested. Seems that they updated documentation and code samples, but also integration with other Google products, especially Calendar. Good to know.

Comments

Google Checkout API

As you already read in the news Google launched a few days ago Google Checkout, its online payment service. Dubbed 'GBuy' may be an alternative to EBay's PayPal (already eBay restricted Google Checkout as payment method on its auction site). One big minus is that is available only for US (for the moment, I hope).

I won't comment here too much about this new service, features or fees but mention new API. New Google Checkout API allows you a deep and secure integration with web/desktop apps. They offer samples for ASP (IIS), PHP & Java, with full documentation for each of these languages. Here's a link to PHP sample.

Files with their functions available for integration into your custom application are not trivial, but easy to understand. Give it a try and see if they are ok for your needs.

Comments

Google Calendar API

Google has just released their web API for calendar (on Apr 20):

Google Calendar allows client applications to view and update calendar events in the form of Google data API ("GData") feeds. Your client application can use the Google Calendar data API to create new events, edit or delete existing events, and query for events that match particular criteria. 

There are a few steps to use Google Calendar, all described in their help files:

  • Create an account
  • Get magic cookie for your calendar, or feed URL (something like http://www.google.com/calendar/feeds/userID/private-magicCookie/full)
  • Request the feed, which returns a XML file;
  • Post back an event or comments with a XML file.

We'll be back shortly with a full featured example using PHP.

Comments

Google Maps API

A well-know service, used by many people is Google Maps API. See them at work at maps.google.com.

You can integrate all these maps directly in your websites or applications. Google Maps API is based on JavaScript. You also need to register your ID key used in all map web pages. First thing, is to include Google JavaScript page from http://maps.google.com/maps :

<script src="http://maps.google.com/maps?file=api&v=1&key= … key ID …" type="text/javascript">
 </script>

Then you can use all JavaScript objects and functions provided by this API, documented here. Here's a small example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="content-type" content="text/html; charset=UTF-8″/>

 <title>Google Maps JavaScript API Example: simple</title>
 <script src="http://maps.google.com/maps?file=api&v=1&key=ABQIAAAAEfCuQGsNiSWxRgf_vfNWaRQjskl1-YgiA_BGX2yRrf7htVrbmBTEB0IH-F489GrwP8-dHLib7cKKIQ" mce_src="http://maps.google.com/maps?file=api&v=1&key=ABQIAAAAEfCuQGsNiSWxRgf_vfNWaRQjskl1-YgiA_BGX2yRrf7htVrbmBTEB0IH-F489GrwP8-dHLib7cKKIQ"
   type="text/javascript">
 </script>

 <script type="text/javascript">
 function onLoad() {
   if (GBrowserIsCompatible()) {
   var map = new GMap(document.getElementById("map"));
   map.centerAndZoom(new GPoint(-122.1419, 37.4419), 4);
   }
 }
</script>
</head>
<body onload="onLoad()">
<div id="map" style="width: 500px; height: 300px"></div>
</body>
</html> 

Idea is simple: create an GMap object based on a GPoint object created from longitude and latitude. Then you can add more objects as layers (GMarker) with their own events (GEvent).

Let's see a little bit more complicated demo with more functionality (popup window). This sample will work only from our website because of the ID key (unless you change it). As you can see, after loading, the maps are independent from your webpage, with their own navigation. All points must be specified by their longitude and latitude, you can't use full addresses. To get this geocodes you can use other websites like www.infosports.com/m/map.htm or www.geocoder.us.

Google API offers much more functionality. You can add markers to the map, with their own events and HTML content. Let's see such an example, with a marker and custom text displayed when click it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <script src="http://maps.google.com/maps?file=api&v=1&key=ABQIAAAAMWlN5S_pnUghkzaCL6OMMhRPit09n3FXh9HBeHPLQkDYD-WLrxTsecUOe9Urm4gtthzKpsI0hcmToA" mce_src="http://maps.google.com/maps?file=api&v=1&key=ABQIAAAAMWlN5S_pnUghkzaCL6OMMhRPit09n3FXh9HBeHPLQkDYD-WLrxTsecUOe9Urm4gtthzKpsI0hcmToA" type="text/javascript"></script>
  </head>
  <body>
    <div id="map" style="width: 400px; height: 300px"></div>
    <script type="text/javascript">
   
    var map = new GMap(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    var point = new GPoint(-79.3763, 43.6458);
    map.centerAndZoom(point, 8 );
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml("Hi there!");
    });
    map.addOverlay(marker);
    </script>
  </body>
</html> 

For a full description of the objects, their methods, events and properties see their documentation (including more complex samples).

Comments

Google Search and Spell Checker

Let's start our web API discussion with a first example: Google Search and Spell Checker.

First step is to register a free account. You'll get a key required when using Google functions.

Here's an example how to use it with NuSOAP and PHP:

# Use the NuSOAP php library (provide relative path to include file)
require_once('nusoap.php');

# Set parameters
$parameters = array(
    'key'=>'… your key from Google …',
    'q' => 'keywords for search',
    'start' => 0,
    'maxResults' => '10′,
    'filter' => 'false',
    'restrict' => '',
    'safeSearch' => 'false',
    'lr' => '',
    'ie' => 'latin',
    'oe' => 'latin'
  );

$soapclient =
  new soapclient('http://api.google.com/GoogleSearch.wsdl', 'wsdl');

$results = $soapclient->call('doGoogleSearch',$parameters);

if ( is_array($results['resultElements']) ) {
   echo "<p>Your Google query found "
  . $results['estimatedTotalResultsCount'] . ":</p>";

  foreach ( $results['resultElements'] as $result ) {
    echo "<p><a href='" . $result['URL'] .
      "' target='_blank'>" .
     ( $result['title'] ? $result['title'] : 'no title' ) .
     "</a><br>" . $result['URL'] . "<br>" .
     ( $result['snippet'] ? $result['snippet'] : 'no snippet' ) .
     "<br>".$result['URL']."</p>";
     }
}

This code creates a SOAP client with entry point http://api.google.com/GoogleSearch.wsdl, then call doGoogleSearch() function with imitialized parameters ($parameters). These parameters are straightforward, with the key provided after Google account registration, your keyword keys, start page and the number of results, as well as other parameters.

At the end, you can display results from $results array in any form you want, maybe integrated with other content.

Another useful Google service is online spell checking, available in a similar manner:

# Use the NuSOAP php library
require_once('nusoap.php');

# Set parameters
$parameters = array(
    'key'=>' … your key from Google …',
    'phrase' => 'someting wroong',
    'start' => 0,
    'maxResults' => '10′,
    'filter' => 'false',
    'restrict' => '',
    'safeSearch' => 'false',
    'lr' => '',
    'ie' => 'latin',
    'oe' => 'latin'
  );

$soapclient =
   new soapclient('http://api.google.com/GoogleSearch.wsdl', 'wsdl');

$results = $soapclient->call('doSpellingSuggestion',$parameters);
echo($results);

Notice parameter difference for each service: for searching is used 'q' parameter and for spell checker 'phrase'. Spell-checker phrase is limited to 1024 characters and 10 words.

Google search and spell-checker is limited to 1,000 searches per day. There are no commercial accounts at this time, for more searches. For other details see Google FAQ or API Reference (with parameters description). Also, you can download from Google examples for Java and .NET.

Or see this sample at work (in a popup window).

Other links:

Comments