103 |
- |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/***************************************************************************
|
|
|
4 |
* Copyright (C) 2009-2011 by Geo Varghese(www.seopanel.in) *
|
|
|
5 |
* sendtogeo@gmail.com *
|
|
|
6 |
* *
|
|
|
7 |
* This program is free software; you can redistribute it and/or modify *
|
|
|
8 |
* it under the terms of the GNU General Public License as published by *
|
|
|
9 |
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
10 |
* (at your option) any later version. *
|
|
|
11 |
* *
|
|
|
12 |
* This program is distributed in the hope that it will be useful, *
|
|
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
15 |
* GNU General Public License for more details. *
|
|
|
16 |
* *
|
|
|
17 |
* You should have received a copy of the GNU General Public License *
|
|
|
18 |
* along with this program; if not, write to the *
|
|
|
19 |
* Free Software Foundation, Inc., *
|
|
|
20 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
21 |
***************************************************************************/
|
|
|
22 |
|
|
|
23 |
# class defines all moz api controller functions
|
|
|
24 |
class MozController extends Controller{
|
|
|
25 |
|
|
|
26 |
// function to get moz rank
|
|
|
27 |
function __getMozRankInfo ($urlList = array(), $accessID = "", $secretKey = "", $returnLog = false) {
|
|
|
28 |
$mozRankList = array();
|
|
|
29 |
|
|
|
30 |
if (SP_DEMO && !empty($_SERVER['REQUEST_METHOD'])) return $mozRankList;
|
|
|
31 |
|
|
|
32 |
if (empty($urlList)) return $mozRankList;
|
|
|
33 |
|
|
|
34 |
// Get your access id and secret key here: https://moz.com/products/api/keys
|
|
|
35 |
$accessID = !empty($accessID) ? $accessID : SP_MOZ_API_ACCESS_ID;
|
|
|
36 |
$secretKey = !empty($secretKey) ? $secretKey : SP_MOZ_API_SECRET;
|
|
|
37 |
|
|
|
38 |
// if empty no need to crawl
|
|
|
39 |
if (empty($accessID) || empty($secretKey)) {
|
|
|
40 |
$alertCtler = new AlertController();
|
|
|
41 |
$alertInfo = array(
|
|
|
42 |
'alert_subject' => "Click here to enter MOZ API key",
|
|
|
43 |
'alert_message' => "Error: MOZ API key not found",
|
|
|
44 |
'alert_url' => SP_WEBPATH ."/admin-panel.php?sec=moz-settings",
|
|
|
45 |
'alert_type' => "danger",
|
|
|
46 |
'alert_category' => "reports",
|
|
|
47 |
);
|
|
|
48 |
$alertCtler->createAlert($alertInfo, false, true);
|
|
|
49 |
return $mozRankList;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
// Set your expires times for several minutes into the future.
|
|
|
53 |
// An expires time excessively far in the future will not be honored by the Mozscape API.
|
|
|
54 |
$expires = time() + 300;
|
|
|
55 |
|
|
|
56 |
// Put each parameter on a new line.
|
|
|
57 |
$stringToSign = $accessID."\n".$expires;
|
|
|
58 |
|
|
|
59 |
// Get the "raw" or binary output of the hmac hash.
|
|
|
60 |
$binarySignature = hash_hmac('sha1', $stringToSign, $secretKey, true);
|
|
|
61 |
|
|
|
62 |
// Base64-encode it and then url-encode that.
|
|
|
63 |
$urlSafeSignature = urlencode(base64_encode($binarySignature));
|
|
|
64 |
|
|
|
65 |
// Add up all the bit flags you want returned.
|
|
|
66 |
// Learn more here: https://moz.com/help/guides/moz-api/mozscape/api-reference/url-metrics
|
|
|
67 |
$cols = "103079231488";
|
|
|
68 |
|
|
|
69 |
// Put it all together and you get your request URL.
|
|
|
70 |
$requestUrl = SP_MOZ_API_LINK . "/url-metrics/?Cols=".$cols."&AccessID=".$accessID."&Expires=".$expires."&Signature=".$urlSafeSignature;
|
|
|
71 |
|
|
|
72 |
// Put your URLS into an array and json_encode them.
|
|
|
73 |
$encodedDomains = json_encode($urlList);
|
|
|
74 |
|
|
|
75 |
$spider = new Spider();
|
|
|
76 |
$spider->_CURLOPT_POSTFIELDS = $encodedDomains;
|
|
|
77 |
$ret = $spider->getContent($requestUrl);
|
|
|
78 |
|
|
|
79 |
// parse rank from the page
|
|
|
80 |
if (!empty($ret['page'])) {
|
|
|
81 |
$rankList = json_decode($ret['page']);
|
|
|
82 |
|
|
|
83 |
// if no errors occured
|
|
|
84 |
if (empty($rankList->error_message)) {
|
|
|
85 |
|
|
|
86 |
// loop through rank list
|
|
|
87 |
foreach ($rankList as $rankInfo) {
|
|
|
88 |
|
|
|
89 |
$mozRankInfo = array(
|
|
|
90 |
'moz_rank' => round($rankInfo->umrp, 2),
|
|
|
91 |
'domain_authority' => round($rankInfo->pda, 2),
|
|
|
92 |
'page_authority' => round($rankInfo->upa, 2),
|
|
|
93 |
);
|
|
|
94 |
|
|
|
95 |
$mozRankList[] = $mozRankInfo;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
} else {
|
|
|
99 |
$crawlInfo['crawl_status'] = 0;
|
|
|
100 |
$crawlInfo['log_message'] = $rankList->error_message;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
} else {
|
|
|
104 |
$crawlInfo['crawl_status'] = 0;
|
|
|
105 |
$crawlInfo['log_message'] = $ret['errmsg'];
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
// update crawl log
|
|
|
109 |
$crawlLogCtrl = new CrawlLogController();
|
|
|
110 |
$crawlInfo['crawl_type'] = 'rank';
|
|
|
111 |
$crawlInfo['ref_id'] = $encodedDomains;
|
|
|
112 |
$crawlInfo['subject'] = "moz";
|
|
|
113 |
$crawlLogCtrl->updateCrawlLog($ret['log_id'], $crawlInfo);
|
|
|
114 |
|
|
|
115 |
return $returnLog ? array($mozRankList, $crawlInfo) : $mozRankList;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
}
|
|
|
119 |
?>
|