Mozapi integration

I found a new script that works if the domains are hardcoded into the script but need the da and pa to displays for domain names that are already in a mysql database, I did try adding the db info and query into the coding above the api keys but did not work, the coding I found is below

<?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'};
	?>

i don’t see any interaction with a database.

Sorry have been playing around with it to try and get it working and now have the following script

<?php
// connect to the database
include('connect-db.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 = $_GET['url'];
	$domainnames = 'url';  // list of expiring URLs
	
  // 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($domainnames)."?Cols=".$flags."&AccessID=".$accessID."&Expires=".$expires."&Signature=".$SafeSignature;
	
 // send request with php-curl.
	$opts = array(
	    CURLOPT_RETURNTRANSFER => true
	    );

// get the records from the database
if ($result = $mysqli->query("SELECT id, category, url, DATE_FORMAT(domain_expiry_date, '%d/%m/%Y') AS domain_expiry_date, site_login_url, site_username_login, site_password_login, no_of_posts, tf, ct, domain_name_owner, DATE_FORMAT(domain_owner_dob, '%d/%m/%Y') AS domain_owner_dob, domain_owner_address, domain_owner_email, domain_owner_phone, email_account, email_account_url, email_account_address, email_username, email_password, registrar, registrar_url, registrar_username, registrar_password, hosting_company, hosting_url, hosting_username, hosting_password, DATE_FORMAT(hosting_expiry_date, '%d/%m/%Y') AS hosting_expiry_date, hosting_cpanel_url, hosting_cpanel_username, hosting_cpanel_password, sites_linked_out_to, DATE_FORMAT(last_post_date, '%d/%m/%Y') AS last_post_date, indexed FROM websites ORDER BY $field $sort"))

// display records in a table
echo "<table class='records'>";

$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

while ($row = $result->fetch_object())
echo "<tr>";
echo "<td>" . $resObj->{'pda'} . "</td>";
echo "</tr>";
}

echo "</table>";

it seems to be displaying a 1 in the DA column but not sure where it’s coming from?

  1. your code has at least one syntax error for a missing curly bracket.
  2. also your result wont change while you get your database entries from $result but only referencing $resObj in the loop.

sorry thought would be better to paste my whole code but thought would only post the coding related to the issue

<?php
// connect to the database
include('connect-db.php');

$field='id';
$sort='ASC';
if(isset($_GET['sorting']))
{
  if($_GET['sorting']=='ASC')
  {
  $sort='DESC';
  }
  else
  {
    $sort='ASC';
  }
}
if($_GET['field']=='id')
{
   $field = "id"; 
}
elseif($_GET['field']=='category')
{
   $field = "category";
}

//  access id and secret key from http://www.seomoz.org/api/keys
	$accessID = "";
	$secretKey = "";
	//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 = $_GET['url'];
	$domainnames = 'url';  // list of expiring URLs
	
  // 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($domainnames)."?Cols=".$flags."&AccessID=".$accessID."&Expires=".$expires."&Signature=".$SafeSignature;
	
 // send request with php-curl.
	$opts = array(
	    CURLOPT_RETURNTRANSFER => true
	    );

// get the records from the database
if ($result = $mysqli->query("SELECT id, category, url, DATE_FORMAT(domain_expiry_date, '%d/%m/%Y') AS domain_expiry_date, site_login_url, site_username_login, site_password_login, no_of_posts, tf, ct, domain_name_owner, DATE_FORMAT(domain_owner_dob, '%d/%m/%Y') AS domain_owner_dob, domain_owner_address, domain_owner_email, domain_owner_phone, email_account, email_account_url, email_account_address, email_username, email_password, registrar, registrar_url, registrar_username, registrar_password, hosting_company, hosting_url, hosting_username, hosting_password, DATE_FORMAT(hosting_expiry_date, '%d/%m/%Y') AS hosting_expiry_date, hosting_cpanel_url, hosting_cpanel_username, hosting_cpanel_password, sites_linked_out_to, DATE_FORMAT(last_post_date, '%d/%m/%Y') AS last_post_date, indexed FROM websites ORDER BY $field $sort"))

{
// display records if there are records to display
if ($result->num_rows > 0)
{
	
// display records in a table
echo "<table class='records'>";

// set table headers
echo '<tr>
<th><a href="view-all-websites.php?sorting='.$sort.'&field=id">Website ID</a></th>
<th><a href="view-all-websites.php?sorting='.$sort.'&field=category">Category</a></th>
<th>URL</th>
<th>DA</th>
<th>TF</th>
<th>CT</th>
<th>No Of Posts</th>
<th>Last Post Date</th>
<th>Indexed</th>
<th colspan="2">Actions</th>
</tr>';

$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

while ($row = $result->fetch_object())
{	
// set up a row for each record
echo "<tr>";
echo "<td><a href='view-specific-website-data.php?id=" . $row->id . "' class='button'>View More Info</a></td>";
echo "<td>" . $row->category . "</td>";
echo "<td><a href='http://" . $row->url . "' target='_blank'>" . $row->url . "</a></td>";
echo "<td>" . $resObj->{'pda'} . "</td>";
echo "<td>" . $row->tf . "</td>";
echo "<td>" . $row->ct . "</td>";
echo "<td>" . $row->no_of_posts . "</td>";
echo "<td>" . $row->last_post_date . "</td>";
echo "<td><img src='images/" . (($row->indexed == 'Yes') ? 'green-check.png' : 'red-cross.png') . "' style='width: 30px; height: 30px;'></td>";
echo "<td><a href='add-update-website-data.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
echo "</tr>";
}

echo "</table>";

// retrieve and show the data :)

}

// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}

}

// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}

// close database connection
$mysqli->close();

?>

I did count all the curly brackets and was all closed, is the issue regarding the $result and $resObj you mentioned?

can you give more info on what you want to get? result vs. expected result

if you want to achieve different results for $resObj->{'pda'} it seems you have to query the webservice for every row seperate, so within your loop.

I want to be able to get the domain authority, trust flow and citation flow data from moz api and display in the three columns, I have uploaded a screenshot if it helps

Hi

Bit of a update

I have the script working if a user enters a url into a input field but still can’t work out how to get the metrics into the column as shown in the screenshot I provided bit earlier

there must be a way somehow to get the metrics for the urls that are in the mysql database, below is the newer script I have

<?php
// connect to the database
include('connect-db.php');

$field='id';
$sort='ASC';
if(isset($_GET['sorting']))
{
  if($_GET['sorting']=='ASC')
  {
  $sort='DESC';
  }
  else
  {
    $sort='ASC';
  }
}
if($_GET['field']=='id')
{
   $field = "id"; 
}
elseif($_GET['field']=='category')
{
   $field = "category";
}

// get the records from the database
if ($result = $mysqli->query("SELECT id, category, url, DATE_FORMAT(domain_expiry_date, '%d/%m/%Y') AS domain_expiry_date, site_login_url, site_username_login, site_password_login, no_of_posts, tf, ct, domain_name_owner, DATE_FORMAT(domain_owner_dob, '%d/%m/%Y') AS domain_owner_dob, domain_owner_address, domain_owner_email, domain_owner_phone, email_account, email_account_url, email_account_address, email_username, email_password, registrar, registrar_url, registrar_username, registrar_password, hosting_company, hosting_url, hosting_username, hosting_password, DATE_FORMAT(hosting_expiry_date, '%d/%m/%Y') AS hosting_expiry_date, hosting_cpanel_url, hosting_cpanel_username, hosting_cpanel_password, sites_linked_out_to, DATE_FORMAT(last_post_date, '%d/%m/%Y') AS last_post_date, indexed FROM websites ORDER BY $field $sort"))

{
// display records if there are records to display
if ($result->num_rows > 0)
{
	
	// you can obtain you access id and secret key here: http://www.seomoz.org/api/keys
$accessID = ""; // * Add unique Access ID
$secretKey = ""; // * Add unique Secret Key
 
// Set your expires for several minutes into the future.
// Values excessively far in the future will not be honored by the Mozscape API.
$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: http://apiwiki.seomoz.org/categories/api-reference
$cols = "103079215140";
 
// 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->ueid;
$theUrl = $json_a->uu;
	
// display records in a table
echo "<table class='records'>";

// set table headers
echo '<tr>
<th><a href="view-all-websites.php?sorting='.$sort.'&field=id">Website ID</a></th>
<th><a href="view-all-websites.php?sorting='.$sort.'&field=category">Category</a></th>
<th>URL</th>
<th>DA</th>
<th>TF</th>
<th>CT</th>
<th>No Of Posts</th>
<th>Last Post Date</th>
<th>Indexed</th>
<th colspan="2">Actions</th>
</tr>';

while ($row = $result->fetch_object())
{	
// set up a row for each record
echo "<tr>";
echo "<td><a href='view-specific-website-data.php?id=" . $row->id . "' class='button'>View More Info</a></td>";
echo "<td>" . $row->category . "</td>";
echo "<td><a href='http://" . $row->url . "' target='_blank'>" . $row->url . "</a></td>";
echo "<td" . $domainAuthority . "</td>";
echo "<td>" . $row->tf . "</td>";
echo "<td>" . $row->ct . "</td>";
echo "<td>" . $row->no_of_posts . "</td>";
echo "<td>" . $row->last_post_date . "</td>";
echo "<td><img src='images/" . (($row->indexed == 'Yes') ? 'green-check.png' : 'red-cross.png') . "' style='width: 30px; height: 30px;'></td>";
echo "<td><a href='add-update-website-data.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
echo "</tr>";
}

echo "</table>";

// retrieve and show the data :)

}

// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}

}

// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}

// close database connection
$mysqli->close();

?>

I had a thought, is it possible to create a form button on each row I have on my php page and if clicked on, it will get the domain authority from moz api for just that record and insert it into the mysql database and then output it in the column?

i still don’t see how the data from the webservice is related to the data from your database, you are using the webservice over and over again without a change. but at least: yes, do it.

sorry lost me, what do you mean webservice, is that the api you mean?

in this context its the same, yes.

ok but am totally lost on how to get the domain authority number displayed for urls in my database using the api

so the question is: how do you get this information for a single url? maybe its by changing the $domainnames part in your code - the documentation for your API will tell you. so that will be what you need to change for every record from your database while you are looping through it, like

function getAuthority($domainnames){ /** your cURL stuff here **/ }

I looked at the documentation and did not find it that useful as could not find a answer on how they get the info for a single url

is wrong as not 100% sure what I am doing but have the following and is showing no errors but not got a clue how to get the domain authority show for each url in my database show on the php page

function getAuthority($objectURL){ 

// you can obtain you access id and secret key here: http://www.seomoz.org/api/keys
$accessID = ""; // * Add unique Access ID
$secretKey = ""; // * Add unique Secret Key
 
// Set your expires for several minutes into the future.
// Values excessively far in the future will not be honored by the Mozscape API.
$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 = 'url';
 
// Add up all the bit flags you want returned.
// Learn more here: http://apiwiki.seomoz.org/categories/api-reference
$cols = "103079215140";
 
// 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->ueid;
$theUrl = $json_a->uu;

}

$domainAuthority = '';

echo  "<td>" . $domainAuthority . "</td>";

these answers exclude each other.

for the last code: respect scope. variables within functions can only be accessed via arguments and return (never use global!).

function getAuthority($objectURL){ /* cURL stuff */ return $domainAuthority; }

I am guessing that they get the info for the single url from the following line

$requestUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/".urlencode($objectURL)."?Cols=".$cols."&AccessID=".$accessID."&Expires=".$expires."&Signature=".$urlSafeSignature;

I got the following which is no doubt wrong as can’t get my head around api as not done it before but got no errors

<?php
// connect to the database
include('connect-db.php');
 
function getAuthority($objectURL){ 

// you can obtain you access id and secret key here: http://www.seomoz.org/api/keys
$accessID = ""; // * Add unique Access ID
$secretKey = ""; // * Add unique Secret Key
 
// Set your expires for several minutes into the future.
// Values excessively far in the future will not be honored by the Mozscape API.
$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 = 'url';
 
// Add up all the bit flags you want returned.
// Learn more here: http://apiwiki.seomoz.org/categories/api-reference
$cols = "103079215140";
 
// 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->ueid;
$theUrl = $json_a->uu;

return $domainAuthority;

}

$field='id';
$sort='ASC';
if(isset($_GET['sorting']))
{
  if($_GET['sorting']=='ASC')
  {
  $sort='DESC';
  }
  else
  {
    $sort='ASC';
  }
}
if($_GET['field']=='id')
{
   $field = "id"; 
}
elseif($_GET['field']=='category')
{
   $field = "category";
}

$domainAuthority = '';

// get the records from the database
if ($result = $mysqli->query("SELECT id, category, url, DATE_FORMAT(domain_expiry_date, '%d/%m/%Y') AS domain_expiry_date, site_login_url, site_username_login, site_password_login, no_of_posts, tf, cf, domain_name_owner, DATE_FORMAT(domain_owner_dob, '%d/%m/%Y') AS domain_owner_dob, domain_owner_address, domain_owner_email, domain_owner_phone, email_account, email_account_url, email_account_address, email_username, email_password, registrar, registrar_url, registrar_username, registrar_password, hosting_company, hosting_url, hosting_username, hosting_password, DATE_FORMAT(hosting_expiry_date, '%d/%m/%Y') AS hosting_expiry_date, hosting_cpanel_url, hosting_cpanel_username, hosting_cpanel_password, sites_linked_out_to, DATE_FORMAT(last_post_date, '%d/%m/%Y') AS last_post_date, indexed FROM websites ORDER BY $field $sort"))

{
// display records if there are records to display
if ($result->num_rows > 0)
{
	
// display records in a table
echo "<table class='records'>";

// set table headers
echo '<tr>
<th><a href="view-all-websites.php?sorting='.$sort.'&field=id">Website ID</a></th>
<th><a href="view-all-websites.php?sorting='.$sort.'&field=category">Category</a></th>
<th>URL</th>
<th>DA</th>
<th>TF</th>
<th>CF</th>
<th>No Of Posts</th>
<th>Last Post Date</th>
<th>Indexed</th>
<th colspan="2">Actions</th>
</tr>';

while ($row = $result->fetch_object())
{	
// set up a row for each record
echo "<tr>";
echo "<td><a href='view-specific-website-data.php?id=" . $row->id . "' class='button'>View More Info</a></td>";
echo "<td>" . $row->category . "</td>";
echo "<td><a href='http://" . $row->url . "' target='_blank'>" . $row->url . "</a></td>";
echo  "<td>" . $domainAuthority . "</td>";
echo "<td>" . $row->tf . "</td>";
echo "<td>" . $row->cf . "</td>";
echo "<td>" . $row->no_of_posts . "</td>";
echo "<td>" . $row->last_post_date . "</td>";
echo "<td><img src='images/" . (($row->indexed == 'Yes') ? 'green-check.png' : 'red-cross.png') . "' style='width: 30px; height: 30px;'></td>";
echo "<td><a href='add-update-website-data.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
echo "</tr>";
}

echo "</table>";

// retrieve and show the data :)

}

// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}

}

// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}

// close database connection
$mysqli->close();

?>

so how do you know if its even possible to get authorities for different URLs? where did you even got the API definition from? where did that cURL code come from? at least you have to try different values, so different URLs.

I don’t know to be honest, I have done any work involving api before and integrating it into a php page and pulling info from a db for it to return api moz seo data

the curl code came from moz sample coding

I think I am better finding a developer to do it for me as I have not got a clue about all this