You are here

public function HtmlAssetInliner::getUrlContents in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8 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 $user_agent:

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 160

Class

HtmlAssetInliner

Namespace

Drupal\tmgmt_smartling\Context

Code

public function getUrlContents($url, $timeout = 0, $user_agent = '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);
  }
  $this
    ->applySettingsToCurl($settings, $crl);
  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, $user_agent);

  # set our 'user agent'
  curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, FALSE);
  $output = curl_exec($crl);
  if ($debug) {
    $curl_info = curl_getinfo($crl);
    $header_size = $curl_info['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($curl_info, 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;
}