php: php script to get Alexa global rank for free

Trabla: php: simple php script to get Alexa global rank


php script to get Alexa global rank - tutorial screenshot 1

Alexa Internet, Inc. is a California-based company that provides commercial web traffic data and analytics. It is a wholly owned subsidiary of Amazon.com.

Founded as an independent company in 1996, Alexa was acquired by Amazon in 1999. Its toolbar collects data on browsing behavior and transmits them to the Alexa website, where they are stored and analyzed, forming the basis for the company's web traffic reporting. According to its website, Alexa provides traffic data, global rankings and other information on 30 million websites, and as of 2015 its website is visited by over 6.5 million people monthly.
https://en.wikipedia.org/wiki/Alexa_Internet

Alexa web traffic metrics are available via  API at http://aws.amazon.com/awis
and is paid service:

Pricing
Up to 1,000 requests/ month - Free
1,001 to 1,000,000 requests/ month -  $0.00045 per request
Over 1,000,000 requests/ month - $0.00030 per request

This example shows how to get alexa global rank for free, by parsing free page data.

Solving:

This is simple php script to get Alexa global rank value for free

<?php

/**
* Return Alexa global rank value or empty string (if site is not ranked )
*
* Example of $siteUrl value - google.com, myblog.blogspot.com
*
* @param string $siteUrl - url of target site to explore
*
*/

function getAlexaGlobalRank(  $siteUrl )
{

    $alexaUrl = "http://www.alexa.com/siteinfo/";

    $url =  $alexaUrl . $siteUrl;

    // Get html Alexa rank page content
    $file = file_get_contents($url);

    $doc = new DOMDocument();
    // Parse html page content and supress warnings
    @$doc->loadHTML( $file );

    $xpath = new DOMXpath($doc);

    // Find DOM element with global rank data
    $element = $xpath->query('//*[@id="traffic-rank-content"]/div/span[2]/div[1]/span/span/div/strong/text()');

    // Get Alexa global rank number
    $alexaGlobalRank = $element->item(1)->data;

    return $alexaGlobalRank;
}

// Using example
$targetSite = "stackoverflow.com";
echo getAlexaGlobalRank( $targetSite );

?>


Result:

Php Script execution result: global alexa rank of stackoverflow.com is 59

php script to get Alexa global rank - tutorial screenshot 2
Now check on site data


php script to get Alexa global rank - tutorial screenshot 3
Hooray!!! :)

No comments:

Post a Comment