You are here

private function HtmlAssetInliner::getUrlContents in TMGMT Translator Smartling 8

Same name and namespace in other branches
  1. 8.4 src/Context/HtmlAssetInliner.php \Drupal\tmgmt_smartling\Context\HtmlAssetInliner::getUrlContents()
  2. 8.2 src/Context/HtmlAssetInliner.php \Drupal\tmgmt_smartling\Context\HtmlAssetInliner::getUrlContents()
  3. 8.3 src/Context/HtmlAssetInliner.php \Drupal\tmgmt_smartling\Context\HtmlAssetInliner::getUrlContents()

Gets content for given url using curl and optionally using user agent

Parameters

$url:

int $timeout:

string $userAgent:

array $settings:

bool $debug:

Return value

int|mixed

1 call to HtmlAssetInliner::getUrlContents()
HtmlAssetInliner::getCompletePage in src/Context/HtmlAssetInliner.php
Gets complete page data and returns generated string

File

src/Context/HtmlAssetInliner.php, line 321

Class

HtmlAssetInliner

Namespace

Drupal\tmgmt_smartling\Context

Code

private function getUrlContents($url, $timeout = 0, $userAgent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10', array $settings, $debug = FALSE) {
  $crl = curl_init();
  if ($debug) {

    // Enable request headers into curl info array.
    curl_setopt($crl, CURLINFO_HEADER_OUT, TRUE);

    // Enable response headers to response.
    curl_setopt($crl, CURLOPT_HEADER, 1);
  }
  if (!empty($settings['context_skip_host_verifying'])) {
    curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, FALSE);
  }
  if (!empty($settings['enable_basic_auth'])) {
    curl_setopt($crl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($crl, CURLOPT_USERPWD, $settings['basic_auth']['login'] . ':' . $settings['basic_auth']['password']);
  }
  curl_setopt($crl, CURLOPT_URL, $url);
  curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);

  # return result as string rather than direct output
  curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, $timeout);

  # set the timeout
  curl_setopt($crl, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($crl, CURLOPT_COOKIE, $this->cookie);
  curl_setopt($crl, CURLOPT_USERAGENT, $userAgent);

  # set our 'user agent'
  curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, FALSE);
  $output = curl_exec($crl);
  if ($debug) {
    $curlInfo = curl_getinfo($crl);
    $header_size = $curlInfo['header_size'];
    $headers = substr($output, 0, $header_size);
    $output = substr($output, $header_size);
    Drupal::logger('tmgmt_smartling_context_debug')
      ->info('Curl request info: @request_info:', [
      '@request_info' => print_r($curlInfo, TRUE),
    ]);
    Drupal::logger('tmgmt_smartling_context_debug')
      ->info('Curl response headers: @response_headers', [
      '@response_headers' => $headers,
    ]);
    Drupal::logger('tmgmt_smartling_context_debug')
      ->info('Curl response body: @response_body', [
      '@response_body' => substr($output, 0, 500) . '*****',
    ]);
  }
  curl_close($crl);
  if (!$output) {
    return -1;
  }
  return $output;
}