Hi, i want to know that how to get the yahoo back links from yahoo using yahoo API in PHP, please hep me out the way to do that Thanks
function getYahooLinks($domain) { $yahooAppId1 = "YOUR_YAHOO_KEY"; $yahoourl="http://search.yahooapis.com/SiteExplorerService/V1/inlinkData?appid=".$appid."&query=".$domain."&entire_site=1&omit_inlinks=domain&results=1&output=php"; $data = @file_get_contents($yahoourl); $results=unserialize($data); return $results['ResultSet']['totalResultsAvailable']; //$results[0][1]; }
Like this: <? function getYahooLinks($domain) { $appid = "31245124213"; $yahoourl="http://search.yahooapis.com/SiteExplorerService/V1/inlinkData?appid=".$appid."&query=".$domain."&entire_site=1&omit_inlinks=domain&results=1&output=php"; $data = @file_get_contents($yahoourl); $results=unserialize($data); return $results['ResultSet']['totalResultsAvailable']; //$results[0][1]; } echo getYahooLinks("digitalpoint.com"); ?> PHP: It will tell u total number of backlinks.
If you want a complete working script (alonwith site names, URLs and paging), I'll post it here on Monday, as I'm busy right now.
Here is the code. Save it as "index.php". It requires "xml.php" too. index.php <?php /* Title: Yahoo backlinks checker Author: Arsalan Jamshed Email: arsalan@indo-pak.org Feel free to modify or refine the code */ ini_set('error_reporting', 'E_ALL & ~E_NOTICE'); set_time_limit(60); include('xml.php'); $domain = $_GET['domain']; ?> <br /><br /> <p align=center><strong>Find Backlinks</strong><br /> <form id="form1" name="form1" method="get" action=""> Enter URL: <input type="text" name="domain" id="domain" /> <input name="Submit" type="submit" value="Submit" /> (e.g google.com) </form> </p> <? if($domain!="") { $id = '785437HGDer'; // enter your Yahoo API key here $start = $_GET['res']; $numresults = 16; if($start=="") $start = 1; $url = 'http://search.yahooapis.com/SiteExplorerService/V1/inlinkData?'; $params = setParams($id, $start,$numresults, $domain); $url = buildUrl($url, $params); $data = getXMLArray($url); $xhtml = formatYahooResultset($data); } /** * Build an array with the parameters we want to use. * */ function setParams($id,$start,$numresults, $domain) { $params = array( 'appid' => $id, 'query' => $domain, 'results' => $numresults, 'entire_site' => '1', 'omit_inlinks'=> 'domain', 'start' => $start ); return $params; } /** * A url building function: * */ function buildUrl($url, $params) { $qstring = ''; // Initialize... foreach($params as $key => $value) { // Build query string... $qstring .= '&'."$key=$value"; } $qstring = ltrim($qstring,'&'); 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"); while (!feof($handle)) { // Read the resulting XML into the variabl $xml: $xml .= fread($handle, 8192); } fclose($handle); // Unserialize the data: $data = XML_unserialize($xml); return $data; } function formatYahooResultset($data) { $domain = $_GET['domain']; // If no results were found: if($data['ResultSet attr']['totalResultsReturned'] == 0) { $results= "no result found"; return $results; } // Now down to business: $tmp = ""; $mycounter=0; foreach($data['ResultSet']['Result'] as $key => $value) { $mycounter++; $myurl = $data['ResultSet']['Result'][$key]['Url']; $mytitle = $data['ResultSet']['Result'][$key]['Title']; $tmp.="<a href='$myurl'>$mytitle</a><br><font color=green size=2>$myurl</font><br><br>"; } $temp = $data['ResultSet attr']['totalResultsAvailable']; $mycalculation = $data['ResultSet attr']['totalResultsAvailable']; $str = $_GET['p']; $res = $_GET['res']; $n_res = $res+10; $pr_res = $res-10; $temp = number_format($temp); $enc = stripslashes($str); $enc = stripslashes($enc); $enc = urlencode($enc); $mycalculation = $mycalculation-$res; if($res!=1) //if it is not the first page { if($mycalculation>=10) { echo "<table align=center bgcolor=#F1E0F1 border=0 width=100%><tr><td align=center><font size=2 face=verdana><a href=index.php?res=$pr_res&domain=$domain><b>Previous Page</b></a> <font size=2 face=verdana>Approximately <b>$temp</b> results found. <a href=index.php?res=$n_res&domain=$domain><font face=verdana size=2><b>Next Page</b></font></font></a></center></td></tr></table></font>".$viewtext."<p>".$tmp."</p><table align=center bgcolor=#F1E0F1 border=0 width=100%><tr><td align=right><a href=index.php?res=$pr_res&domain=$domain><font face=verdana size=2><b>Previous Page</b></font></a> <a href=index.php?res=$n_res&domain=$domain><font size=2 face=verdana><b>Next Page</b></font></a></font></td></tr></table>"; } else { echo "<font size=1><table align=center bgcolor=#F1E0F1 border=0 width=100%><tr><td><center><font size=2 face=verdana><a href=index.php?res=$pr_res&domain=$domain><b>Previous Page</b></a></font><font size=2 face=verdana>Approximately <b>$temp</b> results found.</font></center></td></tr></table></font>".$viewtext."<p>".$tmp."</p><table align=center bgcolor=#F1E0F1 border=0 width=100%><tr><td align=right><a href=index.php?res=$pr_res&domain=$domain><font face=verdana size=2><b>Previous Page</b></font></a></td></tr></table>"; } } else { echo "<table align=center bgcolor=#F1E0F1 border=0 width=100%><tr><td><center><font face=verdana size=2>Approximately <b>$temp</b> results found. <a href=index.php?res=$n_res&domain=$domain><b>Next Page</b></a></font></center></td></tr></table>".$viewtext."<p>".$tmp."</p><table align=center bgcolor=#F1E0F1 border=0 width=100%><tr><td align=right><a href=index.php?res=$n_res&domain=$domain><font face=verdana size=2><b>Next Page</b></font></a></td></tr></table>"; } } echo "</font>"; ?> PHP:
Here is xml.php. Place it in the same folder. <?php ################################################################################### # XML_unserialize: takes raw XML as a parameter (a string) # and returns an equivalent PHP data structure ################################################################################### function & XML_unserialize(&$xml){ $xml_parser = &new XML(); $data = &$xml_parser->parse($xml); $xml_parser->destruct(); return $data; } ################################################################################### # XML_serialize: serializes any PHP data structure into XML # Takes one parameter: the data to serialize. Must be an array. ################################################################################### function & XML_serialize(&$data, $level = 0, $prior_key = NULL){ if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; } while(list($key, $value) = each($data)) if(!strpos($key, ' attr')) #if it's not an attribute #we don't treat attributes by themselves, so for an empty element # that has attributes you still need to set the element to NULL if(is_array($value) and array_key_exists(0, $value)){ XML_serialize($value, $level, $key); }else{ $tag = $prior_key ? $prior_key : $key; echo str_repeat("\t", $level),'<',$tag; if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element while(list($attr_name, $attr_value) = each($data["$key attr"])) echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"'; reset($data["$key attr"]); } if(is_null($value)) echo " />\n"; elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n"; else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n"; } reset($data); if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; } } ################################################################################### # XML class: utility class to be used with PHP's XML handling functions ################################################################################### class XML{ var $parser; #a reference to the XML parser var $document; #the entire XML structure built up so far var $parent; #a pointer to the current parent - the parent will be an array var $stack; #a stack of the most recent parent at each nesting level var $last_opened_tag; #keeps track of the last tag opened. function XML(){ $this->parser = &xml_parser_create(); xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false); xml_set_object(&$this->parser, &$this); xml_set_element_handler(&$this->parser, 'open','close'); xml_set_character_data_handler(&$this->parser, 'data'); } function destruct(){ xml_parser_free(&$this->parser); } function & parse(&$data){ $this->document = array(); $this->stack = array(); $this->parent = &$this->document; return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL; } function open(&$parser, $tag, $attributes){ $this->data = ''; #stores temporary cdata $this->last_opened_tag = $tag; if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric #this is the third or later instance of $tag we've come across $key = count_numeric_items($this->parent[$tag]); }else{ #this is the second instance of $tag that we've seen. shift around if(array_key_exists("$tag attr",$this->parent)){ $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]); unset($this->parent["$tag attr"]); }else{ $arr = array(&$this->parent[$tag]); } $this->parent[$tag] = &$arr; $key = 1; } $this->parent = &$this->parent[$tag]; }else{ $key = $tag; } if($attributes) $this->parent["$key attr"] = $attributes; $this->parent = &$this->parent[$key]; $this->stack[] = &$this->parent; } function data(&$parser, $data){ if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags $this->data .= $data; } function close(&$parser, $tag){ if($this->last_opened_tag == $tag){ $this->parent = $this->data; $this->last_opened_tag = NULL; } array_pop($this->stack); if($this->stack) $this->parent = &$this->stack[count($this->stack)-1]; } } function count_numeric_items(&$array){ return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0; } ?> PHP:
Thanks Wowla, I installed your code, and it works nicely. I'm trying to get ALL my backlink data, so I can import it in to my own application. Or, I'm looking for a way to get the number of unique domains that link to me, and a list of those domains. I'm posting here so that if you decide to include these features in your script, I'll get a thread update notice. Thanks for your work!
Thanks wowla! Your script works better than the commercial products I've been using for backlinks. Any thoughts on producing our link tools or incorporating more functions? Do let me know please if you produce an enhanced tool.