\n"; $r .= " \n"; $r .= " \n"; $r .= " \n"; $r .= "
\n"; $r .= "\n"; return $r; } class MSNSearch { var $appID; var $errorMessage; var $page; var $query; var $recordsPerPage; var $results; var $safeSearch; var $totalPages; var $totalRecords; function MSNSearch($appID) { $this->appID = $appID; $this->errorMessage = ""; $this->page = 1; $this->query = ""; $this->recordsPerPage = 10; $this->safeSearch = true; $this->totalPages = 0; $this->totalRecords = 0; } function setPage($page) { $this->page = ($page < 1) ? 1 : $page; } function setQuery($query) { $this->query = $query; } function search_header($query) { $r = "

Results Page " . $this->page . ""; $r .= " of " . $this->totalPages . "" . ($this->totalPages == 1 ? " Page " : " Pages "); $r .= " for " . $query . "

\n"; return $r; } function search_navagation($q) { if ($this->totalRecords <= $this->recordsPerPage) return ""; $query = urlencode($q); $query = htmlspecialchars($query,ENT_NOQUOTES,"UTF-8"); $r = "

"; if ($this->page > 1) $r .= " previous page | "; else $r .= " previous page | "; if ($this->totalRecords > ($this->page * $this->recordsPerPage)) $r .= " next page "; else $r .= " next page "; $r .= "

\n"; return $r; } function search_results() { $r = "
\n"; foreach ($this->results as $item) { $r .= "
" . $item['title'] . "
\n"; $r .= "
" . htmlspecialchars($item['snippet'],ENT_NOQUOTES,"UTF-8"); $r .= (strlen($item['snippet']) > 5 ? "
\n" : ""); $r .= "" . htmlspecialchars(str_replace("http://","",$item['url']),ENT_NOQUOTES,"UTF-8") . "\n"; $r .= "
\n"; } $r .= "
\n"; return $r; } function search() { $this->errorMessage = ""; $this->totalPages = 0; $this->totalRecords = 0; $this->results = array(); $retry_count = 0; $xml = false; while( !$xml && $retry_count < 3) { # Try request 3 times before failing $xml = simplexml_load_file($this->getMSNURL()); $retry_count++; } if (!$xml) { $this->errorMessage = "MSN Search failed"; $msg = "Search error:\n" . $this->errorMessage . "\n"; $msg .= "Query: " . $this->query . "\n"; $msg .= "Date: " . date('D M d Y h:i:s'); if (defined("MAILTO")) @mail(MAILTO,"** Search ERROR ***", $msg); return false; } # Deal with namespaces $xml_results = $xml->children('http://schemas.microsoft.com/LiveSearch/2008/04/XML/web'); $this->totalRecords = $xml_results->Web->Total; $this->totalPages = ceil($this->totalRecords / $this->recordsPerPage); if ($this->totalRecords > 0) { foreach($xml_results->Web->Results->WebResult as $item) { $this->results[] = array ( 'url' => $item->Url, 'urlDisplay' => $item->DisplayUrl, 'urlCache' => "", 'title' => isset($item->Title) && trim($item->Title) != '' ? $item->Title : $item->DisplayUrl, 'snippet' => isset($item->Description) ? $item->Description : "" ); } } return true; } function getMSNURL() { $result = "http://api.search.live.net/xml.aspx?"; $result .= "AppId=" . $this->appID; $result .= "&Query=" . urlencode($this->query); $result .= "&Sources=Web"; $result .= "&Version=2.0"; #result .= "&Market=en-us"; if ($this->safeSearch) $result .= "&Adult=Moderate"; else $result .= "&Adult=Off"; #$result .= "&Options=EnableHighlighting"; $result .= "&Web.Count=" . $this->recordsPerPage; $result .= "&Web.Offset=" . ($this->page - 1) * $this->recordsPerPage; #$result .= "&Web.FileType=DOC"; $result .= "&Web.Options=DisableHostCollapsing+DisableQueryAlterations"; return $result; } function spell($query) { $url = "http://api.search.live.net/xml.aspx?AppId=" . $this->appID; $url .= "&Query=" . urlencode($query); $url .= "&Sources=Spell&Version=2.0"; $results = array(); $retry_count = 0; $xml = false; while( !$xml && $retry_count < 3) { # Try request 3 times before failing $xml = simplexml_load_file($url); $retry_count++; } if (!$xml) { $this->errorMessage = "MSN Spell failed"; $msg = "Spell error:\n" . $this->errorMessage . "\n"; $msg .= "Query: " . $this->query . "\n"; $msg .= "Date: " . date('D M d Y h:i:s'); if (defined("MAILTO")) @mail(MAILTO,"** Search ERROR ***", $msg); return $results; } $xml_results = $xml->children('http://schemas.microsoft.com/LiveSearch/2008/04/XML/spell'); if ($xml_results->Spell->Total > 0) { foreach($xml_results->Spell->Results->SpellResult as $item) { $results[] = $item->Value; } } return $results; } } ?>