function cf_http_get_response in Common Functionality 7.2
Same name and namespace in other branches
- 7 modules/cf_http/cf_http.module \cf_http_get_response()
Reads an http page at the given path and returns an unprocessed response.
@see: cf_http_get_webpage() @see: fsockopen()
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:
- connected: A boolean with TRUE representing that the connection to the
server was established and FALSE otherwise.
- response: The http response as returned by the target server. This http response must be processed.
- socket_error: An array containing the error code and message returned by fsockopen() on error.
Related topics
1 call to cf_http_get_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 65 - Common Functionality - HTTP module.
Code
function cf_http_get_response($server, $path, $port = 80, $headers = "Accept-Encoding: deflate\r\n", $timeout = 8, $buffer = 8192) {
$results = array(
'connected' => FALSE,
'response' => '',
'is_local' => FALSE,
'socket_error' => array(
'code' => 0,
'message' => NULL,
),
);
if (cf_is_empty_or_non_string('server', $server, WATCHDOG_ERROR)) {
return $results;
}
if (cf_is_empty_or_non_string('path', $path, WATCHDOG_ERROR)) {
return $results;
}
if (!is_numeric($port)) {
if (class_exists('cf_error')) {
cf_error::invalid_numeric('port');
}
return $results;
}
if (!empty($headers) && !is_string($headers)) {
cf_error::invalid_string('headers');
return $results;
}
$fp = fsockopen($server, $port, $errno, $errstr, $timeout);
if ($errno != 0) {
$results['socket_error']['code'] = $errno;
$results['socket_error']['message'] = $errstr;
return $results;
}
$server_address = preg_replace('@^\\w+://@i', '', $server);
if ($fp && is_string($server_address)) {
$request = 'GET ' . $path . ' HTTP/1.1' . "\r\n";
$request .= 'Host: ' . $server_address . "\r\n";
$request .= $headers;
$request .= 'Connection: Close' . "\r\n\r\n";
fwrite($fp, $request);
stream_set_timeout($fp, $timeout);
$results['is_local'] = stream_is_local($fp);
while (!feof($fp)) {
$results['response'] .= fgets($fp, $buffer);
$info = stream_get_meta_data($fp);
if ($info['timed_out']) {
fclose($fp);
return $results;
}
}
$results['connected'] = TRUE;
fclose($fp);
return $results;
}
return $results;
}