You are here

function cf_http_split_response in Common Functionality 7

Same name and namespace in other branches
  1. 7.2 modules/cf_http/cf_http.module \cf_http_split_response()

Breaks apart an html formatted document string.

The string is broken into an array containing two parts: 'head' and 'body'. All other elements before, between, or after the html <head> and <body> tags are lost/ignored.

Why: Custom php scripts need a straight-forward and easy way to pull data from another website. This is useful as an alternative to iframe and has advantages and disadvantages to iframes. An advantage is that this allows showing remote content even if the remote url is down (via caching). A disadvantage is that remote images and links need to be processed, updated, and possibly even manually cached.

Parameters

string $document: An http response string.

array $function_history: (optional) An array of function names, ie: array('0' => 'my_function_name').

Return value

array An array containing the connection status and return http response. The array keys:

  • split: A boolean with TRUE representing that the document string was

successfully split, FALSE otherwise.

  • headers: The http header from the httpd response.
  • document: The complete html document from the http response.

File

modules/cf_http/cf_http.module, line 347

Code

function cf_http_split_response($document, array $function_history = array()) {
  cf_error_append_history($function_history, __FUNCTION__);
  $results = array(
    'split' => FALSE,
    'head' => '',
    'body' => '',
  );
  if (cf_is_empty_or_non_string($function_history, 'document', $document, WATCHDOG_ERROR)) {
    return $results;
  }
  $matches = array();
  $result = preg_match('/<head[^>]*>(.*)<\\/head>/sim', $document, $matches);
  if ($result > 0 && isset($matches[1])) {
    $results['head'] = $matches[1];
  }
  else {
    return $results;
  }
  $matches = array();
  $result = preg_match('/<body[^>]*>(.*)<\\/body>/sim', $document, $matches);
  if ($result > 0 && isset($matches[1])) {
    $results['body'] = $matches[1];
  }
  else {
    return $results;
  }
  $results['split'] = TRUE;
  return $results;
}