Log in

View Full Version : mozapi php integration



ianhaney
10-20-2016, 01:03 PM
Sorry new question, hopefully be the last

I have been asked if mozapi can be integrated into a php page which know it can be done but I have never done any work before involving api so not sure how to go about it

the page it needs to go on has the domains in the database and the mozapi info needs to display in correct columns, the client needs the domain authority, trust flow and citation flow if possible to be pulled in from mozapi

just need pointing in the right direction or a rough start to what the code would look like, I guess I would need to include the db info and query above the api script?

I found the following coding on Google



//MOZ API INFO
$accessID = "API ID HERE"; // * Add unique Access ID
$secretKey = "SECRET KEY HERE"; // * Add unique Secret Key

//Set Expires time in future here 5 minutes.
$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 = $_POST['url'];

// 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 = "103079215104";

// 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
$MozRank = $json_a->umrp;
$theUrl = $json_a->uu;
$title = $json_a->ut;


Just unsure how to get that displaying in the correct columns and if the coding is correct

Thank you in advance

ianhaney
10-20-2016, 02:08 PM
I have found a new script that works but only if the domain name is hardcoded in the script but can't work out how to get the da and pa info for domain names already stored in a mysql database, below is a new script I found



<?php
// access id and secret key from http://www.seomoz.org/api/keys
$accessID = "ACCESS ID";
$secretKey = "SECRET KEY";
//Set Expires time in future here 5 minutes.
$expires = time() + 300;

// Need to put linefeed between your AccessID and Expires time variable.
$SignInStr = $accessID. "\n" .$expires;

// Create your binary Signature from hmac hash.
$binarySignature = hash_hmac('sha1', $SignInStr, $secretKey, true);

// base64-encode signature then urlencode it's output.
$SafeSignature = urlencode(base64_encode($binarySignature));

// Url for which we want url metrics.
$objURL = "http://www.bbc.co.uk";

// sum all the bit flags you want to be returned.
// Find bit flag values visit http://apiwiki.seomoz.org/categories/api-reference
$flags = "103079215108";
// combine everythind create request url and here We use URL Metrics API.
$reqUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/".urlencode($objURL)."?Cols=".$cols."&AccessID=".$accessID."&Expires=".$expires."&Signature=".$SafeSignature;

// send request with php-curl.
$opts = array(
CURLOPT_RETURNTRANSFER => true
);

$curlhandle = curl_init($reqUrl);
curl_setopt_array($curlhandle, $opts);
$content = curl_exec($curlhandle);
curl_close($curlhandle);
$resObj = json_decode($content); //decode the json object and fetch results
echo "Domain Authority : " . $resObj->{'pda'};
echo "<br><br>";
echo "Page Authority : " . $resObj->{'upa'};
?>