You are here

public static function Helper::http__request in Anti Spam by CleanTalk 9.1.x

Same name and namespace in other branches
  1. 8.4 src/lib/Cleantalk/Common/Helper.php \Cleantalk\Common\Helper::http__request()

* Function sends raw http request * * May use 4 presets(combining possible): * get_code - getting only HTTP response code * async - async requests * get - GET-request * ssl - use SSL * * @todo Have to replace this method to the new class like HttpHelper

Parameters

string $url URL: * @param array $data POST|GET indexed array with data to send * @param string|array $presets String or Array with presets: get_code, async, get, ssl, dont_split_to_array * @param array $opts Optional option for CURL connection * * @return array|bool (array || array('error' => true))

3 calls to Helper::http__request()
Helper::http__request__get_content in src/lib/Cleantalk/Common/Helper.php
Wrapper for http_request Requesting data via HTTP request with GET method
Helper::http__request__get_response_code in src/lib/Cleantalk/Common/Helper.php
Wrapper for http_request Requesting HTTP response code for $url
Helper::http__request__rc_to_host in src/lib/Cleantalk/Common/Helper.php
Do the remote call to the host.

File

src/lib/Cleantalk/Common/Helper.php, line 834

Class

Helper
CleanTalk Helper class. Compatible with any CMS.

Namespace

Cleantalk\Common

Code

public static function http__request($url, $data = array(), $presets = null, $opts = array()) {
  if (function_exists('curl_init')) {
    $ch = curl_init();
    if (!empty($data)) {

      // If $data scalar converting it to array
      $data = is_string($data) || is_int($data) ? array(
        $data => 1,
      ) : $data;

      // Build query
      $opts[CURLOPT_POSTFIELDS] = $data;
    }

    // Merging OBLIGATORY options with GIVEN options
    $opts = self::array_merge__save_numeric_keys(array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_CONNECTTIMEOUT_MS => 6000,
      CURLOPT_FORBID_REUSE => true,
      CURLOPT_USERAGENT => self::AGENT . '; ' . (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'UNKNOWN_HOST'),
      CURLOPT_POST => true,
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_SSL_VERIFYHOST => 0,
      CURLOPT_HTTPHEADER => array(
        'Expect:',
      ),
      // Fix for large data and old servers http://php.net/manual/ru/function.curl-setopt.php#82418
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_MAXREDIRS => 5,
    ), $opts);

    // Use presets
    $presets = is_array($presets) ? $presets : explode(' ', $presets);
    foreach ($presets as $preset) {
      switch ($preset) {

        // Do not follow redirects
        case 'dont_follow_redirects':
          $opts[CURLOPT_FOLLOWLOCATION] = false;
          $opts[CURLOPT_MAXREDIRS] = 0;
          break;

        // Get headers only
        case 'get_code':
          $opts[CURLOPT_HEADER] = true;
          $opts[CURLOPT_NOBODY] = true;
          break;

        // Make a request, don't wait for an answer
        case 'async':
          $opts[CURLOPT_CONNECTTIMEOUT_MS] = 1000;
          $opts[CURLOPT_TIMEOUT_MS] = 1000;
          break;
        case 'get':
          $opts[CURLOPT_URL] .= $data ? '?' . str_replace("&", "&", http_build_query($data)) : '';
          $opts[CURLOPT_CUSTOMREQUEST] = 'GET';
          $opts[CURLOPT_POST] = false;
          $opts[CURLOPT_POSTFIELDS] = null;
          break;
        case 'ssl':
          $opts[CURLOPT_SSL_VERIFYPEER] = true;
          $opts[CURLOPT_SSL_VERIFYHOST] = 2;
          if (defined('CLEANTALK_CASERT_PATH') && CLEANTALK_CASERT_PATH) {
            $opts[CURLOPT_CAINFO] = CLEANTALK_CASERT_PATH;
          }
          break;
        default:
          break;
      }
    }
    unset($preset);
    curl_setopt_array($ch, $opts);
    $result = curl_exec($ch);

    // RETURN if async request
    if (in_array('async', $presets)) {
      return true;
    }
    if ($result) {
      if (strpos($result, PHP_EOL) !== false && !in_array('dont_split_to_array', $presets)) {
        $result = explode(PHP_EOL, $result);
      }

      // Get code crossPHP method
      if (in_array('get_code', $presets)) {
        $curl_info = curl_getinfo($ch);
        $result = $curl_info['http_code'];
      }
      curl_close($ch);
      $out = $result;
    }
    else {
      $out = array(
        'error' => curl_error($ch),
      );
    }
  }
  else {
    $out = array(
      'error' => 'CURL_NOT_INSTALLED',
    );
  }

  /**
   * Getting HTTP-response code without cURL
   */
  if ($presets && ($presets == 'get_code' || is_array($presets) && in_array('get_code', $presets)) && isset($out['error']) && $out['error'] == 'CURL_NOT_INSTALLED') {
    $headers = get_headers($url);
    $out = (int) preg_replace('/.*(\\d{3}).*/', '$1', $headers[0]);
  }
  return $out;
}