You are here

function cf_http_get_webpage in Common Functionality 7

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

Reads and processes a website page at the given path.

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 $server: Hostname or ip address of the server. Should not contain http:// or similary prefixes.

string $path: The file/path on the server to.

int $port: (optional) Port number of the page to read (defaults to 80).

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:

  • read: A boolean with TRUE representing that the read was successful and

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 521

Code

function cf_http_get_webpage($server, $path, $port = 80, array $function_history = array()) {
  cf_error_append_history($function_history, __FUNCTION__);
  $results = array(
    'read' => FALSE,
    'headers' => '',
    'document' => '',
    'http_error' => array(
      'error_code' => 0,
      'key' => '',
      'value' => '',
    ),
  );
  $result = cf_http_get_response($server, $path, $port, $function_history);
  if ($result['connected']) {
    $result = cf_http_parse_response($result['response'], $function_history);
    if ($result['parsed']) {
      $results['headers'] = $result['headers'];
      $results['document'] = $result['document'];
      $results['http_error'] = $result['http_error'];
      $results['read'] = TRUE;
    }
  }
  return $results;
}