Archive forYahoo

Yahoo! Mail Web Service APIs

Yahoo! announced today availability for its Mail Web Service APIs:

With the Yahoo! Mail Web Service APIs, you can build applications to perform tasks such as listing messages, displaying folders, and composing and sending messages.

Here's an authentication example from their samples:

<?php
// test.php — Test Yahoo! Browser-Based Authentication
// A simple auth exmaple.
// Author: Jason Levitt
// Date: November 20th, 2006
// Version 1.0
//

// Edit these. Change the values to your Application ID and Secret
define("APPID", 'JzkILqzIkY1xxxxxxxxxIaGILaRWClvO');
define("SECRET", '10de7e35xxxxxxxxxe5749207495ed');

// Include the proper class file
$v = phpversion();
if ($v[0] == '4′) {
include("ybrowserauth.class.php4″);
} elseif ($v[0] == '5′) {
include("ybrowserauth.class.php5″);
} else {
die('Error: could not find the bbauth PHP class file.');
}

function CreateContent() {

$authObj = new YBrowserAuth(APPID, SECRET);

// If Yahoo! isn't sending the token, then we aren't coming back from an
// authentication attempt
if (empty($_GET["token"])) {
// You can send some data along with the authentication request
// In this case, the data is the string 'some_application_data'
echo 'You have not signed on using BBauth yet<br /><br />';
echo '<a href="'.$authObj->getAuthURL('some_application_data', true).'">Click here to authorize</a>';
return;
}

// Validate the sig
if ($authObj->validate_sig()) {
echo '<h2>BBauth authentication Successful</h2>';
echo '<h3>The user hash is: '.$authObj->userhash.'</h3>';
echo '<b>appdata value is:</b> '. $authObj->appdata . '<br />';
} else {
die('<h1>BBauth authentication Failed</h1> Possible error msg is in $sig_validation_error:<br />'. $authObj->sig_validation_error);
}

return;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8″ />
</head>
<body>
<div id="look">
<h2>Test BBauth </h2>
<div>
<?php CreateContent(); ?>
</div>
<div id="content">
</div>
</div>
</body>
</html>

They also offers $10 commission for developers which refer clients to premium Yahoo! Mail account.

Comments

del.icio.us API

You know what del.icio.us is: a social bookmark service, now bought by Yahoo. You can create a free account and add your website bookmarks, even share with others.

They also released some API, but in my opinion very limited. All you can do is to manage your own bookmarks (links) with their tags and bundles. You can't do searches on popular websites and tags, or last added bookmarks.

To use it from any application or website you need a free account. All API help files are available at del.icio.us/help/api. They also provide HTML, RSS, JSON & JavaScript access (see help page).

The most important function (page), I think, is posts/add, which add a new link to your online account. You can do this operation from your browser openning

http://username:password@del.icio.us//api/posts/add?url=…&description=…URL title…&extended=…URL description…&tags=… tags…

 

You can create such a link even from JavaScript and call this page. One simple solution from JavaScript is to use new Image(), even if the value returned is not an image:

var img=new Image();
img.src='http://..full URL as above …';

When everything was done right is returned

<result code="done" />

otherwise would be:

<result code="something went wrong" />

Here's some PHP code to add a new bookmark to your account, in 2 simple ways:

function deliciousAdd($user, $pass, $url, $description, $extended, $tags) {
  $r=file("http://{$user}:{$pass}@del.icio.us/api/posts/add?url=".urlencode($url).
     "&description=".urlencode($description)."&extended=".urlencode($extended).
     "&tags=".urlencode($tags));
  return ($r[1]=="<result code=\"done\" />");
}

function deliciousAdd2($user, $pass, $url, $description, $extended, $tags)
   {
   $domain="del.icio.us";
   $socketConnection = fsockopen($domain, 80, $errno, $errstr);

   if (!$socketConnection) {
 echo("Network error: $errstr ($errno)");
   } else {
       $tmp = '';
       fputs($socketConnection,
 "GET /api/posts/add?url=".urlencode($url).
 "&description=".urlencode($description)."&extended=".urlencode($extended).
 "&tags=".urlencode($tags).
 " HTTP/1.0\r\nAuthorization: Basic ".base64_encode($user.":".$pass)."\r\n".
 "Host: $domain\r\nConnection: close\r\n\r\n");

       while (!feof($socketConnection)) {
           $tmp .= fgets($socketConnection, 128);
       }

       fclose ($socketConnection);

   }

return(strpos($tmp,"<result code=\"done\" />"));
}

They return TRUE or FALSE, depends on the response and could be use like:

deliciousAdd($username,$password,
    "http://Yahoo.com","Yahoo!","Yahoo","yahoo");

deliciousAdd2($username,$password,
    "http://Yahoo.com","Yahoo!","Yahoo","yahoo"); 

Simple, isn't it?

Comments

Yahoo Geocoding API

There are some mapping APIs that accept only geocode coordinates (longitudes & latitudes). You need a solution to get them for all addresses you want. One of the best solution is to use Yahoo Geocoding API. You need to register first then you can request it with your custom parameters and get the results as XML, JSON or serialized PHP.

You can use the form below to get your values:

Street:
City:
State:
Zip:
or
Free Text:

- city, state
- city, state, zip
- zip
- street, city, state
- street, city, state, zip
- street, zip
Longitude:
Latitude:
 

This sample is using JSON and 2 extra files. One is a custom JSON library (json.js) for client side calls, plus a PHP file (load.php) which loads any URL on request based on fetchUrlWithoutHanging() from php.net/file (because XMLHTTP can't open pages from different server because of security restrictions).

The JavaScript function to interogate Yahoo service is here (Update: check new XML version below, JSON version is no more available from Yahoo):

function geocode() { 
var frm=document.forms['frm'];
if(frm.f.value!='')
  tmp="/load.php?url="+escape("api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&street=&city=&state=&location="+frm.f.value+"&output=json");
else
  tmp="/load.php?url="+escape("api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&street="+frm.s.value+"&city="+frm.c.value+"&state="+frm.p.value+"&output=json");
tmp2=json_load(tmp);
var obj=eval('('+tmp2+')');
if(obj) {
  frm.l1.value=obj.ResultSet.Result[0].Longitude;
  frm.l2.value=obj.ResultSet.Result[0].Latitude;
  }
else {
  frm.l1.value="";
  frm.l2.value="";
  }
obj=null;
}

Here's a simple request to Yahoo service: http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&street=701+First+Street&city=Sunnyvale&state=CA
This call return JavaScript code to build an object with all requested values as properties.

There may be some problems with this scripts, but generally it should work.

The Yahoo Geocoding service is limited to 50,000 queries per IP address per day. Combined with Yahoo Maps or Google Maps could be a very powerful online tool.

Update (March 22nd): JSON results are no more available from Yahoo Geocoding API, so, we've changed the scripts to use XML files, directly from your browser (IE or FF):

function geocode() { 
var frm=document.forms['frm'];

if(frm.f.value!='')
   tmp="/load.php?format=xml&url="+
       escape("api.local.yahoo.com/MapsService/V1/geocode?appid=cod_xx2006&street=&city=&state=&location="+frm.f.value+"&output=xml");
else
   tmp="/load.php?format=xml&url="+
       escape("api.local.yahoo.com/MapsService/V1/geocode?appid=cod_xx2006&street="+frm.s.value+"&city="+frm.c.value+"&state="+
frm.p.value+"&output=xml");

var xmlDocument=null;

if(window.XMLHttpRequest) {
     // Firefox, Mozilla
     xmlDocument = document.implementation.createDocument("","",null);
     xmlDocument.async = false; 
     xmlDocument.load(tmp);
     lat=xmlDocument.getElementsByTagName("Latitude").item(0).firstChild.nodeValue;
     long=xmlDocument.getElementsByTagName("Longitude").item(0).firstChild.nodeValue;
     err=xmlDocument.getElementsByTagName("Result").item(0).attributes.getNamedItem("warning").firstChild.nodeValue;
} else if(window.ActiveXObject) {
     // IE/Windows ActiveX version
     xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
     xmlDocument.async = false;
     xmlDocument.load(tmp);
     xmlDocument.setProperty("SelectionLanguage", "XPath");
     xmlDocument.setProperty("SelectionNamespaces", "xmlns:my='urn:yahoo:maps'");
     var objNode=xmlDocument.selectSingleNode("//my:Result/@warning");
     err=objNode.text;
     var objNode=xmlDocument.selectSingleNode("//my:Result/my:Latitude");
     lat=objNode.text;
     var objNode=xmlDocument.selectSingleNode("//my:Result/my:Longitude");
     long=objNode.text;
}

if(xmlDocument) {
  frm.l1.value=lat;
  frm.l2.value=long;
  if(err!="") alert('Err: '+err);
  }

You can download YahooGeocode.js file with all client side process (the code above). It uses XML DOM to load Yahoo page using a local proxy (load.php) and then uses different methods to get data: getElementsByTagName() for Firefox & XPath for Internet Explorer.

Comments (1)

Yahoo Spell Checker API

Another web service available from Yahoo is online spell checker. Thsi service is similar with Yahoo web search: you provide all parameters with REST and get results as XML, JSON or serialized PHP. URL used for this service is http://api.search.yahoo.com/WebSearchService/V1/spellingSuggestion and its description with all parameters is at http://developer.yahoo.net/search/web/V1/spellingSuggestion.html.

Here's an example from JavaScript using JSON:

<script language=JavaScript>
function ws_results(obj) {
alert("Correct spelling of 'someting' is '"+obj.ResultSet.Result+"'");
}
</script>
<script type="text/javascript"
src="http://api.search.yahoo.com/WebSearchService/V1/spellingSuggestion?appid=YahooDemo&query=someting&output=json&callback=ws_results">
</script>

And something similar with serialized PHP:

$request =  'http://api.search.yahoo.com/WebSearchService/V1/spellingSuggestion?appid=YahooDemo&query=someting&output=php';

$response = implode('',file($request));

if ($response === false) {
 die('Request failed');
}

$phpobj = unserialize($response);

echo "Correct spelling of 'someting' is '{$phpobj['ResultSet']['Result']}'"; 

Simply, isn't it? There is the same 5000 queries limit per day per IP for this web service.

Comments

Yahoo Search API

Yahoo searches are a little bit more complex (or simplier?) because there are more options for getting the results and where searches are performed. For this reason we separated them from spell checker, also available from Yahoo.

To perform Yahoo searches you'd need to register an application ID and then pass proper parameters thru REST to search pages located at http://api.search.yahoo.com/WebSearchService/V1/webSearch.

You can found all input & output parameters at http://developer.yahoo.net/search/web/V1/webSearch.html . Basicaly, Yahoo accepts parameters in a single way (REST) and return results in 3 possible ways, at your choice: XML (default), JSON & serialized PHP (based on serialize / unserialize functions from PHP, available since PHP 3.05). Depending on your needs, one or another solution to get results may be more convenient.

You can download from Yahoo website examples for Macromedia Flash, Java, JavaScript, PHP & Python. Server-side, especially in PHP the simplest method is using serialized results (available not only from PHP but other languages too, see this page, like C#, Python, Ruby and more). Here's an example with PHP:

$request =  'http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=something&results=10&output=php';

$response = implode('',file($request));

if ($response === false) {
 die('Request failed');
}

$phpobj = unserialize($response);

foreach($phpobj['ResultSet']['Result'] as $arr) {
  echo "<p><a href='{$arr['Url']}' target='_blank'>{$arr['Title']}</a>\n
           <br>{$arr['Summary']}</p>\n";

Don't forget to replace your application ID and your custom parameters.

JSON version is useful, especially from JavaScript, client-side:

<script language=JavaScript>
function ws_results(obj) {
  var res=obj.ResultSet.Result;
  for (var i = 0;i     document.write("<p><a href='"+res[i].Url+"' target='_blank'>"+
    res[i].Title+"</a>\n<br>"+
    res[i].Summary+"</a></p>\n");
}
</script>

<script type="text/javascript"
src="http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=something&results=10&output=json&callback=ws_results" mce_src="http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=something&results=10&output=json&callback=ws_results">
</script>

It's very important to declare ws_results() callback function before calling Yahoo service. It automatically create an object parameter (obj) based on raw result returned by Yahoo service.

XML examples are provided by Yahoo, so I don't insist on them now.

Yahoo services have a limit of 5000 queries per day per IP. When you hit this limit an error message is returned. You have a log of these searches at http://api.search.yahoo.com/webservices/usage_data

As you can see, there is not very hard to integrare Yahoo search with your websites or applications. For other Yahoo services check our website.

Comments