You are here

private function Cleantalk::sendRequest in Anti Spam by CleanTalk 8

Send JSON request to servers

Parameters

$msg:

Return value

boolean|\CleantalkResponse

1 call to Cleantalk::sendRequest()
Cleantalk::httpRequest in src/Cleantalk.php
httpRequest

File

src/Cleantalk.php, line 250

Class

Cleantalk
Cleantalk class create request

Namespace

Drupal\cleantalk

Code

private function sendRequest($data = null, $url, $server_timeout = 3) {

  // Convert to array
  $data = \Drupal\Component\Serialization\Json::decode(\Drupal\Component\Serialization\Json::encode($data));

  // Convert to JSON
  $data = \Drupal\Component\Serialization\Json::encode($data);
  if (isset($this->api_version)) {
    $url = $url . $this->api_version;
  }

  // Switching to secure connection
  if ($this->ssl_on && !preg_match("/^https:/", $url)) {
    $url = preg_replace("/^(http)/i", "\$1s", $url);
  }
  $result = false;
  $curl_error = null;
  if (function_exists('curl_init')) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $server_timeout);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    // receive server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // resolve 'Expect: 100-continue' issue
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Expect:',
    ));

    // see http://stackoverflow.com/a/23322368
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

    // Disabling CA cert verivication
    // Disabling common name verification
    if ($this->ssl_on) {
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    $result = curl_exec($ch);
    if (!$result) {
      $curl_error = curl_error($ch);
    }
    curl_close($ch);
  }
  if (!$result) {
    $allow_url_fopen = ini_get('allow_url_fopen');
    if (function_exists('file_get_contents') && isset($allow_url_fopen) && $allow_url_fopen == '1') {
      $opts = array(
        'http' => array(
          'method' => 'POST',
          'header' => "Content-Type: text/html\r\n",
          'content' => $data,
          'timeout' => $server_timeout,
        ),
      );
      $context = stream_context_create($opts);
      $result = @file_get_contents($url, false, $context);
    }
  }
  if (!$result) {
    $response = null;
    $response['errno'] = 1;
    if ($curl_error) {
      $response['errstr'] = sprintf("CURL error: '%s'", $curl_error);
    }
    else {
      $response['errstr'] = 'No CURL support compiled in';
    }
    $response['errstr'] .= ' or disabled allow_url_fopen in php.ini.';
    $response = (object) \Drupal\Component\Serialization\Json::decode(\Drupal\Component\Serialization\Json::encode($response));
    return $response;
  }
  $errstr = null;
  $response = (object) \Drupal\Component\Serialization\Json::decode($result);
  if ($result !== false && is_object($response)) {
    $response->errno = 0;
    $response->errstr = $errstr;
  }
  else {
    $errstr = 'Unknown response from ' . $url . '.' . ' ' . $result;
    $response = null;
    $response['errno'] = 1;
    $response['errstr'] = $errstr;
    $response = (object) \Drupal\Component\Serialization\Json::decode(\Drupal\Component\Serialization\Json::encode($response));
  }
  return $response;
}