Moz API PHP connection

Im currently developing some new reporting functionality and as part of this I need to pull certain data from moz.com 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

 

<?php
// Obtain your access id and secret key here: http://www.seomoz.org/api/keys
$accessID = “member-XXXXXXXX”;
$secretKey = “XXXXXXXXXXXXXXXXXXX”;

// 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 = “www.website.co.uk”;

// Add up all the bit flags you want returned.
// Learn more here: http://apiwiki.seomoz.org/categories/api-reference
$cols = “81604388192”;

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

// We can easily use Curl to send off our request.
$options = array(
CURLOPT_RETURNTRANSFER => true
);

$ch = curl_init($requestUrl);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($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;

 
?>
<html>
<head>
<title>Moz API Feed for reporting</title>
</head>
<body>
<h1>Moz API Feed for reporting</h1>

<ul>
<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>
</ul></body></html>

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.