Moz API PHP connection

Im currently developing some new reporting functionality and as part of this I need to pull certain data from a quick look at there API and its fairly easy to do this using PHP.

First of all you need to have a look at there API reference you particularly need to work out with flag bits you use and then add them up.

I then used this code to connect to the API and return the data I wanted


// Obtain your access id and secret key here:
$accessID = “member-XXXXXXXX”;

// Set your expires for five minutes into the future.
$expires = time() + 300;

// A new linefeed is necessary between your AccessID and Expires.
$stringToSign = $accessID.”\n”.$expires;

// Get the “raw” or binary output of the hmac hash.
$binarySignature = hash_hmac(‘sha1’, $stringToSign, $secretKey, true);

// We need to base64-encode it and then url-encode that.
$urlSafeSignature = urlencode(base64_encode($binarySignature));

// This is the URL that we want link metrics for.
$objectURL = “”;

// Add up all the bit flags you want returned.
// Learn more here:
$cols = “81604388192”;

// Now put your entire request together.
// This example uses the Mozscape URL Metrics API.
$requestUrl = “”.urlencode($objectURL).”?Cols=”.$cols.”&AccessID=”.$accessID.”&Expires=”.$expires.”&Signature=”.$urlSafeSignature;

// We can easily use Curl to send off our request.
$options = array(

$ch = curl_init($requestUrl);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
// * Store URL metrics in array

$json_a = json_decode($content);

// * Assign URL metrics to separate variables

$pageAuthority = round($json_a->upa,0); // * Use the round() function to return integer
$domainAuthority = round($json_a->pda,0);
$externalLinks = $json_a->puid;
$subLinks = $json_a->fuid;
$equityLinks = $json_a->ujid;
$ExtequityLinks = $json_a->ueid;
$SubequityLinks = $json_a->feid;
$rootdomainsLinks = $json_a->uipl;
$rootLinks = $json_a->pid;
$theUrl = $json_a->uu;

<title>Moz API Feed for reporting</title>
<h1>Moz API Feed for reporting</h1>

<li><strong>Domain Authority:</strong> <?php echo $domainAuthority; ?></li>
<li><strong>Links to Root Domain:</strong> <?php echo $externalLinks; ?></li>
<li><strong>Links to Subdomain:</strong> <?php echo $subLinks; ?></li>
<li><strong>Equity Links:</strong> <?php echo $equityLinks; ?></li>
<li><strong>External Equity Links:</strong> <?php echo $ExtequityLinks; ?></li>
<li><strong>Subdomain External Links:</strong> <?php echo $SubequityLinks; ?></li>
<li><strong>Root Domains Linking:</strong> <?php echo $rootdomainsLinks; ?></li>
<li><strong>Root Domain, Root Domains Linking:</strong> <?php echo $rootLinks; ?></li>

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notice: ob_end_flush(): failed to send buffer of zlib output compression (0) in /home/wwwwebam/public_html/blog/wp-includes/functions.php on line 4339