Dunque ho usato alcune funzioni trovate su php.net ed una classe trovata sul forum(http://www.devpro.it/code/38.html ) trovata su http://www.devpro.it/:
Questa funzione (trovata su php.net) consente di estrarre il contenuto di un tag è può essere utile per risalire al titolo della pagina.
Codice PHP:
function TagContent($page,$tag,$endtag){
// tags
$start = $tag;
$end = $endtag;
// open the file
$fp = @fopen( $page, 'r' );
$cont = "";
// read the contents
while( !@feof( $fp ) ) {
$buf = trim( @fgets( $fp, 4096 ) );
$cont .= $buf;
}
// get tag contents
preg_match( "/$start(.*)$end/s", $cont, $match );
// tag contents
$contents = $match[ 1 ];
return $contents;
}
poi ho usato questa funzione fatta da me:
Codice PHP:
function GetSiteInfo($url){
if(TagContent($url,"<title>","<\/title>") != ""){
$title = rtrim(TagContent($url,"<title>","<\/title>"));
}else{
$title = rtrim(TagContent($url,"<TITLE>","<\/TITLE>"));}
$meta_tags = get_meta_tags($url);
$page_code = implode(file($url));
$page_body_code = $page_code;
$page_text = strip_tags($page_code);
preg_match("/^(http:\/\/)?([^\/]+)/i",$url, $matches);
$NomeHost = $matches[2];
$Info['title'] = $title;
$Info['meta-tag'] = $meta_tags;
$Info['Host'] = $NomeHost;
$Info['Page-Text'] = $page_text;
$Info['Page-Code'] = $page_code;
return $Info;
}
per ottenere un array con tutte le principali informazioni
e questa per registrare le informazioni nel database
Codice PHP:
function InserPage($title,$url,$domain,$descr,$keys,$TextContent,$meta_tags,$chace_info,$cache){
$link = mysql_connect ("host","user,"pass");
$sel = mysql_select_db ("dtabase",$link);
$inser="INSERT INTO `Motore Ricerca Web` ( `Titolo` ,`Url` , `Dominio` , `Descrizione` , `keyword` , `Contenuti Testuali` , `meta-tags` , `Cache Info` , `Cache` ) ";
$value="VALUES ('".htmlspecialchars($title)."', '".$url."', '".$domain."', '".htmlentities($descr,ENT_QUOTES)."', '".$keys."', '".htmlentities($TextContent,ENT_QUOTES)."', '".http_build_query($meta_tags)."', '".$chace_info."', '".htmlentities($cache,ENT_QUOTES)."');";
$querys = $inser.$value;
$risultato = mysql_query($querys);
if($risultato){return "Registred";}else{return "Error";}
}
Ed ecco tutto il codice completo che uso:
Codice PHP:
//Creazione classe
include("class.LinkExtractor.php");//Trovata sul forum di html.it non mi ricordo da chi
$myLinks = &new LinkExtractor();
//Acquisizione informazioni dell'indirizzo tramite l'apposita funzione
$site_info = GetSiteInfo($url);
//Settaggio variabili contenenti Descrizione e Keywords
$descrizione = $site_info['meta-tag']['description'];
$keyword = $site_info['meta-tag']['keywords'];
//Unset
unset($site_info['meta-tag']['description']);
unset($site_info['meta-tag']['keywords']);
//Chiamata alla funzione che inserisce la pagina nel database
if (in_use($url) == false){
print $url." ".InserPage($site_info['title'],$url,$site_info['Host'],$descrizione,$keyword,$site_info['Page-Text'],$site_info['meta-tag'],"",$site_info['Page-Code'])."
";
}else{print $url." Present
";}
//Estrazione dei link contenuti nella pagina
if( $myLinks->parseUrl($url) == true ) {
for( $a = 0, $b = count( $PageLinks = $myLinks->getLinks() ); $a < $b; $a++ ) {}
}
//Ciclo for per la scrittura delle main page nel database
for($i=0;$i<count($PageLinks);$i++){
//Formattazione dell'indirizzo per renderlo corretto
if(substr(str_replace("./","/",$PageLinks[$i]),0,7) == "http://"){$pre = "";}else{if(substr(str_replace("./","/",$PageLinks[$i]),0,1) == "/"){$pre = $url;}else{$pre = $url."/";};}
$page_url = $pre.str_replace("./","/",$PageLinks[$i]);
//Acquisizione delle informazioni della pagina in un array
$site_info = GetSiteInfo($page_url);
//Stampa dell'indirizzo
$log_info = $log_info + $page_url;
print $page_url;
//Controllo della presenza dell'indirizzo nel database
if (in_use($page_url) == false){
$descrizione = $site_info['meta-tag']['description'];
$keyword = $site_info['meta-tag']['keywords'];
unset($site_info['meta-tag']['description']);
unset($site_info['meta-tag']['keywords']);
//Stampa del risultato dell'operazione di inserimento
$insert_result = " ".InserPage($site_info['title'],$page_url,$site_info['Host'],$descrizione,$keyword,$site_info['Page-Text'],$site_info['meta-tag'],"",CreateCache($page_url))."
";
$log_info = $log_info + $insert_result;
print $insert_result;
}else{print " Present
";}
if($subpage == 1){
if( $myLinks->parseUrl($page_url) == true ) {
for( $a = 0, $b = count( $PageSubLinks = $myLinks->getLinks() ); $a < $b; $a++ ){
if(substr(str_replace("./","/",$PageSubLinks[$a]),0,7) == "http://"){$pre = "";}else{if(substr(str_replace("./","/",$PageSubLinks[$a]),0,1) == "/"){$pre = $url;}else{$pre = $url."/";};}
$sub_page_url = $pre.str_replace("./","/",$PageSubLinks[$a]);
$log_info = $log_info + "".$sub_page_url."
";
if (in_use($sub_page_url) == false){print "".$sub_page_url."
";}
}
}
}
}
############### Funzioni ##########
function http_build_query($formdata, $numeric_prefix = ""){
$arr = array();
foreach ($formdata as $key => $val)
$arr[] = urlencode($numeric_prefix.$key)."=".urlencode($val);
return implode($arr, "&");
}
function InserPage($title,$url,$domain,$descr,$keys,$TextContent,$meta_tags,$chace_info,$cache){
$link = mysql_connect ("host","user","pass")
$sel = mysql_select_db ("database",$link);
$inser="INSERT INTO `Motore Ricerca Web` ( `Titolo` ,`Url` , `Dominio` , `Descrizione` , `keyword` , `Contenuti Testuali` , `meta-tags` , `Cache Info` , `Cache` ) ";
$value="VALUES ('".htmlspecialchars($title)."', '".$url."', '".$domain."', '".htmlentities($descr,ENT_QUOTES)."', '".$keys."', '".htmlentities($TextContent,ENT_QUOTES)."', '".http_build_query($meta_tags)."', '".$chace_info."', '".htmlentities($cache,ENT_QUOTES)."');";
$querys = $inser.$value;
$risultato = mysql_query($querys);
if($risultato){return "Registred";}else{return "Error";}
}
function CreateCache($url){
$url1 = str_replace("http://","",$url);
$url1 = str_replace("/","()",$url1);
$url1 = str_replace("?","[]",$url1);
$file_name = "./cache/".$url1.".htm";
$file_code = file($url);
$page_text = implode($file_code);
$fp = fopen($file_name,"w+");
$result = fwrite($fp,$page_text);
return $url1;
fclose($fp);
}
function in_use($url){
$link = mysql_connect ("host","user","pass")
$sel = mysql_select_db ("database",$link);
$querys = "SELECT * FROM `Motore Ricerca Web` WHERE `Url` = '".$url."'";
$risultato = mysql_query($querys);
$risultato = mysql_query($querys);
if (!mysql_num_rows($risultato)){
return false;
}else{
return true;
}
mysql_close();
}
function GetSiteInfo($url){
if(TagContent($url,"<title>","<\/title>") != ""){
$title = rtrim(TagContent($url,"<title>","<\/title>"));
}else{
$title = rtrim(TagContent($url,"<TITLE>","<\/TITLE>"));}
$meta_tags = get_meta_tags($url);
$page_code = implode(file($url));
$page_body_code = $page_code;
$page_text = strip_tags($page_code);
preg_match("/^(http:\/\/)?([^\/]+)/i",$url, $matches);
$NomeHost = $matches[2];
$Info['title'] = $title;
$Info['meta-tag'] = $meta_tags;
$Info['Host'] = $NomeHost;
$Info['Page-Text'] = $page_text;
$Info['Page-Code'] = $page_code;
return $Info;
}
function TagContent($page,$tag,$endtag){
// tags
$start = $tag;
$end = $endtag;
// open the file
$fp = @fopen( $page, 'r' );
$cont = "";
// read the contents
while( !@feof( $fp ) ) {
$buf = trim( @fgets( $fp, 4096 ) );
$cont .= $buf;
}
// get tag contents
preg_match( "/$start(.*)$end/s", $cont, $match );
// tag contents
$contents = $match[ 1 ];
return $contents;
}