You are here

public static function CleantalkHelper::api_send_request in Anti Spam by CleanTalk 7.4

Same name and namespace in other branches
  1. 7.5 src/CleantalkHelper.php \CleantalkHelper::api_send_request()

* Function sends raw request to API server * *

Parameters

string url of API server: * @param array data to send * @param boolean is data have to be JSON encoded or not * @param integer connect timeout * @return type

10 calls to CleantalkHelper::api_send_request()
CleantalkHelper::api_method_send_empty_feedback in src/CleantalkHelper.php
* Function sends empty feedback for version comparison in Dashboard * *
CleantalkHelper::api_method__get_2s_blacklists_db in src/CleantalkHelper.php
CleantalkHelper::api_method__get_account_status in src/CleantalkHelper.php
* Function gets information about account * *
CleantalkHelper::api_method__get_antispam_report in src/CleantalkHelper.php
* Function gets spam report * *
CleantalkHelper::api_method__get_antispam_report_breif in src/CleantalkHelper.php
* Function gets spam statistics * *

... See full list

File

src/CleantalkHelper.php, line 502

Class

CleantalkHelper
Cleantalk's hepler class

Code

public static function api_send_request($data, $url = self::URL, $isJSON = false, $timeout = 3, $ssl = false) {
  $result = null;
  $curl_error = false;
  $original_data = $data;
  if (!$isJSON) {
    $data = http_build_query($data);
    $data = str_replace("&", "&", $data);
  }
  else {
    $data = json_encode($data);
  }
  if (function_exists('curl_init') && function_exists('json_decode')) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Expect:',
    ));
    if ($ssl === true) {
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    }
    else {
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    }
    $result = curl_exec($ch);
    if ($result === false) {
      if ($ssl === false) {
        return self::api_send_request($original_data, $url, $isJSON, $timeout, true);
      }
      $curl_error = curl_error($ch);
    }
    curl_close($ch);
  }
  else {
    $curl_error = 'CURL_NOT_INSTALLED';
  }
  if ($curl_error) {
    $opts = array(
      'http' => array(
        'method' => "POST",
        'timeout' => $timeout,
        'content' => $data,
      ),
    );
    $context = stream_context_create($opts);
    $result = @file_get_contents($url, 0, $context);
  }
  if (!$result && $curl_error) {
    return json_encode(array(
      'error' => true,
      'error_string' => $curl_error,
    ));
  }
  return $result;
}