這篇文章主要介紹了php實現專業獲取網站SEO資訊類,實例分析了seoreport類針對網站SEO資訊檢查與獲取的技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了php實作專業取得網站SEO資訊類別。具體如下:
這個seo類別的功能包括:
- 檢查指定的網站回應
- 取得從該網站首頁的語言和其他meta標籤資料的
- 取得網站的導入鏈接,從Alexa的流量排名
- 獲取網站的導入鏈接,由谷歌索引的網頁數量
- 獲取網站的信任,從WOT排名。
- 獲取,因為它是第一個註冊的網站域名年齡
- 獲取的Twitter網站頁面的數量
- 獲取的Facebook鏈接的網站頁面
- 獲取網站谷歌網頁速度等級
- 取得網站的Google網頁排名
<?php /** * * SEO report for different metrics * * @category SEO * @author Chema <chema@garridodiaz.com> * @copyright (c) 2009-2012 Open Classifieds Team * @license GPL v3 * Based on seo report script http://www.phpeasycode.com && PHP class SEOstats * */ class seoreport{ /** * * check if a url is online/alive * @param string $url * @return bool */ public static function is_alive($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'curlHeaderCallback'); curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_exec ($ch); $int_return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close ($ch); if ($int_return_code != 200 && $int_return_code != 302 && $int_return_code != 304) { return FALSE; } else return TRUE; } /** * HTTP GET request with curl. * * @param string $url String, containing the URL to curl. * @return string Returns string, containing the curl result. * */ protected static function get_html($url) { $ch = curl_init($url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch,CURLOPT_MAXREDIRS,2); if(strtolower(parse_url($url, PHP_URL_SCHEME)) == 'https') { curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,1); } $str = curl_exec($ch); curl_close($ch); return ($str)?$str:FALSE; } /** * * get the domain from any URL * @param string $url */ public static function domain_name($url) { $nowww = ereg_replace('www\.','',$url); $domain = parse_url($nowww); if(!empty($domain["host"])) return $domain["host"]; else return $domain["path"]; } /** * * get the metas from a url and the language of the site * @param string $url * @return array */ public static function meta_info($url) { //doesn't work at mediatemple /*$html = new DOMDocument(); if(!$html->loadHtmlFile($url)) return FALSE;*/ if (!$html_content = self::get_html($url)) return FALSE; $html = new DOMDocument(); $html->loadHtml($html_content); $xpath = new DOMXPath( $html ); $url_info = array(); $langs = $xpath->query( '//html' ); foreach ($langs as $lang) { $url_info['language'] = $lang->getAttribute('lang'); } $metas = $xpath->query( '//meta' ); foreach ($metas as $meta) { if ($meta->getAttribute('name')) { $url_info[$meta->getAttribute('name')] = $meta->getAttribute('content'); } } return $url_info; } /** * * Alexa rank * @param string $url * @return integer */ public static function alexa_rank($url) { $domain = self::domain_name($url); $request = "http://data.alexa.com/data?cli=10&dat=s&url=" . $domain; $data = self::get_html($request); preg_match('/<POPULARITY URL="(.*?)" TEXT="([\d]+)"\/>/si', $data, $p); return ($l[2]) ? $l[2] : NULL; } /** * * Alexa inbounds link * @param string $url * @return integer */ public static function alexa_links($url) { $domain = self::domain_name($url); $request = "http://data.alexa.com/data?cli=10&dat=s&url=" . $domain; $data = self::get_html($request); preg_match('/<LINKSIN NUM="([\d]+)"\/>/si', $data, $l); return ($l[1]) ? $l[1] : NULL; } /** * Returns total amount of results for any Google search, * requesting the deprecated Websearch API. * * @param string $query String, containing the search query. * @return integer Returns a total count. */ public static function google_pages($url) { //$query = self::domain_name($url); $url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=1&q='.$url; $str = self::get_html($url); $data = json_decode($str); return (!isset($data->responseData->cursor->estimatedResultCount)) ? '0' : intval($data->responseData->cursor->estimatedResultCount); } /** * * gets the inbounds links from a site * @param string $url * @param integer */ public static function google_links($url) { $request = "http://www.google.com/search?q=" . urlencode("link:" . $url) . "&hl=en"; $data = self::get_html($request); preg_match('/<p id=resultStats>(About )?([\d,]+) result/si', $data, $l); return ($l[2]) ? $l[2] : NULL; } /** * * web of trust rating * @param string $url * @reutn integer */ public static function WOT_rating($url) { $domain = self::domain_name($url); $request = "http://api.mywot.com/0.4/public_query2?target=" . $domain; $data = self::get_html($request); preg_match_all('/<application name="(\d+)" r="(\d+)" c="(\d+)"\/>/si', $data, $regs); $trustworthiness = ($regs[2][0]) ? $regs[2][0] : NULL; return (is_numeric($trustworthiness))? $trustworthiness:NULL; } /** * * how old is the domain? * @param string $domain * @return integer unixtime */ public static function domain_age($domain) { $request = "http://reports.internic.net/cgi/whois?whois_nic=" . $domain . "&type=domain"; $data = self::get_html($request); preg_match('/Creation Date: ([a-z0-9-]+)/si', $data, $p); return (!$p[1])?FALSE:strtotime($p[1]); } /** * * counts how many tweets about the url * @param string $url * @return integer */ public static function tweet_count($url) { $url = urlencode($url); $twitterEndpoint = "http://urls.api.twitter.com/1/urls/count.json?url=%s"; $fileData = file_get_contents(sprintf($twitterEndpoint, $url)); $json = json_decode($fileData, true); unset($fileData); // free memory return (is_numeric($json['count']))? $json['count']:NULL; } /** * Returns the total amount of Facebook Shares for a single page * * @link https://graph.facebook.com/ * @param string The URL to check. * @return integer Returns the total amount of Facebook */ public static function facebook_shares($q) { //Execution and result of Json $str = self::get_html('http://graph.facebook.com/?id='.urlencode($q)); $data = json_decode($str); //Return only number of facebook shares $r = $data->shares; return ($r != NULL) ? $r : intval('0'); } /** * * get the pagespeed rank over 100 * @param string $url * @return integer */ public static function page_speed($url) { $url = 'https://developers.google.com/_apps/pagespeed/run_pagespeed?url='.$url.'&format=json'; $str = self::get_html($url); $data = json_decode($str); return intval($data->results->score); } /** * * get google page rank * @param string $url * @return integer */ public static function page_rank($url) { $query = "http://toolbarqueries.google.com/tbr?client=navclient-auto&ch=".self::CheckHash(self::HashURL($url)). "&features=Rank&q=info:".$url."&num=100&filter=0"; $data = self::get_html($query);//die(print_r($data)); $pos = strpos($data, "Rank_"); if($pos === false) { return NULL; } else { $pagerank = substr($data, $pos + 9); return $pagerank; } } // functions for google pagerank /** * To calculate PR functions */ public static function StrToNum($Str, $Check, $Magic) { $Int32Unit = 4294967296; // 2^32 $length = strlen($Str); for ($i = 0; $i < $length; $i++) { $Check *= $Magic; //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31), // the result of converting to integer is undefined // refer to http://www.php.net/manual/en/language.types.integer.php if ($Check >= $Int32Unit) { $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit)); //if the check less than -2^31 $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check; } $Check += ord($Str{$i}); } return $Check; } /** * Genearate a hash for a url */ public static function HashURL($String) { $Check1 = self::StrToNum($String, 0x1505, 0x21); $Check2 = self::StrToNum($String, 0, 0x1003F); $Check1 >>= 2; $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F); $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF); $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF); $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F ); $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 ); return ($T1 | $T2); } /** * genearate a checksum for the hash string */ public static function CheckHash($Hashnum) { $CheckByte = 0; $Flag = 0; $HashStr = sprintf('%u', $Hashnum) ; $length = strlen($HashStr); for ($i = $length - 1; $i >= 0; $i --) { $Re = $HashStr{$i}; if (1 === ($Flag % 2)) { $Re += $Re; $Re = (int)($Re / 10) + ($Re % 10); } $CheckByte += $Re; $Flag ++; } $CheckByte %= 10; if (0 !== $CheckByte) { $CheckByte = 10 - $CheckByte; if (1 === ($Flag % 2) ) { if (1 === ($CheckByte % 2)) { $CheckByte += 9; } $CheckByte >>= 1; } } return '7'.$CheckByte.$HashStr; } }
使用範例
<?php include 'seoreport.php'; ini_set('max_execution_time', 180); $url = (isset($_GET['url']))?$_GET['url']:'http://phpclasses.org'; $meta_tags = seoreport::meta_info($url); //die(var_dump($meta_tags)); //first check if site online if ($meta_tags!==FALSE) { $stats = array(); $stats['meta'] = $meta_tags; $stats['alexa']['rank'] = seoreport::alexa_rank($url); $stats['alexa']['links'] = seoreport::alexa_links($url); $stats['domain']['WOT_rating'] = seoreport::WOT_rating($url); $stats['domain']['domain_age'] = seoreport::domain_age($url); $stats['social']['twitter'] = seoreport::tweet_count($url); $stats['social']['facebook'] = seoreport::facebook_shares($url); $stats['google']['page_rank'] = seoreport::page_rank($url); $stats['google']['page_speed'] = seoreport::page_speed($url); $stats['google']['pages'] = seoreport::google_pages($url); $stats['google']['links'] = seoreport::google_links($url); var_dump($stats); } else 'Site not online. '.$url;
#總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。
相關推薦:
#以上是php基於seoreport類針對網站SEO資訊檢查與獲取的詳細內容。更多資訊請關注PHP中文網其他相關文章!

在PHP中,trait適用於需要方法復用但不適合使用繼承的情況。 1)trait允許在類中復用方法,避免多重繼承複雜性。 2)使用trait時需注意方法衝突,可通過insteadof和as關鍵字解決。 3)應避免過度使用trait,保持其單一職責,以優化性能和提高代碼可維護性。

依賴注入容器(DIC)是一種管理和提供對象依賴關係的工具,用於PHP項目中。 DIC的主要好處包括:1.解耦,使組件獨立,代碼易維護和測試;2.靈活性,易替換或修改依賴關係;3.可測試性,方便注入mock對象進行單元測試。

SplFixedArray在PHP中是一種固定大小的數組,適用於需要高性能和低內存使用量的場景。 1)它在創建時需指定大小,避免動態調整帶來的開銷。 2)基於C語言數組,直接操作內存,訪問速度快。 3)適合大規模數據處理和內存敏感環境,但需謹慎使用,因其大小固定。

PHP通過$\_FILES變量處理文件上傳,確保安全性的方法包括:1.檢查上傳錯誤,2.驗證文件類型和大小,3.防止文件覆蓋,4.移動文件到永久存儲位置。

JavaScript中處理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。 1.??返回第一個非null或非undefined的操作數。 2.??=將變量賦值為右操作數的值,但前提是該變量為null或undefined。這些操作符簡化了代碼邏輯,提高了可讀性和性能。

CSP重要因為它能防範XSS攻擊和限制資源加載,提升網站安全性。 1.CSP是HTTP響應頭的一部分,通過嚴格策略限制惡意行為。 2.基本用法是只允許從同源加載資源。 3.高級用法可設置更細粒度的策略,如允許特定域名加載腳本和样式。 4.使用Content-Security-Policy-Report-Only頭部可調試和優化CSP策略。

HTTP請求方法包括GET、POST、PUT和DELETE,分別用於獲取、提交、更新和刪除資源。 1.GET方法用於獲取資源,適用於讀取操作。 2.POST方法用於提交數據,常用於創建新資源。 3.PUT方法用於更新資源,適用於完整更新。 4.DELETE方法用於刪除資源,適用於刪除操作。

HTTPS是一種在HTTP基礎上增加安全層的協議,主要通過加密數據保護用戶隱私和數據安全。其工作原理包括TLS握手、證書驗證和加密通信。實現HTTPS時需注意證書管理、性能影響和混合內容問題。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版
中文版,非常好用

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。