You are here

public static function API::check_response in Anti Spam by CleanTalk 8.4

Same name and namespace in other branches
  1. 9.1.x src/lib/Cleantalk/Common/API.php \Cleantalk\Common\API::check_response()

* Function checks server response * *

Parameters

array|string $result: * @param string $method_name * * @return mixed (array || array('error' => true))

22 calls to API::check_response()
API::method__backlinks_check_cms in src/lib/Cleantalk/Common/API.php
* Wrapper for get_antispam_report API method. * Function gets spam domains report. * *
API::method__get_2s_blacklists_db in src/lib/Cleantalk/Common/API.php
* Wrapper for 2s_blacklists_db API method. * Gets data for SpamFireWall. * *
API::method__get_antispam_report in src/lib/Cleantalk/Common/API.php
* Wrapper for get_antispam_report API method. * Gets spam report. * *
API::method__get_antispam_report_breif in src/lib/Cleantalk/Common/API.php
* Wrapper for get_antispam_report_breif API method. * Ggets spam statistics. * *
API::method__get_api_key in src/lib/Cleantalk/Common/API.php
* Wrapper for get_api_key API method. * Gets access key automatically. * *

... See full list

File

src/lib/Cleantalk/Common/API.php, line 725

Class

API
CleanTalk API class. Mostly contains wrappers for API methods. Check and send mehods. Compatible with any CMS.

Namespace

Cleantalk\Common

Code

public static function check_response($result, $method_name = null) {

  // Errors handling
  // Bad connection
  if (is_array($result) && isset($result['error'])) {
    $last = error_get_last();
    $out = !empty($result['error']) ? array(
      'error' => 'CONNECTION_ERROR : "' . $result['error'] . '"',
    ) : array(
      'error' => 'CONNECTION_ERROR : "Unknown Error. Last error: ' . $last['message'],
    );
    return $out;
  }

  // JSON decode errors
  $result = json_decode($result, true);
  if (empty($result)) {
    return array(
      'error' => 'JSON_DECODE_ERROR',
    );
  }

  // Server errors
  if ($result && isset($result['error_no'], $result['error_message'])) {
    if ($result['error_no'] != 12) {
      return array(
        'error' => "SERVER_ERROR NO: {$result['error_no']} MSG: {$result['error_message']}",
        'error_no' => $result['error_no'],
        'error_message' => $result['error_message'],
      );
    }
  }

  // Pathces for different methods
  switch ($method_name) {

    // notice_paid_till
    case 'notice_paid_till':
      $result = isset($result['data']) ? $result['data'] : $result;
      if (isset($result['error_no']) && $result['error_no'] == 12 || !(isset($result['service_id']) && is_int($result['service_id'])) && empty($result['moderate_ip'])) {
        $result['valid'] = 0;
      }
      else {
        $result['valid'] = 1;
      }
      return $result;
      break;

    // get_antispam_report_breif
    case 'get_antispam_report_breif':
      $out = isset($result['data']) && is_array($result['data']) ? $result['data'] : array(
        'error' => 'NO_DATA',
      );
      for ($tmp = array(), $i = 0; $i < 7; $i++) {
        $tmp[date('Y-m-d', time() - 86400 * 7 + 86400 * $i)] = 0;
      }
      $out['spam_stat'] = (array) array_merge($tmp, isset($out['spam_stat']) ? $out['spam_stat'] : array());
      $out['top5_spam_ip'] = isset($out['top5_spam_ip']) ? $out['top5_spam_ip'] : array();
      return $out;
      break;
    default:
      return isset($result['data']) && is_array($result['data']) ? $result['data'] : array(
        'error' => 'NO_DATA',
      );
      break;
  }
}