Alexa API, PHP and getting traffic rankings

Discussion in 'PHP' started by mad4, Oct 5, 2006.

  1. #1
    I've been trying the official scripts for getting the traffic rankings using the Alexa API but it fails on about 75% of requests.

    Has anyone got any tips or scripts that actually work or do I need to screen scrape?

    The official script:
    <?php
    
    // Make a request to the AWIS UrlInfo operation to get information about a given url
    
    define("ACCESS_KEY", "--- Replace with your access key id ---");
    define("SECRET_ACCESS_KEY", "--- Replace with your secret access key ---");
    
    define("SERVICE", "AlexaWebInfoService");
    define("OPERATION", "UrlInfo");
    define("RESPONSE_GROUP", "Rank");
    
    $site_url = "yahoo.com";
    
    echo("For site: " . $site_url."\n\n");
    
    $awis_url = generate_rest_url($site_url);
    
    echo ("Request: \n". $awis_url."\n\n");
    
    // Make request
    
    $result = make_http_request($awis_url);
    
    // Display resulting XML
    
    echo("Response:\n");
    echo($result);
    
    // Returns the AWS rest url to get AWIS information for the given site
    
    function generate_rest_url($site_url) {
    
            $timestamp =  generate_timestamp();
            $site_enc = urlencode($site_url);
            $timestamp_enc = urlencode($timestamp);
            $signature_enc = urlencode (
                calculate_RFC2104HMAC
                        (SERVICE . OPERATION . $timestamp, SECRET_ACCESS_KEY)
                );
    
            return  "http://awis.amazonaws.com/onca/xml?"
                            . "Service=".SERVICE
                            . "&Operation=".OPERATION
                            . "&AWSAccessKeyId=".ACCESS_KEY
                            . "&ResponseGroup=".RESPONSE_GROUP
                            . "&Timestamp=$timestamp_enc"
                            . "&Signature=$signature_enc"
                            . "&Url=$site_enc";
    
    }
    
    
    // Calculate signature using HMAC: http://www.faqs.org/rfcs/rfc2104.html
    
    function calculate_RFC2104HMAC ($data, $key) {
        return base64_encode (
            pack("H*", sha1((str_pad($key, 64, chr(0x00))
            ^(str_repeat(chr(0x5c), 64))) .
            pack("H*", sha1((str_pad($key, 64, chr(0x00))
            ^(str_repeat(chr(0x36), 64))) . $data))))
         );
    }
    
    // Timestamp format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
    
    function generate_timestamp () {
        return gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
    }
    
    // Make an http request to the specified URL and return the result
    
    function make_http_request($url){
           $ch = curl_init($url);
           curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    	   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
           $result = curl_exec($ch);
           curl_close($ch);
           return $result;
    }
    
    
    ?>
    
    
    PHP:
     
    mad4, Oct 5, 2006 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    I have Alexa rank checker as part of my Site Information tool. It is based on similar code. Functions calculate_RFC2104HMAC and generate_timestamp are the same, but I am getting response like this:
    
      $secret_access_key = 'skipped';
      $aws_access_key_id = 'skipped';
      $service = 'AlexaWebInfoService';
      $operation = 'UrlInfo';
      $timestamp =  generate_timestamp();
      $site_enc = urlencode($url);
      $timestamp_enc = urlencode($timestamp);
      $signature_enc = urlencode(calculate_RFC2104HMAC($service . $operation . $timestamp, $secret_access_key));
    
      $awsurl =  'http://awis.amazonaws.com/onca/xml?'
          . "Service=$service"
          . "&Operation=$operation"
          . "&Timestamp=$timestamp_enc"
          . "&AWSAccessKeyId=$aws_access_key_id"
          . "&ResponseGroup=Rank"
          . "&Url=$site_enc"
          . "&Signature=$signature_enc";
    
      $request = @file_get_contents($awsurl);
      if ($request && preg_match("/<Rank>([0-9]+)<\/Rank>/", $request, $match))
       return $match[1];
    
    
    PHP:
     
    wmtips, Oct 5, 2006 IP
    mad4 likes this.
  3. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Couldn't get your script to work. Think its my server firewall.

    Got it sorted so its working about 50% of the time. :(
     
    mad4, Oct 5, 2006 IP
  4. saneinsight

    saneinsight Guest

    Messages:
    159
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi, do you have to pay to use the Alexa API to get traffic stats? Some where I read on their site it's $0.15 / 1000. If so, is it worth it?
     
    saneinsight, Dec 6, 2006 IP
  5. GuruQuest

    GuruQuest Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #6
    One API of Alexa is available that gives latest rank details of the website.

    It's simple to use with URL.

    <?php
         $domain = "guruquest.net";
         $url =  "http://data.alexa.com/data?cli=10&url=".$domain;
         $response = file_get_contents($url);
         print_r(json_decode(json_encode($response)),1);
    ?>
    PHP:
    http://data.alexa.com/data?cli=10&url=domain_name

    from there reference alexa api php
     
    Last edited by a moderator: Jan 11, 2017
    GuruQuest, Jan 11, 2017 IP