Skip to content

Instantly share code, notes, and snippets.

@scamba
Last active May 7, 2022 01:44
Show Gist options
  • Save scamba/e07ba61e9a812d342cab458a0b2706cb to your computer and use it in GitHub Desktop.
Save scamba/e07ba61e9a812d342cab458a0b2706cb to your computer and use it in GitHub Desktop.
PHP script for get DA from MOZ's API
<?php
/*
-- =========================================================================================
-- Author: Sergio Camba (https://github.com/scamba)
-- Create date: 18/04/2017
-- Description: Function library for MOZ API
-- More info: https://moz.com/products/api
-- =========================================================================================
------------------------------
--> Explanation of functions:
------------------------------
* getDA( $urlToDA )
----------------------->
Description:
- Function prepared to return the Domain Authority (DA) as a rounded integer that measures the MOZ platform.
Parameters:
- $urlToDA: String type URL --> xxx.com, www.xxx.com, etc.
Return:
- A rounded integer corresponding to the DA of that URL according to MOZ.
To consider:
- Due to the limitations of the free account, a delay of 11 seconds between call and call must be put.
- In addition to the DA, $content array, returns other data that can be observed with a print_r($ content); at line 65.
*/
function getDA( $urlToDA ){
// Get your access id and secret key here: https://moz.com/products/api/keys
$accessID = "your-MOZ-accessID";
$secretKey = "your-MOZ-secretKey";
// Set your expires times for several minutes into the future.
// An expires time excessively far in the future will not be honored by the Mozscape API.
$expires = time() + 300;
// Put each parameter on a new line.
$stringToSign = $accessID."\n".$expires;
// Get the "raw" or binary output of the hmac hash.
$binarySignature = hash_hmac('sha1', $stringToSign, $secretKey, true);
// Base64-encode it and then url-encode that.
$urlSafeSignature = urlencode(base64_encode($binarySignature));
// Specify the URL that you want link metrics for.
$objectURL = $urlToDA;
// Add up all the bit flags you want returned.
// Learn more here: https://moz.com/help/guides/moz-api/mozscape/api-reference/url-metrics
$cols = "103079215108";
// Put it all together and you get your request URL.
// 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;
// Use Curl to send off your request.
$options = array(
CURLOPT_RETURNTRANSFER => true
);
$ch = curl_init($requestUrl);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
//print_r($content); // Uncommnet to check full results
$array1 = explode(',', $content);
$array2 = explode(":", $array1[0]);
sleep(11); // We waited 11 seconds (delay required for the Moz API with free account)
return (int)round($array2[1]); // We return the DA as rounded INT
}
?>
@MostafaNorzade
Copy link

Is this API service have a limit?

@scamba
Copy link
Author

scamba commented Aug 13, 2019

This function its a quite old, check it first if works properly. The limit is for time between function calls if you insert on loop.

@MostafaNorzade
Copy link

This function its a quite old, check it first if works properly. The limit is for time between function calls if you insert on loop.

Thanks. for API count have a limit ?

@scamba
Copy link
Author

scamba commented Aug 13, 2019

I dont know mate. Moz has made several changes on his API last months, you should test it first and go back here to explain it to all us. This would be awesome :)

@sophiehf35
Copy link

sophiehf35 commented May 7, 2022

Hello Sergio Camba, I am trying to connect to moz using the newest API version V2, but for some reason it just don´t work... I saw you made this script some time ago... could you look my answer at Stack? https://stackoverflow.com/questions/72122223/trouble-to-use-api-v2-moz-http-request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment