function cf_http_parse_response in Common Functionality 7
Same name and namespace in other branches
- 7.2 modules/cf_http/cf_http.module \cf_http_parse_response()
Accepts and processes provided http content.
This process checks for a valid http response, unchunks if needed, returns http content without headers on success, false on any errors.
Originally From: http://php.net/manual/en/function.fsockopen.php#85572
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 $http_response: An http response string.
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:
- parsed: A boolean with TRUE representing that the http request string
was successfully parsed, FALSE otherwise.
- headers: The http header from the httpd response.
- document: The complete html document from the http response.
1 call to cf_http_parse_response()
- cf_http_get_webpage in modules/
cf_http/ cf_http.module - Reads and processes a website page at the given path.
File
- modules/
cf_http/ cf_http.module, line 264
Code
function cf_http_parse_response($http_response, array $function_history = array()) {
cf_error_append_history($function_history, __FUNCTION__);
$results = array(
'parsed' => FALSE,
'header' => '',
'document' => '',
'http_error' => array(
'error_code' => 0,
'key' => '',
'value' => '',
),
);
if (cf_is_empty_or_non_string($function_history, 'http_response', $http_response, WATCHDOG_ERROR)) {
return $results;
}
// split into array, headers and content.
$hunks = explode("\r\n\r\n", trim($http_response));
if (!is_array($hunks) || count($hunks) < 2) {
return $results;
}
$header = $hunks[count($hunks) - 2];
$document = $hunks[count($hunks) - 1];
$headers = explode("\n", $header);
$results['headers'] = $headers;
$results['document'] = $document;
unset($hunks);
unset($header);
unset($document);
if (!cf_http_validate_response($results['headers'], $function_history)) {
$results['http_error'] = cf_http_headers_errors($results['headers'], $function_history);
}
if (in_array('Transfer-Coding: chunked', $results['headers'])) {
$result = cf_http_unchunk_response($results['document'], $function_history);
if ($result['unchunked']) {
$results['document'] = $result['document'];
}
}
$results['document'] = trim($results['document']);
// remove some additional trash not removed by the original function
$results['document'] = preg_replace("/^[[:alnum:]]+\r\n/i", '', $results['document']);
$results['document'] = preg_replace("/\r\n0\$/i", '', $results['document']);
if (!is_string($results['document'])) {
$results['document'] = '';
return $results;
}
$results['parsed'] = TRUE;
return $results;
}