how to get bing api search results in MsOffice VBA

For the loyal VBA fans a quick snippet to grab search results from Bing in Access or Excel. There are few articles on the web about that, and that is a shame. So lets add some content on it to the web : first in MsAccess VBA and then in Excel, with a sample workbook added.

Working with the BING API requires an Application Id you can get at the Bing website.

Accessing the Bing Api in MsAccess VBA

In the the VBA Ide, through the menu Tools References I add a reference to the XML object library that contains the XMLHTTPRequest object, enabling me to make HTTP requests from VBA in the MsAccess database.

The simple part is the http request string :

  1.  
  2.         Dim MyKeyword As String
  3.         MyKeyword = "seo"
  4.        
  5.         Dim requestString As String
  6.         requestString = "http://api.bing.net/xml.aspx?"
  7.  
  8.         Dim AppId As String
  9.         AppId = "(get one at the bing website)"
  10.  
  11.        ' Common request fields (required)
  12.         requestString = requestString & _
  13.         "AppId=" & AppId & "&Query=" & MyKeyword & "&Sources=Web"
  14.  
  15.         ' Common request fields (optional)
  16.         requestString = requestString & _
  17.               "&Version=2.0" _
  18.             & "&Market=en-us" _
  19.             & "&Adult=Moderate" _
  20.             & "&Options="
  21.  
  22.         ' Web-specific request fields (optional)
  23.         requestString = requestString & _
  24.               "&Web.Count=10" _
  25.             & "&Web.Offset=0" _
  26.             & "&Web.Options=DisableHostCollapsing+DisableQueryAlterations"

Then comes the working part, sending the actual request and receiving the XML response (objSvrHTTP.responseText) containing the search data. I use a DomDocument object and XPath to get at the XML data.

First sending the request

  1.     objSvrHTTP.Open "GET", requestString, False
  2.     objSvrHTTP.send requestString

then creating a domdocument and receiving the response in it

  1.     'Create the DomDocument Object
  2.     Dim oDoc As MSXML2.DOMDocument
  3.     Set oDoc = CreateObject("MSXML2.DOMDocument")
  4.     oDoc.async = False
  5.     oDoc.validateOnParse = False
  6.      
  7.     'Load the response in the DomDocument Object
  8.     Dim fSuccess As Boolean
  9.     fSuccess = oDoc.loadXML(objSvrHTTP.responseText)
  10.     If Not fSuccess Then
  11.         MsgBox "failed"
  12.         Exit Sub
  13.     End If

The response uses two namespaces and the data we actually want is in the “web” namespace, so before we start selecting nodes in the xml-tree we first indicate what namespace we want to access :

  1.     oDoc.SetProperty "SelectionNamespaces", "xmlns:web='http://schemas.microsoft.com/LiveSearch/2008/04/XML/web'"

The total search result pages is a top level node in the namespace :

  1.     Set xmlnode = oDoc.selectSingleNode("//web:Total")
  2.     Debug.Print xmlnode.Text

…but the good stuff is in the “WebResult” nodes in the xml tree, I can use an XPath reference //web:Webresult to access that node-collection and with a simple for-next iterate through the collection, selecting the single nodes containing the actual data.

  1.     Set oChildren = oDoc.selectNodes("//web:WebResult")
  2.     For Each oResult In oChildren
  3.         Debug.Print oResult.selectSingleNode("./web:Title").Text
  4.         Debug.Print oResult.selectSingleNode("./web:Description").Text
  5.         Debug.Print oResult.selectSingleNode("./web:Url").Text
  6.         Debug.Print oResult.selectSingleNode("./web:DisplayUrl").Text
  7.         Debug.Print vbCrLf
  8.     Next

I output to the Immediate window aka Debug (under View, Immediate Window or Ctrl+G)

What can we do with it ? Let’s make a table for date, keyword, url, title, description, position

and add some code to store the results we retrieve

  1.     Dim table1 As DAO.Recordset
  2.     Set table1 = CurrentDb.OpenRecordset("Table1", dbOpenTable)
  3.     'counter for position    
  4.     i = 0
  5.    
  6.     Set oChildren = oDoc.selectNodes("//web:WebResult")
  7.     For Each oStruct In oChildren
  8.  
  9.        i = i + 1
  10.        With table1
  11.             .AddNew
  12.             'data from bing
  13.             !TITLE = oStruct.selectSingleNode("./web:Title").Text
  14.             !Description = oStruct.selectSingleNode("./web:Description").Text
  15.             !url = oStruct.selectSingleNode("./web:Url").Text
  16.             'and some additional data
  17.             !MYDATE = Now()
  18.             !POSITION = i
  19.             !KEYWORD = MyKeyword
  20.             'store the record
  21.             .Update
  22.         End With
  23.  
  24.     Next
  25.  
  26.     table1.Close

There now that scratches the seo itch :

MsAccess stuff is a bit hard to put on the web as sample, so I’ll make a quick one in Excel and put it up for download.

Accessing the Bing Api in Excel VBA

there you go : on sheet 1 you can fill in your AppID and keyword, click the butt(on…

…and on sheet 2 you get the top 10 search results

It uses the cells B1 and B2 as named ranges for MyKeyword and AppID, and has the code in the enclosed module. It also contains one of my AppId’s so no querying nasty shit, please.

ga api sample : get pageviews

I was going to put that online : how to get the pageviews out of the google analytics api, using simplexml and php. Google use three namespaces in the output file which make it less easy accessible, so here’s a quick sample of how to get your sites pageviews out of it :

  1. //ids           = site identifier (from the site data feed)
  2. //metrics     = what i want to see
  3. //start-date
  4. //end-date
  5.  
  6. $feedUri = "https://www.google.com/analytics/feeds/data?ids=ga:10516419&metrics=ga:pageviews&start-date=2009-04-01&end-date=2009-05-01";    
  7.  
  8.  $curl = curl_init();
  9.  curl_setopt($curl, CURLOPT_URL, $feedUri);
  10.  curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
  11.  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  12.  
  13.        $headers[] = "Authorization: GoogleLogin auth=".$Authtoken;
  14.  
  15. //for authtoken : see previous post
  16.  curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  17.  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  18.  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  19.  curl_setopt($curl, CURLOPT_VERBOSE, 1);
  20.  
  21. //get the string containing the xml file
  22.  $gA = curl_exec($curl);

the feed has three namespaces (atom, opensearch and dxp/analytics), a simple way is accessing the ENTRY tags (from the Atom namespace), in that tag is one DXP: line and that has the answer to the question.

<dxp:metric confidenceInterval=’0.0′ name=’ga:pageviews’ type=’integer’ value=’755’/>

  1. //load the string into a simple xml object
  2.  $feed = simplexml_load_string($gA);
  3.  
  4. //take the atom namespace
  5.  $children =  $feed->children('http://www.w3.org/2005/Atom');
  6.  
  7. //take the entry tags
  8.  $parts = $children->entry;
  9.  foreach ($parts as $entry) {
  10.  
  11.         //from the entry tag,
  12.         //access the dxp namespace
  13.   $dxp = (object) $entry->children('http://schemas.google.com/analytics/2009');
  14.  
  15.         //METRIC contains the answer to the question
  16.         //grab from the tag METRIC the attribute VALUE
  17.                 echo   (string) $dxp->metric->attributes()->value;
  18.  
  19.         }

Important is using the (string) typecast, normally simplexml returns a simplexml object, when you force a string type, it gives the actual metric ga:pageview value attribute as number.

google analytics have an api !

[note: over at ioncannon Carson McDonald made a cool google analytics plugin for wordpress, i use it on this blog, works fine].

An actual google analytics api, and I missed out on it. This api is already a month old and i havent read anything on the blogs about it.

I found it half an hour ago, I havent checked it completely but it looks promising. Here is the first bit, basic authentication with php and curl.

  1. $USER_EMAIL=""; // #Insert your Google Account email here
  2. $USER_PASS=""; //#Insert your password here
  3.  
  4. //array with some general data
  5. $data = array(
  6.   "Email" => $USER_EMAIL,
  7.   "Passwd" => $USER_PASS,
  8.   "accountType" => "GOOGLE",
  9.   "source" => "curl-accountFeed-v1",
  10.   "service" => "analytics"
  11. );
  12.  
  13. $friends_url = 'https://www.google.com/accounts/ClientLogin';
  14. $curl = curl_init();
  15. curl_setopt($curl, CURLOPT_URL, $friends_url);
  16. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
  17. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  18.  
  19. //http-post that contains the array as data
  20. curl_setopt($curl, CURLOPT_POST, true);
  21. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  22.  
  23. //go shove the https secure connection verification
  24. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  25. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  26.  
  27. curl_setopt($curl, CURLOPT_VERBOSE, 1);
  28.    
  29.  
  30. $googleAuth = curl_exec($curl);
  31.  
  32. //optional : some feedback
  33.  
  34. //check if we get an error code from cUrl
  35. //    echo curl_errno($curl)."<br />";
  36. //    echo  curl_error($curl)."<br />" ;
  37.  
  38.  
  39. //print the body of the returned data
  40. //    print_r($googleAuth);
  41.  
  42. //print all the headers
  43. //    $info = curl_getinfo($curl);
  44. //    print_r($info);

somewhere in the garbled mess that curl returns is the Authorization token, starts with auth=.

  1. $start = strpos($googleAuth, "Auth=") + 5;
  2. $Authtoken = substr($googleAuth, $start);
  3.  
  4. //echo $Authtoken;

I put that token in the header of the next calls and google assumes I am kosher : time to get the accounts feed :

  1. //add the authoritzation token as extra header
  2. $headers[] = "Authorization: GoogleLogin auth=".$Authtoken;
  3.  
  4.  
  5. $friends_url = 'https://www.google.com/analytics/feeds/accounts/default';
  6.  
  7.  $curl = curl_init();
  8.  curl_setopt($curl, CURLOPT_URL, $friends_url);
  9.  curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);
  10.  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  11.  curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  12.  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  13.  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  14.  curl_setopt($curl, CURLOPT_VERBOSE, 1);
  15.  $googleAccounts = curl_exec($curl);
  16.  
  17. //check errors
  18. echo curl_errno($curl);
  19. echo curl_error($curl) ;
  20. print_r($googleAccounts);

And there it is : a whole list with weird codes, my account list :) seems easier than the other gData api’s.

note : the google code curl example does not show the ” auth=” part of the token, they assume you use the entire line “auth=…” as token.

Once I have my spectacular visitor count in a sidebar widget I’ll blog another post on this one.