Getting PR and Alexa.

Discussion in 'PHP' started by mbaldwin, Aug 18, 2011.

  1. #1
    Hi,
    I am looking for direction for creating a script, or finding one for getting at least Google PR and Alexa ranking. I am building a database which will contain a few thousand links, and I would like to be able to set a cron and have the PR and Alexa updated on a regular basis.

    Thanks,
    Michael
     
    Solved! View solution.
    mbaldwin, Aug 18, 2011 IP
  2. mbaldwin

    mbaldwin Active Member

    Messages:
    215
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    95
    #2
    Well, I found a script a few posts down at '
    http://forums.digitalpoint.com/showthread.php?t=2260553


    To get the Google PR, but I keep getting a pr of -1 returned to me. Now I am running this on my local machine right now, does this need to be uploaded to my server to actually work?

    Thanks,
    Michael
     
    mbaldwin, Aug 18, 2011 IP
  3. pararudalf

    pararudalf Peon

    Messages:
    209
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes It requires to upload in server.
     
    pararudalf, Aug 18, 2011 IP
  4. #4
    Includes Alexa rank, all you need to do is set up your mysql statement to grab urls from your database.

    
    <?php
    
    $update_rank = 'http://digitalpoint.com';
    
    
    define('GOOGLE_MAGIC', 0xE6359A60);
            function _zeroFill($a, $b){
                $z = hexdec(80000000);
                if ($z & $a){
                    $a = ($a>>1);
                    $a &= (~$z);
                    $a |= 0x40000000;
                    $a = ($a>>($b-1));
                }else
                    $a = ($a>>$b);
                return $a;
            }
    
            function _mix($a,$b,$c){
                $a -= $b; $a -= $c; $a ^= (_zeroFill($c,13));
                $b -= $c; $b -= $a; $b ^= ($a<<8);
                $c -= $a; $c -= $b; $c ^= (_zeroFill($b,13));
                $a -= $b; $a -= $c; $a ^= (_zeroFill($c,12));
                $b -= $c; $b -= $a; $b ^= ($a<<16);
                $c -= $a; $c -= $b; $c ^= (_zeroFill($b,5));
                $a -= $b; $a -= $c; $a ^= (_zeroFill($c,3));
                $b -= $c; $b -= $a; $b ^= ($a<<10);
                $c -= $a; $c -= $b; $c ^= (_zeroFill($b,15));
                return array($a,$b,$c);
            }
            
            function _GoogleCH($url, $length=null, $init=GOOGLE_MAGIC){
                if(is_null($length))
                    $length = sizeof($url); 
                $a = $b = 0x9E3779B9;
                $c = $init;
                $k = 0;
                $len = $length;
                while($len >= 12){
                    $a += ($url[$k+0] +($url[$k+1]<<8) +($url[$k+2]<<16) +($url[$k+3]<<24));
                    $b += ($url[$k+4] +($url[$k+5]<<8) +($url[$k+6]<<16) +($url[$k+7]<<24));
                    $c += ($url[$k+8] +($url[$k+9]<<8) +($url[$k+10]<<16)+($url[$k+11]<<24));
                    $_mix = _mix($a,$b,$c);
                    $a = $_mix[0]; $b = $_mix[1]; $c = $_mix[2];
                    $k += 12;
                    $len -= 12;
                }
                $c += $length;
                switch($len){
                    case 11: $c+=($url[$k+10]<<24);
                    case 10: $c+=($url[$k+9]<<16);
                    case 9 : $c+=($url[$k+8]<<8);
                    case 8 : $b+=($url[$k+7]<<24);
                    case 7 : $b+=($url[$k+6]<<16);
                    case 6 : $b+=($url[$k+5]<<8);
                    case 5 : $b+=($url[$k+4]);
                    case 4 : $a+=($url[$k+3]<<24);
                    case 3 : $a+=($url[$k+2]<<16);
                    case 2 : $a+=($url[$k+1]<<8);
                    case 1 : $a+=($url[$k+0]);
                }
                $_mix = _mix($a,$b,$c);
                return $_mix[2];
            }
    
            function _strord($string){
                for($i=0;$i<strlen($string);$i++)
                    $result[$i] = ord($string{$i});
                return $result;
            }
            
            function getPageRank($url){
                $pagerank = -1;
                $ch = "6"._GoogleCH(_strord("info:" . $url));
                $fp = fsockopen("www.google.com", 80, $errno, $errstr, 30);
                if($fp){
                    $out = "GET /search?client=navclient-auto&ch=" . $ch .  "&features=Rank&q=info:" . $url . " HTTP/1.1\r\n";
                    $out .= "Host: www.google.com\r\n";
                    $out .= "Connection: Close\r\n\r\n";
                    fwrite($fp, $out);
                    while (!feof($fp)){
                        $data = fgets($fp, 128);
                        $pos = strpos($data, "Rank_");
                        if($pos === false){
                        }else
                            $pagerank = substr($data, $pos + 9);
                    }
                    fclose($fp);
                }
                return $pagerank;
            }
    
    function alexa_rank($url) {
        $url = trim($url);
        if (!preg_match('~^http://~i', $url))
            $url = "http://{$url}";
        $host = parse_url($url, PHP_URL_HOST);
        $host = preg_replace('~^www\.~', NULL, strtolower($host));
        preg_match('~\<POPULARITY.+?TEXT="(\d+)"/\>~', file_get_contents('http://data.alexa.com/data?cli=10&dat=s&url=' . $host), $a);
        return $a[1];
    }
    
    //USAGE
    
      $pr = getPageRank($update_rank);
      $alexa = alexa_rank($update_rank);
    
    
    
    // sample values print out
    echo $pr;
    
    echo "<br />";
    
    echo $alexa;
    
    ?> 
    
    
    PHP:
     
    MyVodaFone, Aug 18, 2011 IP
  5. mbaldwin

    mbaldwin Active Member

    Messages:
    215
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    95
    #5
    Thanks very much for that. I should be able to get the rest figured out. I just wasn't sure how to get the info I needed.

    Michael
     
    mbaldwin, Aug 19, 2011 IP
  6. nashed

    nashed Guest

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you can find many free script on google ;)
     
    nashed, Aug 21, 2011 IP
  7. mbaldwin

    mbaldwin Active Member

    Messages:
    215
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    95
    #7
    I am getting a lot of pr returned as -1. I have tested a few of the sites manually, and they work, and return a pr. Does google limit the number of searches or doing something that might be blocking me if I do to many at a time? or is there something else that might be going on.

    Thanks,
    Michael
     
    mbaldwin, Aug 21, 2011 IP