summaryrefslogtreecommitdiff
path: root/ext/standard/url.c
diff options
context:
space:
mode:
authorIlia Alshanetsky <iliaa@php.net>2003-02-07 21:33:35 +0000
committerIlia Alshanetsky <iliaa@php.net>2003-02-07 21:33:35 +0000
commitb1a6778fe52a6a8514db903adb72a03d83051886 (patch)
tree73825cb81f0538ff0cf6a6a061f9d9ad60798a0f /ext/standard/url.c
parent598e82751d0dd9bd228167d262a09d26d6459f54 (diff)
downloadphp-git-b1a6778fe52a6a8514db903adb72a03d83051886.tar.gz
Added get_browser() function. This function can be used to fetch the headers
sent by the server when a request is made for a given URL.
Diffstat (limited to 'ext/standard/url.c')
-rw-r--r--ext/standard/url.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/ext/standard/url.c b/ext/standard/url.c
index 8752a6a201..8bd2a99d00 100644
--- a/ext/standard/url.c
+++ b/ext/standard/url.c
@@ -552,6 +552,63 @@ PHPAPI int php_raw_url_decode(char *str, int len)
}
/* }}} */
+/* {{{ proto array get_headers(string url)
+ fetches all the headers sent by the server in response to a HTTP request */
+PHP_FUNCTION(get_headers)
+{
+ char *url, *url_len;
+ php_stream_context *context = NULL;
+ php_stream *stream;
+ zval **prev_val, **hdr = NULL;
+ HashPosition pos;
+ long format = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &url, &url_len, &format) == FAILURE) {
+ return;
+ }
+
+ if (!(stream = php_stream_open_wrapper_ex(url, "r", REPORT_ERRORS | STREAM_USE_URL | STREAM_ONLY_GET_HEADERS, NULL, context))) {
+ RETURN_FALSE;
+ }
+
+ array_init(return_value);
+
+ zend_hash_internal_pointer_reset_ex(HASH_OF(stream->wrapperdata), &pos);
+ while (zend_hash_get_current_data_ex(HASH_OF(stream->wrapperdata), (void**)&hdr, &pos) != FAILURE) {
+ if (!format) {
+no_name_header:
+ add_next_index_stringl(return_value, Z_STRVAL_PP(hdr), Z_STRLEN_PP(hdr), 1);
+ } else {
+ char c;
+ char *s, *p;
+
+ if ((p = strchr(Z_STRVAL_PP(hdr), ':'))) {
+ c = *p;
+ *p = '\0';
+ s = p + 1;
+ while (isspace(*s)) {
+ s++;
+ }
+
+ if (zend_hash_find(HASH_OF(return_value), Z_STRVAL_PP(hdr), (p - Z_STRVAL_PP(hdr) + 1), (void **) &prev_val) == FAILURE) {
+ add_assoc_stringl_ex(return_value, Z_STRVAL_PP(hdr), (p - Z_STRVAL_PP(hdr) + 1), s, (Z_STRLEN_PP(hdr) - (s - Z_STRVAL_PP(hdr))), 1);
+ } else { /* some headers may occur more then once, therefor we need to remake the string into an array */
+ convert_to_array(*prev_val);
+ add_next_index_stringl(*prev_val, s, (Z_STRLEN_PP(hdr) - (s - Z_STRVAL_PP(hdr))), 1);
+ }
+
+ *p = c;
+ } else {
+ goto no_name_header;
+ }
+ }
+ zend_hash_move_forward_ex(HASH_OF(stream->wrapperdata), &pos);
+ }
+
+ php_stream_close(stream);
+}
+/* }}} */
+
/*
* Local variables:
* tab-width: 4