Login   Register  
PHP Classes
elePHPant
Icontem

File: Alexa.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Sergey [YS.PRO]  >  Alexa  >  Alexa.class.php  >  Download  
File: Alexa.class.php
Role: Class source
Content type: text/plain
Description: Alexa class
Class: Alexa
Get site ranking information from Alexa site
Author: By
Last change: Changed ALEXA_SITE_INFO_URL const and parsing rules according to changes on alexa.com
Date: 5 years ago
Size: 1,455 bytes
 

Contents

Class file image Download
<?php
/**
 * @author [YS.PRO]
 * @copyright Copyright &copy; 2009, [YS.PRO]
 * http://ys-pro.com
 * @version 0.2
 */
class Alexa {

    const 
CURL_TIMEOUT 20;
    const 
ALEXA_SITE_INFO_URL 'http://www.alexa.com/siteinfo/';

    private 
$domain NULL;
    
    public function 
__construct($domain NULL) {
        if (!
is_null($domain)) {
            
$this->domain $domain;
        } else {
            throw new 
Exception('You must pass domain name to constructor!');
        }
    }
    
    public function 
setDomain($domain) {
        
$this->domain $domain;
    }

    public function 
getAlexaRank() {
        
$response $this->get(self::ALEXA_SITE_INFO_URL $this->domain);
        
// parse string with alexa ranking info
        
$regexp '#<div class="data .+?">(.*?)</div>#si';
        
preg_match($regexp$response$matches);
        if (!isset(
$matches[1])) {
            return 
FALSE;
        }
        
preg_match('#[\d,]+#s'$matches[1], $m);
        if (!isset(
$m[0])) {
            return 
FALSE;
        }
        
$rank $m[0];
        
// delete commas
        
$rank str_replace(','''$rank);
        return (int) 
$rank;
    }
    
    protected function 
get($url) {
        
$hCurl curl_init($url);
        
curl_setopt($hCurlCURLOPT_TIMEOUTself::CURL_TIMEOUT);
        
curl_setopt($hCurlCURLOPT_RETURNTRANSFERTRUE);
        return 
curl_exec($hCurl);
    }
}
?>