PHP to achieve professional access to Web site SEO information class examples _php skills

Source: Internet
Author: User
Tags curl hash php class php programming sprintf strlen urlencode xpath

This article describes the PHP to achieve professional access to Web site SEO information class. Share to everyone for your reference. Specifically as follows:

The features of this SEO class include:
-Check the specified Web site response
-Get the language and other meta tag data from the home page of the site
-Get the site's import links, ranking from Alexa traffic
-Access to the site's import links, the number of pages indexed by Google
-Get the trust of the website from WOT rankings.
-GET, because it is the first registered website domain name age
-Number of pages on the Twitter site obtained
-Get the Site page of the Facebook link
-Get the website speed level of Google page
-Get the site's Google page rankings

<?php/** * SEO for different metrics * * @category seo * @author chema <chema@garridodiaz.com> * @copyright (c) 2009-2012 Open Classifieds Team * @license GPL v3 * Based on SEO script http://www.phpeasycode.co M && 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!= && $int _return_code!= 302 && $int _return_code!= 304) {return fals
    E
  else return TRUE;
   }/** * HTTP GET request with curl. * * @param string $urlString, 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 Mediatempl
    E/* $html = new DOMDocument (); if (! $html->loadhtmlfile ($url)) return false;*/if (! $html _content = self::get_html ($url)) return Fals
    E
    $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::d omain_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_li
    Nks ($url) {$domain = self::d omain_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::d omain_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 Fu Nction Google_links ($url) {$request = "http://www.google.com/search?q=". UrlEncode ("Link:". $url).
    "&hl=en";
    $data = self::get_html ($request); Preg_match ('/<div id=resultstats> ')? (
    [\d,]+) Result/si ', $data, $l); Return ($l [2])?
  $l [2]: NULL; /** * * Web of trust rating * @param string $url * @reutn integer/public static function Wot_ratin
    G ($url) {$domain = self::d omain_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) {//ex
    Ecution 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 @param string $url * @return integer/public static funct Ion 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_ra NK ($url) {$query = "http://toolbarqueries.google.com/tbr?client=navclient-auto&ch=". Self::checkhash (Self::has HURL ($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) {$Int 32Unit = 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 inte Ger is undefined
      Refer to http://www.php.net/manual/en/language.types.integer.php if ($Check >= $Int 32Unit) {$C
        Heck = ($Check-$Int 32Unit * (int) ($Check/$Int 32Unit)); If the check less than-2^31 $Check = ($Check < 2147483648)?
      ($Check + $Int 32Unit): $Check;
    $Check + + ord ($Str {$i});
  return $Check; }/** * Genearate a hash for a URL/public static function Hashurl ($String) {$Check 1 = self::strtonum ($
    String, 0x1505, 0x21);
    $Check 2 = self::strtonum ($String, 0, 0x1003f);
    $Check 1 >>= 2; $Check 1 = (($Check 1 >> 4) & 0x3ffffc0) |
    ($Check 1 & 0x3F); $Check 1 = (($Check 1 >> 4) & 0x3ffc00) |
    ($Check 1 & 0x3ff); $Check 1 = (($Check 1 >> 4) & 0x3c000) |
    ($Check 1 & 0x3fff); $T 1 = ((($Check 1 & 0x3c0) << 4) | ($Check 1 & 0x3c)) &LT;&LT;2) |
    ($Check 2 & 0xf0f); $T 2 = ((($Check 1 & 0xffffc000) << 4) | ($Check 1 & 0x3C00)) << 0xA) |
    ($Check 2 & 0xf0f0000);
  Return ($T 1 | $T 2); }/** * Genearate a checksum for the hash string/public static function Checkhash ($Hashnum) {$CheckByt
    e = 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; }
}

Use example

<?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));
    The 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::d omain_age ($url);  
    $stats [' Social '] [' Twitter '] = Seoreport::tweet_count ($url);
    $stats [' Social '] [' Facebook '] = Seoreport::facebook_shares ($url);
    $stats [' Google '] [' page_rank '] = Seoreport::p age_rank ($url);
    $stats [' Google '] [' page_speed '] = Seoreport::p age_speed ($url);
    $stats [' Google '] [' pages '] = seoreport::google_pages ($url);
    $stats [' Google '] [' links '] = Seoreport::google_links ($url);
 Var_dump ($stats); Else ' Site not online. '. $url;

I hope this article will help you with your PHP programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.