Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
class Search {
function google($q) {
$request = 'http://ajax.googleapis.com/ajax/services/search/web?rsz=large&v=1.0&q='.urlencode($q);
$result = json_decode(Utils::request($request));
//print_r($result->responseData->results);
$results = Array();
foreach ($result->responseData->results as $result) {
$results[] = new SearchItem($result->titleNoFormatting, $result->url, $result->content, 'google');
}
return $results;
}
function yahoo($q) {
$request = 'http://boss.yahooapis.com/ysearch/web/v1/'.urlencode($q).'?appid=APIKEYGOESHERE&format=json&count=8';
$result = json_decode(Utils::request($request));
//print_r($result->ysearchresponse->resultset_web);
$results = Array();
foreach ($result->ysearchresponse->resultset_web as $result) {
$results[] = new SearchItem($result->title, $result->url, $result->abstract, 'yahoo');
}
return $results;
}
function wiki($q, $url) {
$request = 'http://'.$url.'/api.php?action=opensearch&format=xml&search='.urlencode($q).'&limit=8';
$result = simplexml_load_string(Utils::request($request));
//print_r($result);
$results = Array();
foreach ($result->Section->Item as $page) {
$results[] = new SearchItem($page->Text, $page->Url, $page->Description, 'wikipedia');
}
return $results;
}
function twitter($q) {
$request = 'http://search.twitter.com/search.json?q='.$q;
$result = json_decode(Utils::request($request));
$results = Array();
foreach($result->results as $tweet) {
$results[] = new SearchItem($tweet->text, null, $tweet->from_user, 'twitter');
}
return $results;
}
function bing($q) {
$request = 'http://api.search.live.net/json.aspx?Appid=APIKEYGOESHERE&sources=web&query='.
$q;//.'&Web.Count='.$count.'&Web.Offset='.$start;
$result = json_decode(Utils::request($request));
$results = Array();
foreach ($result->SearchResponse->Web->Results as $page) {
$results[] = new SearchItem($page->Title, $page->Url, $page->Description, 'wikipedia');
}
return $results;
}
function results($sia) {
if (count($sia) != 0) {
foreach ($sia as $si) {
echo '<div class="result">';
if ($si->engine != "twitter") {
echo '<a href="'.$si->url.'"><b>'.$si->title.'</b></a><br />';
echo $si->content;
} else {
echo $si->title.'<br />';
echo '<b><a href="http://twitter.com/'.$si->content.'">'.$si->content.'</a></b>';
}
echo '</div>';
echo '<div class="resultHR"></div>';
}
} else {
echo '<div class="notice">Nothing here!</div>';
}
}
}
class SearchItem {
public $title, $url, $content, $engine = null;
function __construct($title, $url, $content, $engine) {
$this->title = $title;
$this->url = $url;
$this->content = $content;
$this->engine = $engine;
}
}
<?php
class Utils {
function request($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'researchr <henri@henriwatson.com>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.