登录  /  注册

php基于seoreport类针对网站SEO信息检查与获取

墨辰丷
发布: 2018-06-11 17:24:29
原创
2817人浏览过

这篇文章主要介绍了php实现专业获取网站seo信息类,实例分析了seoreport类针对网站seo信息检查与获取的技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php实现专业获取网站SEO信息类。具体如下:

这个seo类的功能包括:
- 检查指定的网站响应
- 获取从该网站主页的语言和其他meta标签数据的
- 获取网站的导入链接,从Alexa的流量排名
- 获取网站的导入链接,由谷歌索引的网页数量
- 获取网站的信任,从WOT排名。
- 获取,因为它是第一个注册的网站域名年龄
- 获取的Twitter网站页面的数量
- 获取的Facebook链接的网站页面
- 获取网站谷歌网页速度等级
- 获取网站的谷歌网页排名

<?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, &#39;curlHeaderCallback&#39;);
    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)) == &#39;https&#39;)
    {
      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(&#39;www\.&#39;,&#39;&#39;,$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&#39;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( &#39;//html&#39; );
    foreach ($langs as $lang)
    {
      $url_info[&#39;language&#39;] = $lang->getAttribute(&#39;lang&#39;);
    }
    $metas = $xpath->query( &#39;//meta&#39; );
    foreach ($metas as $meta)
    {
      if ($meta->getAttribute(&#39;name&#39;))
      {
        $url_info[$meta->getAttribute(&#39;name&#39;)] = $meta->getAttribute(&#39;content&#39;);
      }
    }
    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(&#39;/<POPULARITY URL="(.*?)" TEXT="([\d]+)"\/>/si&#39;, $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(&#39;/<LINKSIN NUM="([\d]+)"\/>/si&#39;, $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 = &#39;http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=1&q=&#39;.$url;
    $str = self::get_html($url);
    $data = json_decode($str);
    return (!isset($data->responseData->cursor->estimatedResultCount))
        ? &#39;0&#39;
        : 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(&#39;/<p id=resultStats>(About )?([\d,]+) result/si&#39;, $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(&#39;/<application name="(\d+)" r="(\d+)" c="(\d+)"\/>/si&#39;, $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(&#39;/Creation Date: ([a-z0-9-]+)/si&#39;, $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[&#39;count&#39;]))? $json[&#39;count&#39;]: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(&#39;http://graph.facebook.com/?id=&#39;.urlencode($q));
    $data = json_decode($str);
    //Return only number of facebook shares
    $r = $data->shares;
    return ($r != NULL) ? $r : intval(&#39;0&#39;);
  }
  /**
   *
   * get the pagespeed rank over 100
   * @param string $url
   * @return integer
   */
  public static function page_speed($url)
  {
    $url = &#39;https://developers.google.com/_apps/pagespeed/run_pagespeed?url=&#39;.$url.&#39;&format=json&#39;;
    $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(&#39;%u&#39;, $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 &#39;7&#39;.$CheckByte.$HashStr;
  }
}
登录后复制

使用范例

<?php
include &#39;seoreport.php&#39;;
ini_set(&#39;max_execution_time&#39;, 180);
  $url = (isset($_GET[&#39;url&#39;]))?$_GET[&#39;url&#39;]:&#39;http://phpclasses.org&#39;;
  $meta_tags = seoreport::meta_info($url);
  //die(var_dump($meta_tags));
  //first check if site online
  if ($meta_tags!==FALSE)
  {
    $stats = array();
    $stats[&#39;meta&#39;] = $meta_tags;
    $stats[&#39;alexa&#39;][&#39;rank&#39;] = seoreport::alexa_rank($url);
    $stats[&#39;alexa&#39;][&#39;links&#39;] = seoreport::alexa_links($url);
    $stats[&#39;domain&#39;][&#39;WOT_rating&#39;] = seoreport::WOT_rating($url);  
    $stats[&#39;domain&#39;][&#39;domain_age&#39;] = seoreport::domain_age($url);  
    $stats[&#39;social&#39;][&#39;twitter&#39;] = seoreport::tweet_count($url);  
    $stats[&#39;social&#39;][&#39;facebook&#39;] = seoreport::facebook_shares($url);
    $stats[&#39;google&#39;][&#39;page_rank&#39;] = seoreport::page_rank($url);
    $stats[&#39;google&#39;][&#39;page_speed&#39;] = seoreport::page_speed($url);
    $stats[&#39;google&#39;][&#39;pages&#39;] = seoreport::google_pages($url);
    $stats[&#39;google&#39;][&#39;links&#39;] = seoreport::google_links($url);
    var_dump($stats);
  }
  else &#39;Site not online. &#39;.$url;
登录后复制

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php操作图片的大小修改、加水印、生成验证码、输出及保存

php流程控制及数学运算

php实现新闻发布系统

以上就是php基于seoreport类针对网站SEO信息检查与获取的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号