· 5 years ago · Feb 25, 2020, 02:16 PM
1<?php
2
3function getSeomozMetrics($objectURL) {
4 $accessID = "member-047bfe9467";
5 $secretKey = "66d1271282c70439c6121a8311a36d46";
6
7
8 // Set the expiry time for the call.
9 $expires = time() + 600;
10
11 // A new linefeed is necessary between your AccessID and Expires.
12 $stringToSign = $accessID."\n".$expires;
13
14 // Get the "raw" or binary output of the hmac hash.
15 $binarySignature = hash_hmac('sha1', $stringToSign, $secretKey, true);
16
17 // We need to base64-encode it and then url-encode that.
18 $urlSafeSignature = urlencode(base64_encode($binarySignature));
19
20 // Add up all the bit flags you want returned.
21 // Learn more here: http://apiwiki.seomoz.org/categories/api-reference
22 $cols = 68719476736+34359738368+536870912+32768+16384+2048+32+4;
23
24 // Put it all together and you get your request URL.
25 $requestUrl = "http://lsapi.seomoz.com/linkscape/url-metrics/?Cols=".$cols."&AccessID=".$accessID."&Expires=".$expires."&Signature=".$urlSafeSignature;
26
27 // Put your URLS into an array and json_encode them.
28 $batchedDomains = $objectURL;
29 $encodedDomains = json_encode($batchedDomains);
30
31 // We can easily use Curl to send off our request.
32 // Note that we send our encoded list of domains through curl's POSTFIELDS.
33 $options = array(
34 CURLOPT_RETURNTRANSFER => true,
35 CURLOPT_POSTFIELDS => $encodedDomains
36 );
37
38 $ch = curl_init($requestUrl);
39 curl_setopt_array($ch, $options);
40 $content = curl_exec($ch);
41 curl_close( $ch );
42
43 $response = json_decode($content,true);
44
45 $count = 0;
46
47 if (isset($response['error_message'])) {
48 $metric_list = array('error'=>$response['error_message']);
49 } else {
50 // For each URL add the metrics
51 foreach($response as $site_metric) {
52 // Translate the Moz API info into something a bit more understandable
53 $metric_list[$count]['url'] = $objectURL[$count];
54 $metric_list[$count]['subdomain'] = $site_metric['ufq'];
55 $metric_list[$count]['domain'] = $site_metric['upl'];
56 $metric_list[$count]['pa'] = $site_metric['upa'];
57 $metric_list[$count]['da'] = $site_metric['pda'];
58 $metric_list[$count]['mozrank'] = $site_metric['umrp'];
59 $metric_list[$count]['title'] = $site_metric['ut'];
60 $metric_list[$count]['external_links'] = $site_metric['ueid'];
61 $count++;
62 }
63 }
64 // Send back the data
65 return $metric_list;
66}
67
68
69
70?>