You are here

public static function API::send_request in Anti Spam by CleanTalk 8.3

* Function sends raw request to API server * *

Parameters

array $data to send: * @param string $url of API server * @param integer $timeout timeout in seconds * @param boolean $ssl use ssl on not * * @return array|bool

22 calls to API::send_request()
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 630

Class

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

Namespace

Drupal\cleantalk\lib\Cleantalk\Common

Code

public static function send_request($data, $url = self::URL, $timeout = 10, $ssl = false, $ssl_path = '') {

  // Possibility to switch agent vaersion
  $data['agent'] = !empty($data['agent']) ? $data['agent'] : (defined('CLEANTALK_AGENT') ? CLEANTALK_AGENT : self::AGENT);

  // Make URL string
  $data_string = http_build_query($data);
  $data_string = str_replace("&", "&", $data_string);

  // Possibility to switch API url
  $url = defined('CLEANTALK_API_URL') ? CLEANTALK_API_URL : $url;
  if (function_exists('curl_init')) {
    $ch = curl_init();

    // Set diff options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Expect:',
    ));
    $ssl_path = $ssl_path ? $ssl_path : (defined('CLEANTALK_CASERT_PATH') ? CLEANTALK_CASERT_PATH : '');

    // Switch on/off SSL
    if ($ssl && $ssl_path) {
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
      curl_setopt($ch, CURLOPT_CAINFO, $ssl_path);
    }
    else {
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    }

    // Make a request
    $result = curl_exec($ch);
    $errors = curl_error($ch);
    curl_close($ch);

    // Retry with SSL enabled if failed
    if ($result === false) {
      if ($ssl === false) {
        return self::send_request($data, $url, $timeout, true, $ssl_path);
      }
    }
  }
  else {
    $errors = 'CURL_NOT_INSTALLED';
  }

  // Trying to use file_get_contents() to make a API call
  if (!empty($errors)) {
    if (ini_get('allow_url_fopen')) {
      $opts = array(
        'http' => array(
          'method' => "POST",
          'timeout' => $timeout,
          'content' => $data_string,
        ),
      );
      $context = stream_context_create($opts);
      $result = @file_get_contents($url, 0, $context);
      $errors = $result === false ? $errors . '_FAILED_TO_USE_FILE_GET_CONTENTS' : false;
    }
    else {
      $errors .= '_AND_ALLOW_URL_FOPEN_IS_DISABLED';
    }
  }
  return empty($result) || !empty($errors) ? array(
    'error' => $errors,
  ) : $result;
}