Get more writing less

Truthfully, there is a 99.9% that SimpleXML will be used to be the main support of the Bing API PHP. Of course, if you have seen some of the first examples to get a result you must enter various nodes and that’s quite troublesome. So my current goal is to reduce the code and get what you want writing less.

How! I’m introducing a new class called ResultSet_Manager which will take the decisions on which class to use (pretty much like factory pattern but not quite since it doesn’t follow some of the standards). The Manager can be turned on optionally to maintain an independance and if it’s on it will call the classes starting with ResultSet_SourceType_(SourceType). Let’s take a look at how it would work:

    // // Notes: Spell query must be urlencode, REQUIRED
    $search->query('which+is+better+tweetdeck+or+seesmic')
           ->setSources('Web')
           ->setFormat('xml')
           ->setResultManager(TRUE); # Required to be boolean

    // Contains the search
    $results = $search->getResults();
   echo $results->Total;
   echo $results->Get->WebResult[1]->Title;
   echo $results->QueriedTerm;

As you see, it is slightly shorter than the previous:

$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;

Now you notice quite a difference, although it’s not the same query we originally ran you should see that the code has been reduced.

While the goal to make everything more simple remains a priority. The ResultSet Manager and the SourceType classes (e.g ResultSet_SourceType_Web) are independant of Bing API PHP. Meaning that you can use it without calling Bing API PHP.

For example (example has not been tested in dev environment):

<?php
$XML = file_get_contents('http://api.search.live.net/xml.aspx?AppId=APPID&Query=Guitars&Sources=Image');
require_once 'SourcesManager/ResultSet_SourceType_Image';
$ImageSet = new ResultSet_SourceType_Image($XML);
$ImageSet->Get->ImageResult[1]->Title;
?>

All SourceTypes classes have and property called “Get” which holds the Result. In Multiple sources, while not yet developed. My other idea is to do something nearing to:

$MultipleSets->Get->ImageResult[1]->Title;
$MultipleSets->Get->WebResult[1]->Title;
$MultipleSets->Get->VideoResult[1]->Title;

Anyway! The next version will provide a experimental support of SOAP. That’s about it for today, see you next time.

2 people like this post.
This entry was posted in PHP, Programming, Projects. 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>