Bing API PHP + SimpleXML = Bingtastic!

So I did promise to “unveil” a few samples. It took me some efforts to build and well I did got a bit lost on thoughts when writing some explanations so if you notice anything weird, my bad!

See the bing application in action: Arch Linux Query / Wii Query Images / Microsoft News Query

Bing developers blog has an article about using Bing API along with DOMDocument but that reminds me of JavaScript and I’m not really a fan. So I took it to a new approach and that is using SimpleXML with Bing API using Bing API PHP. Easy and painless:

Now let’s take a look at this example:

// Contains the search
$results = $search->getResults();

// Start our SimpleXML class
$xml = new SimpleXMLElement($results);

// Every structure of bing.com is pratically similar, so its safe to say you can use this assign
$QueriedTerm = $xml->Query->SearchTerms;

/**
* Getting the sources from WEB
*/
$WebResultSet = $xml->children('http://schemas.microsoft.com/LiveSearch/2008/04/XML/web');
// Demonstration on how to count the results

How did I get http://schemas.microsoft.com/LiveSearch/2008/04/XML/web ? Quite simple, when you var_dump the xml you will notice this:

<?xml version="1.0" encoding="utf-8" ?><?pageview_candidate?><SearchResponse xmlns="http://schemas.microsoft.com/LiveSearch/2008/04/XML/element" Version="2.1">

That above is not really what we need, what we are looking for is this:

<Query><SearchTerms>Gimp</SearchTerms></Query><mms:Image xmlns:mms="http://schemas.microsoft.com/LiveSearch/2008/04/XML/multimedia">

What is the difference? Look at the url, we firstly had web schema URL. Now we have Image’s schema. Copy that url, crank up your SimpleXML, load your XML, and now call the children function.

So we end up with:

// Start our SimpleXML class
$xml = new SimpleXMLElement($results);

// Every structure of bing.com is pratically similar, so its safe to say you can use this assign
$QueriedTerm = $xml->SearchTerms;

/**
* Getting the sources from WEB
*/
$WebResultSet = $xml->children('http://schemas.microsoft.com/LiveSearch/2008/04/XML/multimedia');

Just for reference I’m using snips of the sample codes that I’m going to provide later on. Now let’s focus a bit more on accessing the children’s properties. From here on it’s practically easy, yet might need to practice to get the hang of it because sometimes; taking in example of Image SourceType, a property might contain another array so it’s good that you keep both XML and PHP files open. Like in this example:

$ThumbURL = $ImageResultSet->Image->Results->ImageResult[$i]->Thumbnail[0]->Url;
$ThumbWidth = $ImageResultSet->Image->Results->ImageResult[$i]->Thumbnail[0]->Width;
$ThumbHeight = $ImageResultSet->Image->Results->ImageResult[$i]->Thumbnail[0]->Height;
$ThumbSite = $ImageResultSet->Image->Results->ImageResult[$i]->Url;

Take a look at the value of $ThumbURL, do you see the property Thumbnail[0]? As you recognize it it is an array, which is why my previous remark that you should keep both your XML and PHP file opened, even if you memorize it, it never hurts refresh the memory.

Sadly, I wish I could explain all the sample codes. I created a small “search engine application” using bing.com. This small application includes most of the library functions except for setCurlArray() due to no current thoughts of using it. You may see the application for yourself and see how it works.

Ah, before I forget, let me show you how setHighlightFormat(); works. I think you will find it most satisfying.

$Description = $search->setHighlightFormat(
"<span class='hl'>",
"</div>",
$WebResultSet->Web->Results->WebResult[$i]->Description
);

The $search is an object that represents Bing API PHP library. You will notice that you can practically add anything to the start or end of the word that is being highlighted. Of course, I also included a reverse of the highlight which might “possibly” come in handy that is resetHighlight(). resetHighlight() will remove the invisible characters provided by bing api.

Conclusion, it’s been quite a week and I think it’s time for me to move on to a new stage meaning no more Bing posts for a while unless neccessary. It’s been quite nice to be able to produce some work with this and I’m really satisfied with myself with the outcome. I’d hope the sample codes will help you develop an even greater application.

Download Sample Codes | Project Moccha (bing application) | Bing API PHP | Project Home

phpDocumentation

7 people like this post.
This entry was posted in General. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>