Yahoo API
Search Terms
example: site:yoursite.com
link:http://www.yourcompetition.com
Number of results per query
10
20
50
100
Limit of results
10
50
100
200
500
Do Backlink Check
';
/**
* A little 'controller' to make this sample page work:
*
*/
$timestart = microtime();
if(isset($_GET['p']) && $_GET['p'] != '') {
$numresults = $_GET['numresults'];
$limit = $_GET['limit'];
$backlink = $_GET['backlink'];
echo "Searching for ". $_GET['p'] . " ";
echo "Numresults ". $numresults . " ";
echo "Limit ". $limit . " ";
echo "ID ". $id . " ";
//echo $form;
//exit;
//echo "Backlink ". $backlink . " ";
//exit;
if ($numresults > 0 && $limit > 0) {
$filename = $id . ".csv";
$report = fopen($filename, 'w');
$xhtml = "";
$start = 1;
while ($start + $numresults -1 <= $limit) {
echo "Start: " .$start. " ";
$url = 'http://api.search.yahoo.com/WebSearchService/V1/webSearch?';
$params = setParams($id, $site, $numresults, $start);
$url = buildUrl($url, $params);
echo "URL: " .$url . " ";
$data = getXMLArray($url);
if ($data === false) {
$limit =1;
continue;
}
if ($data['ResultSet attr']['totalResultsAvailable'] < $limit) {
$limit = $data['ResultSet attr']['totalResultsAvailable'];
}
if ($data['ResultSet attr']['totalResultsReturned'] < $numresults) {
$limit = $start - 1 + $data['ResultSet attr']['totalResultsReturned'];
}
$xhtml .= $form.formatYahooResultset($data,$backlink);
$start += $numresults;
sleep(1);
}
fclose($report);
echo 'You can download this report HERE or click HERE for a more detailed report.';
}
} else {
// If an empty search was given:
$xhtml = (isset($_GET['p']) && $_GET['p'] == '') ? '
Please enter a search term
':'';
// Show the form:
$xhtml .= $form;
}
echo $xhtml;
//echo "Raw Data - " . print_r($data);
$duration = microtime_diff($timestart, microtime());
$duration = sprintf("%0.3f", $duration);
//echo "Logout
";
//echo "Add a Site
";
echo "(" . $duration . " seconds)
";
//
//
// Here be functions....
//
//
/**
* Build an array with the parameters we want to use.
*
*/
function setParams($id, $site, $numresults, $start) {
$params = array(
'appid' => $id,
'start' => $start,
'query' => urlencode($_GET['p']),
'results' => $numresults
);
return $params;
}
function setBLParams($id, $site,$numresults, $start) {
$params = array(
'appid' => $id,
'start' => $start,
'query' => urlencode("link:" .$site),
'results' => $numresults
);
return $params;
}
function setBLParamsMinusSite($id, $site,$numresults, $start) {
$url_a = parse_url($site);
$host = explode(".",$url_a['host']);
$url = implode(".", array_slice($host, 1));
$query = urlencode("link:" .$site." -site:" .$url);
echo "Checking: " .$site . " ";
//$domain = split( $site
$params = array(
'appid' => $id,
'start' => $start,
'query' => $query,
'results' => $numresults
);
return $params;
}
/**
* A small url building function:
*
*/
function buildUrl($url, $params) {
$qstring = ''; // Initialize...
foreach($params as $key => $value) { // Build query string...
$qstring .= '&'."$key=$value";
}
$qstring = ltrim($qstring,'&'); // Trim the fat...
return $url.$qstring;
}
/**
* This function accesses an XML file ($url), reads it into a variable,
* and then unserializes it into an associative array:
*/
function getXMLArray($url) {
$xml = '';
// Access the file ~ make the request:
$handle = fopen($url,"rb");
if ($handle === false)
return false;
while (!feof($handle)) {
// Read the resulting XML into the variabl $xml:
$xml .= fread($handle, 8192);
}
fclose($handle);
// Include the library:
include_once('xml.php');
// Unserialize the data:
$data = XML_unserialize($xml);
return $data;
}
function getBackLinkNumber ($blsite) {
global $id, $numresults;
$url2 = 'http://api.search.yahoo.com/WebSearchService/V1/webSearch?';
$params = setBLParams($id, $blsite, $numresults, $start);
$url2 = buildUrl($url2, $params);
$results = $url2;
$bldata = getXMLArray($url2);
if ($bldata === false)
$results = -1;
else
$results = $bldata['ResultSet attr']['totalResultsAvailable'];
return $results;
}
function getBackLinkNumberMinusSite ($blsite) {
global $id, $numresults;
$url2 = 'http://api.search.yahoo.com/WebSearchService/V1/webSearch?';
$params = setBLParamsMinusSite($id, $blsite, $numresults, $start);
$url2 = buildUrl($url2, $params);
$results = $url2;
$bldata = getXMLArray($url2);
if ($bldata === false)
$results = -1;
else
$results = $bldata['ResultSet attr']['totalResultsAvailable'];
return $results;
}
/**
* This function formats our Yahoo! specific array:
*
*/
function formatYahooResultset($data,$backlink) {
global $report;
//global $start;
// If no results were found:
if($data['ResultSet attr']['totalResultsReturned'] == 0) {
$results = 'There were 0 results found with Yahoo! search. Please try again by typing your search terms into the search box, and then click search.
';
return $results;
}
// Now down to business:
$count = $data['ResultSet attr']['firstResultPosition'];
$tmp = '';
foreach($data['ResultSet']['Result'] as $key => $value) {
$url = $data['ResultSet']['Result'][$key]['Url'];
//$report['url'] = $url;
//$html = download_pretending($url,"Yellowbrickroad");
//$a = pc_link_extractor($html);
$tmp .= ''.$count.'. '.$data['ResultSet']['Result'][$key]['Title'].'
';
$r = htmlentities($data['ResultSet']['Result'][$key]['Summary']);
$tmp .= ''.$r.' ';
$tmp .= ''.$data['ResultSet']['Result'][$key]['Url'].' ';
if ($backlink=="on"){
$num_bls = getBackLinkNumberMinusSite($data['ResultSet']['Result'][$key]['Url']);
$tmp .= 'X Backlinks: '.$num_bls.
' ';
}
//sleep(1);
//' Internal links: '.getBackLinkNumber($data['ResultSet']['Result'][$key]['Url']).'';
// ' Internal links: '.getBackLinkNumber($data['ResultSet']['Result'][$key]['Url']).' ';
/*
$tmp .= 'Links on page: '.count($a).' ';
$ads = "";
if ( find_adsense($html)) {
$tmp .= ' Adsense ';
$ads = "adsense";
}
*/
$tmp .= ' ';
$count++;
$sum = '"'.$url . '",'. $num_bls . "\r\n";
//$sum = $url.",".count($a).",".$ads . "\r\n";
fwrite($report,$sum);
}
//"1 - 10 of about 13,800,000 for truck tires"
$finish = $data['ResultSet attr']['totalResultsReturned'] + $data['ResultSet attr']['firstResultPosition'] - 1;
$results = ''.$data['ResultSet attr']['firstResultPosition'].' - '.$finish.' of about '.$data['ResultSet attr']['totalResultsAvailable'].' results provided by Yahoo! search.
'.$tmp.'
';
return $results;
}
function download_pretending($url,$user_agent) {
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
function find_adsense($html) {
#looking for
#