Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 170 lines (137 sloc) 6.125 kb
49d96c1 Add PHP Class
Brian LaFrance authored
1 <?php
2 /**
3 * alPartner - AuthorityLabs Partner API PHP Library
4 *
5 * @category Services
6 * @package AuthorityLabs Partner API PHP Library
7 * @author Jonathan Kressaty <jonathan.kressaty@gmail.com> & Brian LaFrance <brian.lafrance@authoritylabs.com>
8 * @license http://creativecommons.org/licenses/MIT/ MIT
186db41 Update URL
Brian LaFrance authored
9 * @link https://github.com/blafrance/authoritylabs-partner-api-tools
49d96c1 Add PHP Class
Brian LaFrance authored
10 */
11
12 class alPartner
13 {
14 private $api_url = 'http://api.authoritylabs.com/keywords/';
15
16 /**
17 * POST a keyword to the queue
18 *
98ec3d1 Clarify that keywords must be encoded as UTF-8 prior to being posted.
Brian LaFrance authored
19 * @param string $keyword Keyword to query against - this keyword must be passed in encoded as UTF-8
49d96c1 Add PHP Class
Brian LaFrance authored
20 * @param string $auth_token AuthorityLabs Partner API Key
21 * @param string $priority OPTIONAL Defines whether or not to use priority queue. Passing "true" to this will use priority queue
22 * @param string $engine OPTIONAL Search engine to query - see supported engines list at http://authoritylabs.com/api/reference/#engines
23 * @param string $locale OPTIONAL Language/Country code - see supported language/country list at http://authoritylabs.com/api/reference/#countries
24 * @param string $pages_from OPTIONAL Default is false and only works with Google. Defines whether or not to use pages from location results
25 * @param string $lang_only OPTIONAL Default is false and only works with Google. Defines whether or not to use pages in specified language only
26 * @param string $callback OPTIONAL Default is taken from the callback URL that is set in your AuthorityLabs Partner API account. This parameter will override that URL
27 *
28 */
29 public function keywordPost($keyword, $auth_token, $priority="false", $engine="google", $locale="en-US", $pages_from="false", $lang_only="false",$callback=null){
30
31 $path = '';
32
33 $post_variables = array(
a230784 Trim whitespace off keywords that are passed in
Brian LaFrance authored
34 'keyword' => trim($keyword),
49d96c1 Add PHP Class
Brian LaFrance authored
35 'auth_token' => $auth_token,
36 'engine' => $engine,
37 'locale' => $locale,
38 'pages_from' => $pages_from,
39 'lang_only' => $lang_only
40 );
41
42 if($callback!=null){
6c9043d @scottp1956 Update alapi.class.php
scottp1956 authored
43 $post_variables['callback'] = $callback;
49d96c1 Add PHP Class
Brian LaFrance authored
44 }
45
46 if($priority=="true"){
47 $path = 'priority';
48 }
49
50 return $this->alRequest($post_variables, 'POST', $path);
51 }
52
53 /**
54 * GET SERP data for a keyword - returns json or html data of SERP, depending on the specified format
55 *
98ec3d1 Clarify that keywords must be encoded as UTF-8 prior to being posted.
Brian LaFrance authored
56 * @param string $keyword Keyword to get data for - this keyword must be passed in encoded as UTF-8
49d96c1 Add PHP Class
Brian LaFrance authored
57 * @param string $auth_token AuthorityLabs Partner API Key
58 * @param string $rank_date Date to retrieve the SERP for. Must be in YYYY-MM-DD format
59 * @param string $data_format OPTIONAL Define format type to retrieve. Default is json - see supported types at http://authoritylabs.com/api/reference/#formats
60 * @param string $engine OPTIONAL Search engine to query - see supported engines list at http://authoritylabs.com/api/reference/#engines
61 * @param string $locale OPTIONAL Language/Country code - see supported language/country list at http://authoritylabs.com/api/reference/#countries
62 * @param string $pages_from OPTIONAL Default is false and only works with Google. Defines whether or not to use pages from location results
63 * @param string $lang_only OPTIONAL Default is false and only works with Google. Defines whether or not to use pages in specified language only
64 *
65 */
66 public function keywordGet($keyword, $auth_token, $rank_date, $data_format="json", $engine="google", $locale="en-US", $pages_from="false", $lang_only="false"){
67
68 $get_variables = array(
69 'keyword' => $keyword,
70 'auth_token' => $auth_token,
71 'engine' => $engine,
72 'locale' => $locale,
73 'pages_from' => $pages_from,
74 'rank_date' => $rank_date,
75 'data_format' => $data_format,
76 'lang_only' => $lang_only
77 );
78
79 $path = 'get?';
80
81 return $this->alRequest($get_variables, 'GET', $path);
82 }
83
84 /**
85 * GET Ranking for a keyword/domain combo - returns an array with ranking data
86 *
87 * @param string $url URL that we want to track rankings for
88 * @param string $json_data json data retrieved using the keywordGet function
89 *
90 */
91 public function parseRanks($url, $json_data){
92
93 $json_data = json_decode($json_data);
94
95 $url = str_ireplace('http://','',$url);
96
97 if(is_object($json_data)){
98
99 $serp = get_object_vars($json_data->serp);
100 $arr_rankings = array();
101
102 foreach($serp as $key=>$val){
103
104 $match = $val->href;
105
106 if(stristr($match, '.' . $url))
107 $arr_rankings[$key] = $val->href;
108
109 if(stristr($match, '/' . $url))
110 $arr_rankings[$key] = $val->href;
111 }
112 }
113
114 return $arr_rankings;
115 }
116
117 /**
118 * Request using PHP CURL functions
119 * Requires curl library installed and configured for PHP
120 * Returns response from the AuthorityLabs Partner API
121 *
122 * @param array $request_vars Data for making the request to API
123 * @param string $method Specifies POST or GET method
124 * @param string $path OPTIONAL Path for the API request - specifies priority or get URL when applicable
125 *
126 */
127 private function alRequest($request_vars = array(), $method, $path=""){
128
129 $qs = '';
130 $response = '';
131
132 foreach($request_vars AS $key=>$value)
133 $qs .= "$key=". urlencode($value) . '&';
134
135 $qs = substr($qs, 0, -1);
136
137 //construct full api url
138 $url = $this->api_url . $path;
139 if(strtoupper($method) == 'GET')
140 $url .= $qs;
141
142 //initialize a new curl object
143 $ch = curl_init();
144
145 curl_setopt($ch, CURLOPT_URL, $url);
146 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
147 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
148
149 switch(strtoupper($method)) {
150 case "GET":
151 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
152 break;
153 case "POST":
154 curl_setopt($ch, CURLOPT_POST, TRUE);
155 curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
156 break;
157 }
158
159 if(FALSE === ($response = curl_exec($ch)))
160 return "Curl failed with error " . curl_error($ch);
161
162 //if POST, return response code from API
163 if(strtoupper($method) == 'POST')
164 $response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
165
166 curl_close($ch);
167
168 return $response;
169 }
170 }
Something went wrong with that request. Please try again.