KnowMyRankings

The KnowMyRankings KnowMyRankings class.

Defined (1)

The class is defined in the following location(s).

/includes/KnowMyRankings.php  
  1. class KnowMyRankings { 
  2.  
  3. private $api_url = "https://api.knowmyrankings.com/v1/"; 
  4. private $api_key; 
  5.  
  6. function __construct( $api_key ) { 
  7.  
  8. $this->api_key = $api_key; 
  9.  
  10.  
  11.  
  12. /** 
  13. * Retrieve API key 
  14. *  
  15. */ 
  16.  
  17. function retrieve_api_key( $args = array() ) { 
  18.  
  19. $url = $this->api_url . "retrieve_api_key"; 
  20.  
  21. $request_args = array( 
  22. 'sslverify' => false,  
  23. 'headers' => array( 
  24. 'Authorization' => 'Basic ' . base64_encode( $args['email'] . ':' . $args['password'] ) 
  25. ); 
  26.  
  27. // Send HTTPS GET 
  28. $server_response = wp_remote_get( $url, $request_args ); 
  29.  
  30. return $this->check_response( $server_response ); 
  31.  
  32.  
  33.  
  34. /** 
  35. * Create user 
  36. *  
  37. */ 
  38.  
  39. function create_user( $args = array() ) { 
  40.  
  41. $url = $this->api_url . "users"; 
  42.  
  43. $request_args = array( 
  44. 'sslverify' => false,  
  45. 'body' => $args 
  46. ); 
  47.  
  48. // Send HTTPS POST 
  49. $server_response = wp_remote_post( $url, $request_args ); 
  50.  
  51. return $this->check_response( $server_response ); 
  52.  
  53.  
  54.  
  55. /** 
  56. * Get URLs 
  57. *  
  58. */ 
  59.  
  60. function get_urls() { 
  61.  
  62. $url = $this->api_url . "urls"; 
  63.  
  64. // Add API key 
  65. $url .= "?api_key=" . $this->api_key;  
  66.  
  67. // Disable SSL check 
  68. $request_args = array ( 
  69. 'sslverify' => false,  
  70. ); 
  71.  
  72. // Send HTTPS GET 
  73. $server_response = wp_remote_get( $url, $request_args ); 
  74.  
  75. return $this->check_response( $server_response ); 
  76.  
  77.  
  78. /** 
  79. * Get URL 
  80. *  
  81. */ 
  82.  
  83. function get_url( $id ) { 
  84.  
  85. $url = $this->api_url . "urls/" . intval ( $id ); 
  86.  
  87. // Add API key 
  88. $url .= "?api_key=" . $this->api_key;  
  89.  
  90. // Disable SSL check 
  91. $request_args = array ( 
  92. 'sslverify' => false,  
  93. ); 
  94.  
  95. // Send HTTPS GET 
  96. $server_response = wp_remote_get( $url, $request_args ); 
  97.  
  98. return $this->check_response( $server_response ); 
  99.  
  100.  
  101. /** 
  102. * Get URL 
  103. *  
  104. */ 
  105.  
  106. function get_keyword( $id ) { 
  107.  
  108. $url = $this->api_url . "keywords/" . intval ( $id ); 
  109.  
  110. // Add API key 
  111. $url .= "?api_key=" . $this->api_key;  
  112.  
  113. // Disable SSL check 
  114. $request_args = array ( 
  115. 'sslverify' => false,  
  116. ); 
  117.  
  118. // Send HTTPS GET 
  119. $server_response = wp_remote_get( $url, $request_args ); 
  120.  
  121. return $this->check_response( $server_response ); 
  122.  
  123.  
  124. /** 
  125. * Add URL 
  126. *  
  127. */ 
  128.  
  129. function add_url( $args = array() ) { 
  130.  
  131. // Add APi key 
  132. $args['api_key'] = $this->api_key; 
  133.  
  134. // Build query URL 
  135. $url = $this->api_url . "urls"; 
  136. $url .= "?" . http_build_query( $args ); 
  137.  
  138. $request_args = array( 
  139. 'sslverify' => false 
  140. ); 
  141.  
  142. // Send HTTPS POST 
  143. $server_response = wp_remote_post( $url, $request_args ); 
  144.  
  145. return $this->check_response( $server_response ); 
  146.  
  147. /** 
  148. * Add URL 
  149. *  
  150. */ 
  151.  
  152. function check_price( $args = array() ) { 
  153.  
  154. // Add APi key 
  155. $args['api_key'] = $this->api_key; 
  156.  
  157. // Build query URL 
  158. $url = $this->api_url . "urls/check_price"; 
  159. $url .= "?" . http_build_query( $args ); 
  160.  
  161. $request_args = array( 
  162. 'sslverify' => false 
  163. ); 
  164.  
  165. // Send HTTPS POST 
  166. $server_response = wp_remote_post( $url, $request_args ); 
  167.  
  168. return $this->check_response( $server_response ); 
  169.  
  170. /** 
  171. * Delete URL 
  172. *  
  173. */ 
  174.  
  175. function delete_url( $id ) { 
  176.  
  177. $url = $this->api_url . "urls/" . intval ( $id ); 
  178.  
  179. // Add API key 
  180. $url .= "?api_key=" . $this->api_key;  
  181.  
  182. // Disable SSL check 
  183. $request_args = array ( 
  184. 'method' => "DELETE",  
  185. 'sslverify' => false,  
  186. ); 
  187.  
  188. // Send HTTPS GET 
  189. $server_response = wp_remote_post( $url, $request_args ); 
  190.  
  191. return $this->check_response( $server_response ); 
  192.  
  193.  
  194. /** 
  195. * Delete keyword 
  196. *  
  197. */ 
  198.  
  199. function delete_keyword( $id ) { 
  200.  
  201. $url = $this->api_url . "keywords/" . intval ( $id ); 
  202.  
  203. // Add API key 
  204. $url .= "?api_key=" . $this->api_key;  
  205.  
  206. // Disable SSL check 
  207. $request_args = array ( 
  208. 'method' => "DELETE",  
  209. 'sslverify' => false,  
  210. ); 
  211.  
  212. // Send HTTPS GET 
  213. $server_response = wp_remote_post( $url, $request_args ); 
  214.  
  215. return $this->check_response( $server_response ); 
  216.  
  217.  
  218. /** 
  219. * Check server response 
  220. *  
  221. */ 
  222.  
  223. function check_response( $server_response ) { 
  224.  
  225. // WP HTTP API Error 
  226. if ( is_wp_error( $server_response ) ) { 
  227.  
  228. return array(  
  229. 'status' => "ERR",  
  230. 'body' => 'WP REMOTE POST ERROR: ' . $server_response->get_error_message()  
  231. ); 
  232. // We got response from KMR API server 
  233. else { 
  234. // A little bit confusing 
  235. $status = $server_response['response']; 
  236. $response = $server_response['body']; 
  237.  
  238. // HTTP Basic Auth failed 
  239. if ( $status['code'] == "401" ) { 
  240. return array(  
  241. 'status' => "ERR",  
  242. 'body' => "Authorization failed" 
  243. ); 
  244.  
  245. // Parse KMR server response errors like 400 or 404 
  246. if ( $status['code'] != "200" ) { 
  247.  
  248. return array(  
  249. 'status' => "ERR",  
  250. 'body' => "KnowMyRankings Server respond: (" . $status['code'] . ") " . $status['message'] 
  251. ); 
  252.  
  253. // JSON to array 
  254. $response = $this->json2arr( $response ); 
  255.  
  256. // Response JSON error 
  257. if ( $response['status'] != "OK" ) { 
  258. return array(  
  259. 'status' => "ERR",  
  260. 'body' => $response['body'] 
  261. ); 
  262.  
  263. // KMR API errors 
  264. if ( isset( $response['body']['errors'] ) ) { 
  265.  
  266. $error = array(  
  267. 'status' => "ERR",  
  268. 'body' => $response['body']['errors'] 
  269. ); 
  270.  
  271. return $error; 
  272.  
  273. // Success 
  274. else { 
  275. return array(  
  276. 'status' => "OK",  
  277. 'body' => $response['body'] 
  278. ); 
  279.  
  280.  
  281.  
  282. /** 
  283. * JSON to array 
  284. *  
  285. */ 
  286.  
  287. function json2arr ( $json ) { 
  288.  
  289. // JSON to array 
  290. if ( is_string( $json ) ) 
  291. $arr = json_decode( $json, true ); 
  292. else 
  293. return array(  
  294. 'status' => "ERR",  
  295. 'body' => "Server response error: response expect to be string"  
  296. ); 
  297.  
  298. // Response JSON error 
  299. if ( ! $arr ) { 
  300. return array(  
  301. 'status' => "ERR",  
  302. 'body' => "Server response error: wrong JSON format"  
  303. ); 
  304. else { 
  305. return array(  
  306. 'status' => "OK",  
  307. 'body' => $arr  
  308. ); 
  309.  
  310.