A Useful REST Request


The preceding request works, but doesn't actually deal with any of the data returned or allow users to perform searches on their own. This example will. First, because auctions are very time sensitive, you will need a function to turn seconds into a nice, human-readable format:

 function prettyTimeRemaining($timestamp) {   $timeRemaining = "";   $weeks = floor($timestamp / 604800);   $timestamp = $timestamp -  ($weeks * 604800);   if ($weeks > 0)   {     $timeRemaining .= "$weeks Week(s) ";   }   $days = floor($timestamp / 86400);   $timestamp = $timestamp - ($days * 86400);   if ($days > 0)   {     $timeRemaining .= "$days Day(s) ";   }   $hours = floor($timestamp / 3600);   $timestamp = $timestamp - ($hours * 3600);   if ($hours > 0)   {     $timeRemaining .= "$hours Hour(s) ";   }   $minutes = floor($timestamp / 60);   $timestamp = $timestamp - ($minutes * 60);   if ($minutes > 0)   {     $timeRemaining .= "$minutes Minute(s) ";   }   $seconds = $timestamp;   $teimRemaining .= "$seconds Second(s)";   return $timeRemaining; } 

The function is pretty basic — it will turn a value like 297143 into 3 Day(s) 10 Hour(s) 32 Minute(s). The mechanics are pretty simple; the values used for division are merely the number of seconds in that time period. Floor rounds any number down to the nearest whole number (1.8 becomes 1).

With that function out of the way, here is quickSearch.php:

 <html> <head> <title>eBay Quick Search REST API</title> </head> <body> <form method="get">   Search Terms: <input type="text" name="search"><br>   <input type="submit"> </form> <pre> 

To allow users to enter whatever query they would like, a form will be needed; returning that HTML code before starting the script saves escaping.

 <?php $endPoint = "http://rest.api.sandbox.ebay.com/restapi"; $requestToken   = "oVAr7OhSbdw%3D**%2Bs1d4ta8quAac9G3rvTuhs8IPvg%3D"; $requestUserId  = "wroxuser"; $resultsPerPage = 10; 

A few basic variables are set, the basic endpoint is set, as well as the number of results shown per page.

 if (isset($_GET['search'])) {    if (isset($_GET['page']) && ctype_digit($_GET['page']))    {       $page = $_GET['page'];    }else    {       $page = 0;    } 

If the script has not received a value for the search parameter, this is likely the first run and there's no point in accessing the API. A page parameter is also allowed; if it is present and is only digits (you don't want a nefarious user sneaking something else in there), it is used, otherwise it is set to zero.

    $skip = $page * $resultsPerPage;    $searchTerms = urlencode($_GET['search']);    $fullEndPoint = $endPoint . "?RequestToken={$requestToken}&RequestUserId=      {$requestUserId}&Query={$searchTerms}&CallName=GetSearchResults&MaxResults=      $resultsPerPage&Skip={$skip}";    $results = file_get_contents($fullEndPoint);    $xml = simplexml_load_string($results);    echo "Your search for <b>$searchTerms</b> yielded a total of <b>     {$xml->Search->GrandTotal}</b> results<br>";    echo "These are results <b>" . ($page * $resultsPerPage + 1) . "</b> to <b>" .     ((1 + $page) * $resultsPerPage) . "</b><br><br>"; 

The API allows you to specify how many results you would like to be skipped. That value is calculated using the number of results displayed per page as well as the page value calculated earlier. The file_get_contents function is used to make the request and obtain results, which are then dropped into a SimpleXML object. Using that SimpleXML object, some basic information regarding the search is presented.

    foreach($xml->Search->Items->Item AS $item)    {     $link = $item->Link;     $link = str_replace("http://cgi.ebay.com/", "http://cgi.sandbox.ebay.com/",       $link);     echo "<a href=\"$link\">{$item->Title}</a><br>";     echo "Current Price: <b>{$item->LocalizedCurrentPrice}</b> \t Bids: <b>       {$item->BidCount}</b><br>"; 

Each of the items returned by the search is iterated through to print the basic item information. For reasons I don't quite understand, the sandbox returns a link pointing at the live site, where the items returned no longer exist. This link is replaced with a working one pointing at the sandbox. The LocalizedCurrentPrice is used to ensure the user gets a clear idea of the price of the item.

     if ($item->ItemProperties->BuyItNow == 1)     {       echo "Buy it Now Price: <b>{$item->BINPrice}</b><br>";     } 

If the item has a BuyItNow price, it is displayed to the user.

     echo "Auction Start: <b>{$item->StartTime}</b> \t Auction End: <b>       {$item->EndTime}</b><br>";     $endTime = strtotime($item->EndTime);     $timeRemaining = $endTime - time();     echo "Time Remaining: <b>" . prettyTimeRemaining($timeRemaining) ."</b><br>";     echo "<br><br>";    } 

The timing information is displayed, along with a clear representation of the amount of time remaining before the auction ends.

    if (trim($xml->Search->HasMoreItems) == 1)    {      echo "<a href=\"?search={$searchTerms}&page=" . ($page + 1) . "\">         Next Page</a><br>";    } } ?> </pre> </body> </html> 

Finally, if there are more search results available, they are displayed, and the HTML code is nicely concluded. Figure 9-1 shows the resultant page output quickSearch.php output.

image from book
Figure 9-1




Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
ISBN: 764589547
EAN: N/A
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net