You are here

function cf_http_get_webpage in Common Functionality 7.2

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

Reads and processes a website page at the given path.

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).

string $headers: (optional) additional headers to pass when requesting the url.

$timeout: (optional) Socket open and stream timeout when connecting to remove host, in seconds.

$buffer: (optional) Buffer size to use when reading from the socket, in bytes.

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.

Related topics

File

modules/cf_http/cf_http.module, line 618
Common Functionality - HTTP module.

Code

function cf_http_get_webpage($server, $path, $port = 80, $headers = "Accept-Encoding: deflate\r\n", $timeout = 8, $buffer = 8192) {
  $results = array(
    'read' => FALSE,
    'headers' => '',
    'document' => '',
    'http_error' => array(
      'error_code' => 0,
      'key' => '',
      'value' => '',
    ),
    'socket_error' => array(
      'code' => 0,
      'message' => NULL,
    ),
  );
  $result = cf_http_get_response($server, $path, $port, $headers, $timeout, $buffer);
  if (isset($results['socket_error'])) {
    $results['socket_error'] = $result['socket_error'];
  }
  if (isset($result['connected']) && $result['connected'] && isset($result['response'])) {
    $result = cf_http_parse_response($result['response']);
    if ($result['parsed']) {
      $results['headers'] = $result['headers'];
      $results['document'] = $result['document'];
      $results['http_error'] = $result['http_error'];
      $results['read'] = TRUE;
    }
  }
  return $results;
}