2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
23 #include "zend_operators.h"
27 #include "ext/standard/info.h"
28 #include "ext/standard/php_string.h"
29 #if defined(HAVE_PHP_SESSION) && !defined(COMPILE_DL_SESSION)
30 # include "ext/session/php_session.h"
34 #include "php_http_std_defs.h"
35 #include "php_http_api.h"
36 #include "php_http_request_api.h"
37 #include "php_http_cache_api.h"
38 #include "php_http_request_method_api.h"
39 #include "php_http_request_api.h"
40 #include "php_http_date_api.h"
41 #include "php_http_headers_api.h"
42 #include "php_http_message_api.h"
43 #include "php_http_send_api.h"
44 #include "php_http_url_api.h"
46 #include "phpstr/phpstr.h"
48 ZEND_EXTERN_MODULE_GLOBALS(http
)
50 /* {{{ proto string http_date([int timestamp])
52 * This function returns a valid HTTP date regarding RFC 822/1123
53 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
56 PHP_FUNCTION(http_date
)
60 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
65 t
= (long) time(NULL
);
68 RETURN_STRING(http_date(t
), 0);
72 /* {{{ proto string http_absolute_uri(string url[, string proto[, string host[, int port]]])
74 * This function returns an absolute URI constructed from url.
75 * If the url is already abolute but a different proto was supplied,
76 * only the proto part of the URI will be updated. If url has no
77 * path specified, the path of the current REQUEST_URI will be taken.
78 * The host will be taken either from the Host HTTP header of the client
79 * the SERVER_NAME or just localhost if prior are not available.
83 * url = "page.php" => http://www.example.com/current/path/page.php
84 * url = "/page.php" => http://www.example.com/page.php
85 * url = "/page.php", proto = "https" => https://www.example.com/page.php
89 PHP_FUNCTION(http_absolute_uri
)
91 char *url
= NULL
, *proto
= NULL
, *host
= NULL
;
92 int url_len
= 0, proto_len
= 0, host_len
= 0;
95 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|ssl", &url
, &url_len
, &proto
, &proto_len
, &host
, &host_len
, &port
) != SUCCESS
) {
99 RETURN_STRING(http_absolute_uri_ex(url
, url_len
, proto
, proto_len
, host
, host_len
, port
), 0);
103 #define HTTP_DO_NEGOTIATE(type, supported, as_array) \
106 if (result = http_negotiate_ ##type(supported)) { \
108 Z_TYPE_P(return_value) = IS_ARRAY; \
109 Z_ARRVAL_P(return_value) = result; \
115 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
116 RETVAL_STRINGL(key, key_len-1, 0); \
120 zend_hash_destroy(result); \
126 array_init(return_value); \
128 FOREACH_VAL(supported, value) { \
129 convert_to_string_ex(value); \
130 add_assoc_double(return_value, Z_STRVAL_PP(value), 1.0); \
135 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
136 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void **) &value)) { \
137 RETVAL_ZVAL(*value, 1, 0); \
146 /* {{{ proto string http_negotiate_language(array supported[, bool return_quality_array = false])
148 * This function negotiates the clients preferred language based on its
149 * Accept-Language HTTP header. It returns the negotiated language or
150 * the default language (i.e. first array entry) if none match.
152 * The qualifier is recognized and languages without qualifier are rated highest.
154 * The supported parameter is expected to be an array having
155 * the supported languages as array values.
169 * include './langs/'. http_negotiate_language($langs) .'.php';
174 PHP_FUNCTION(http_negotiate_language
)
177 zend_bool as_array
= 0;
179 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &supported
, &as_array
) != SUCCESS
) {
183 HTTP_DO_NEGOTIATE(language
, supported
, as_array
);
187 /* {{{ proto string http_negotiate_charset(array supported)
189 * This function negotiates the clients preferred charset based on its
190 * Accept-Charset HTTP header. It returns the negotiated charset or
191 * the default charset (i.e. first array entry) if none match.
193 * The qualifier is recognized and charset without qualifier are rated highest.
195 * The supported parameter is expected to be an array having
196 * the supported charsets as array values.
202 * 'iso-8859-1', // default
207 * $pref = http_negotiate_charset($charsets);
208 * if (!strcmp($pref, 'iso-8859-1')) {
209 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
210 * iconv_set_encoding('output_encoding', $pref);
211 * ob_start('ob_iconv_handler');
216 PHP_FUNCTION(http_negotiate_charset
)
219 zend_bool as_array
= 0;
221 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|b", &supported
, &as_array
) != SUCCESS
) {
225 HTTP_DO_NEGOTIATE(charset
, supported
, as_array
);
229 /* {{{ proto bool http_send_status(int status)
231 * Send HTTP status code.
234 PHP_FUNCTION(http_send_status
)
238 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
241 if (status
< 100 || status
> 510) {
242 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
246 RETURN_SUCCESS(http_send_status(status
));
250 /* {{{ proto bool http_send_last_modified([int timestamp])
252 * This converts the given timestamp to a valid HTTP date and
253 * sends it as "Last-Modified" HTTP header. If timestamp is
254 * omitted, current time is sent.
257 PHP_FUNCTION(http_send_last_modified
)
261 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
266 t
= (long) time(NULL
);
269 RETURN_SUCCESS(http_send_last_modified(t
));
273 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
275 * Sets the content type.
278 PHP_FUNCTION(http_send_content_type
)
283 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
288 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", lenof("application/x-octetstream")));
290 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
294 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
296 * Set the Content Disposition. The Content-Disposition header is very useful
297 * if the data actually sent came from a file or something similar, that should
298 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
301 PHP_FUNCTION(http_send_content_disposition
)
305 zend_bool send_inline
= 0;
307 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
310 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
314 /* {{{ proto bool http_match_modified([int timestamp[, for_range = false]])
316 * Matches the given timestamp against the clients "If-Modified-Since" resp.
317 * "If-Unmodified-Since" HTTP headers.
320 PHP_FUNCTION(http_match_modified
)
323 zend_bool for_range
= 0;
325 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
329 // current time if not supplied (senseless though)
331 t
= (long) time(NULL
);
335 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t
));
337 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t
));
341 /* {{{ proto bool http_match_etag(string etag[, for_range = false])
343 * This matches the given ETag against the clients
344 * "If-Match" resp. "If-None-Match" HTTP headers.
347 PHP_FUNCTION(http_match_etag
)
351 zend_bool for_range
= 0;
353 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
358 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag
));
360 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag
));
364 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
366 * If timestamp_or_expires is greater than 0, it is handled as timestamp
367 * and will be sent as date of last modification. If it is 0 or omitted,
368 * the current time will be sent as Last-Modified date. If it's negative,
369 * it is handled as expiration time in seconds, which means that if the
370 * requested last modification date is not between the calculated timespan,
371 * the Last-Modified header is updated and the actual body will be sent.
374 PHP_FUNCTION(http_cache_last_modified
)
376 long last_modified
= 0, send_modified
= 0, t
;
379 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
383 t
= (long) time(NULL
);
386 if (!last_modified
) {
387 /* does the client have? (att: caching "forever") */
388 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
389 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
390 /* send current time */
394 /* negative value is supposed to be expiration time */
395 } else if (last_modified
< 0) {
398 /* send supplied time explicitly */
400 send_modified
= last_modified
;
403 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
407 /* {{{ proto bool http_cache_etag([string etag])
409 * This function attempts to cache the HTTP body based on an ETag,
410 * either supplied or generated through calculation of the MD5
411 * checksum of the output (uses output buffering).
413 * If clients "If-None-Match" header matches the supplied/calculated
414 * ETag, the body is considered cached on the clients side and
415 * a "304 Not Modified" status code is issued.
418 PHP_FUNCTION(http_cache_etag
)
423 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
427 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
431 /* {{{ proto string ob_etaghandler(string data, int mode)
433 * For use with ob_start().
435 PHP_FUNCTION(ob_etaghandler
)
441 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
445 Z_TYPE_P(return_value
) = IS_STRING
;
446 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
450 /* {{{ proto void http_throttle(double sec[, long bytes = 2097152])
452 * Use with http_send() API.
458 * # http_throttle(1, 20000);
459 * # http_throttle(0.5, 10000);
460 * # http_throttle(0.1, 2000);
461 * http_send_file('document.pdf');
465 PHP_FUNCTION(http_throttle
)
470 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "dl", &interval
, &chunk_size
)) {
474 HTTP_G(send
).throttle_delay
= interval
;
475 HTTP_G(send
).buffer_size
= chunk_size
;
479 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
481 * Redirect to a given url.
482 * The supplied url will be expanded with http_absolute_uri(), the params array will
483 * be treated with http_build_query() and the session identification will be appended
484 * if session is true.
486 * Depending on permanent the redirection will be issued with a permanent
487 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
490 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
491 * if the client doesn't redirect immediatly.
493 PHP_FUNCTION(http_redirect
)
496 size_t query_len
= 0;
497 zend_bool session
= 0, permanent
= 0, free_params
= 0;
499 char *query
= NULL
, *url
= NULL
, *URI
, *LOC
, *RED
= NULL
;
501 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bb", &url
, &url_len
, ¶ms
, &session
, &permanent
) != SUCCESS
) {
505 /* append session info */
509 MAKE_STD_ZVAL(params
);
512 #ifdef HAVE_PHP_SESSION
513 # ifdef COMPILE_DL_SESSION
514 if (SUCCESS
== zend_get_module_started("session")) {
515 zval nm_retval
, id_retval
, func
;
518 INIT_PZVAL(&nm_retval
);
519 INIT_PZVAL(&id_retval
);
520 ZVAL_NULL(&nm_retval
);
521 ZVAL_NULL(&id_retval
);
523 ZVAL_STRINGL(&func
, "session_id", lenof("session_id"), 0);
524 call_user_function(EG(function_table
), NULL
, &func
, &id_retval
, 0, NULL TSRMLS_CC
);
525 ZVAL_STRINGL(&func
, "session_name", lenof("session_name"), 0);
526 call_user_function(EG(function_table
), NULL
, &func
, &nm_retval
, 0, NULL TSRMLS_CC
);
528 if ( Z_TYPE(nm_retval
) == IS_STRING
&& Z_STRLEN(nm_retval
) &&
529 Z_TYPE(id_retval
) == IS_STRING
&& Z_STRLEN(id_retval
)) {
530 if (add_assoc_stringl_ex(params
, Z_STRVAL(nm_retval
), Z_STRLEN(nm_retval
)+1,
531 Z_STRVAL(id_retval
), Z_STRLEN(id_retval
), 0) != SUCCESS
) {
532 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
537 if (PS(session_status
) == php_session_active
) {
538 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
539 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
546 /* treat params array with http_build_query() */
548 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
560 URI
= http_absolute_uri(url
);
563 spprintf(&LOC
, 0, "Location: %s?%s", URI
, query
);
564 if (SG(request_info
).request_method
&& strcmp(SG(request_info
).request_method
, "HEAD")) {
565 spprintf(&RED
, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
568 spprintf(&LOC
, 0, "Location: %s", URI
);
569 if (SG(request_info
).request_method
&& strcmp(SG(request_info
).request_method
, "HEAD")) {
570 spprintf(&RED
, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
583 RETURN_SUCCESS(http_exit_ex(permanent
? 301 : 302, LOC
, RED
, 1));
587 /* {{{ proto bool http_send_data(string data)
589 * Sends raw data with support for (multiple) range requests.
592 PHP_FUNCTION(http_send_data
)
596 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
600 convert_to_string_ex(&zdata
);
601 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
605 /* {{{ proto bool http_send_file(string file)
607 * Sends a file with support for (multiple) range requests.
610 PHP_FUNCTION(http_send_file
)
615 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
622 RETURN_SUCCESS(http_send_file(file
));
626 /* {{{ proto bool http_send_stream(resource stream)
628 * Sends an already opened stream with support for (multiple) range requests.
631 PHP_FUNCTION(http_send_stream
)
636 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
640 php_stream_from_zval(file
, &zstream
);
641 RETURN_SUCCESS(http_send_stream(file
));
645 /* {{{ proto string http_chunked_decode(string encoded)
647 * This function decodes a string that was HTTP-chunked encoded.
648 * Returns false on failure.
650 PHP_FUNCTION(http_chunked_decode
)
652 char *encoded
= NULL
, *decoded
= NULL
;
653 size_t decoded_len
= 0;
656 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
660 if (NULL
!= http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
661 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
668 /* {{{ proto object http_parse_message(string message)
670 * Parses (a) http_message(s) into a simple recursive object structure:
674 * print_r(http_parse_message(http_get(URL, array('redirect' => 3)));
679 * [httpVersion] => 1.1
680 * [responseCode] => 200
683 * [Content-Length] => 3
687 * [parentMessage] => stdClass object
690 * [httpVersion] => 1.1
691 * [responseCode] => 302
694 * [Content-Length] => 0
698 * [parentMessage] => ...
704 PHP_FUNCTION(http_parse_message
)
708 http_message
*msg
= NULL
;
710 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &message
, &message_len
)) {
715 if (msg
= http_message_parse(message
, message_len
)) {
716 object_init(return_value
);
717 http_message_tostruct_recursive(msg
, return_value
);
718 http_message_free(&msg
);
725 /* {{{ proto array http_parse_headers(string header)
728 PHP_FUNCTION(http_parse_headers
)
733 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
737 array_init(return_value
);
738 if (SUCCESS
!= http_parse_headers(header
, return_value
)) {
739 zval_dtor(return_value
);
745 /* {{{ proto array http_get_request_headers(void)
747 * Get a list of incoming HTTP headers.
749 PHP_FUNCTION(http_get_request_headers
)
753 array_init(return_value
);
754 http_get_request_headers(return_value
);
758 /* {{{ proto string http_get_request_body(void)
760 * Get the raw request body (e.g. POST or PUT data).
762 PHP_FUNCTION(http_get_request_body
)
769 if (SUCCESS
== http_get_request_body(&body
, &length
)) {
770 RETURN_STRINGL(body
, (int) length
, 0);
777 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
779 * Match an incoming HTTP header.
781 PHP_FUNCTION(http_match_request_header
)
783 char *header
, *value
;
784 int header_len
, value_len
;
785 zend_bool match_case
= 0;
787 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|b", &header
, &header_len
, &value
, &value_len
, &match_case
)) {
791 RETURN_BOOL(http_match_request_header_ex(header
, value
, match_case
));
796 #ifdef HTTP_HAVE_CURL
798 /* {{{ proto string http_get(string url[, array options[, array &info]])
800 * Performs an HTTP GET request on the supplied url.
802 * The second parameter is expected to be an associative
803 * array where the following keys will be recognized:
805 * - redirect: int, whether and how many redirects to follow
806 * - unrestrictedauth: bool, whether to continue sending credentials on
807 * redirects to a different host
808 * - proxyhost: string, proxy host in "host[:port]" format
809 * - proxyport: int, use another proxy port as specified in proxyhost
810 * - proxyauth: string, proxy credentials in "user:pass" format
811 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
812 * - httpauth: string, http credentials in "user:pass" format
813 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
814 * - compress: bool, whether to allow gzip/deflate content encoding
816 * - port: int, use another port as specified in the url
817 * - referer: string, the referer to sends
818 * - useragent: string, the user agent to send
819 * (defaults to PECL::HTTP/version (PHP/version)))
820 * - headers: array, list of custom headers as associative array
821 * like array("header" => "value")
822 * - cookies: array, list of cookies as associative array
823 * like array("cookie" => "value")
824 * - cookiestore: string, path to a file where cookies are/will be stored
825 * - resume: int, byte offset to start the download from;
826 * if the server supports ranges
827 * - maxfilesize: int, maximum file size that should be downloaded;
828 * has no effect, if the size of the requested entity is not known
829 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
830 * - timeout: int, seconds the request may take
831 * - connecttimeout: int, seconds the connect may take
832 * - onprogress: mixed, progress callback
835 * The optional third parameter will be filled with some additional information
836 * in form af an associative array, if supplied, like the following example:
840 * 'effective_url' => 'http://localhost',
841 * 'response_code' => 403,
842 * 'total_time' => 0.017,
843 * 'namelookup_time' => 0.013,
844 * 'connect_time' => 0.014,
845 * 'pretransfer_time' => 0.014,
846 * 'size_upload' => 0,
847 * 'size_download' => 202,
848 * 'speed_download' => 11882,
849 * 'speed_upload' => 0,
850 * 'header_size' => 145,
851 * 'request_size' => 62,
852 * 'ssl_verifyresult' => 0,
854 * 'content_length_download' => 202,
855 * 'content_length_upload' => 0,
856 * 'starttransfer_time' => 0.017,
857 * 'content_type' => 'text/html; charset=iso-8859-1',
858 * 'redirect_time' => 0,
859 * 'redirect_count' => 0,
860 * 'http_connectcode' => 0,
861 * 'httpauth_avail' => 0,
862 * 'proxyauth_avail' => 0,
867 PHP_FUNCTION(http_get
)
869 zval
*options
= NULL
, *info
= NULL
;
874 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
883 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
884 if (SUCCESS
== http_get(URL
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
885 RETURN_PHPSTR_VAL(&response
);
892 /* {{{ proto string http_head(string url[, array options[, array &info]])
894 * Performs an HTTP HEAD request on the suppied url.
895 * Returns the HTTP response as string.
896 * See http_get() for a full list of available options.
898 PHP_FUNCTION(http_head
)
900 zval
*options
= NULL
, *info
= NULL
;
905 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
914 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
915 if (SUCCESS
== http_head(URL
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
916 RETURN_PHPSTR_VAL(&response
);
923 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
925 * Performs an HTTP POST request, posting data.
926 * Returns the HTTP response as string.
927 * See http_get() for a full list of available options.
929 PHP_FUNCTION(http_post_data
)
931 zval
*options
= NULL
, *info
= NULL
;
932 char *URL
, *postdata
;
933 int postdata_len
, URL_len
;
935 http_request_body body
;
937 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
946 body
.type
= HTTP_REQUEST_BODY_CSTRING
;
947 body
.data
= postdata
;
948 body
.size
= postdata_len
;
950 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
951 if (SUCCESS
== http_post(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
952 RETVAL_PHPSTR_VAL(&response
);
959 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
961 * Performs an HTTP POST request, posting www-form-urlencoded array data.
962 * Returns the HTTP response as string.
963 * See http_get() for a full list of available options.
965 PHP_FUNCTION(http_post_fields
)
967 zval
*options
= NULL
, *info
= NULL
, *fields
, *files
= NULL
;
971 http_request_body body
;
973 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|aa/!z", &URL
, &URL_len
, &fields
, &files
, &options
, &info
) != SUCCESS
) {
977 if (SUCCESS
!= http_request_body_fill(&body
, Z_ARRVAL_P(fields
), files
? Z_ARRVAL_P(files
) : NULL
)) {
986 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
987 if (SUCCESS
== http_post(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
988 RETVAL_PHPSTR_VAL(&response
);
992 http_request_body_dtor(&body
);
996 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
998 * Performs an HTTP PUT request, uploading file.
999 * Returns the HTTP response as string.
1000 * See http_get() for a full list of available options.
1002 PHP_FUNCTION(http_put_file
)
1006 zval
*options
= NULL
, *info
= NULL
;
1009 php_stream_statbuf ssb
;
1010 http_request_body body
;
1012 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &file
, &f_len
, &options
, &info
)) {
1016 if (!(stream
= php_stream_open_wrapper(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
1019 if (php_stream_stat(stream
, &ssb
)) {
1020 php_stream_close(stream
);
1029 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
1031 body
.size
= ssb
.sb
.st_size
;
1033 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
1034 if (SUCCESS
== http_put(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
1035 RETVAL_PHPSTR_VAL(&response
);
1039 http_request_body_dtor(&body
);
1043 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1045 * Performs an HTTP PUT request, uploading stream.
1046 * Returns the HTTP response as string.
1047 * See http_get() for a full list of available options.
1049 PHP_FUNCTION(http_put_stream
)
1051 zval
*resource
, *options
= NULL
, *info
= NULL
;
1056 php_stream_statbuf ssb
;
1057 http_request_body body
;
1059 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sr|a/!z", &URL
, &URL_len
, &resource
, &options
, &info
)) {
1063 php_stream_from_zval(stream
, &resource
);
1064 if (php_stream_stat(stream
, &ssb
)) {
1073 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
1075 body
.size
= ssb
.sb
.st_size
;
1077 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
1078 if (SUCCESS
== http_put(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
1079 RETURN_PHPSTR_VAL(&response
);
1085 #endif /* HTTP_HAVE_CURL */
1088 /* {{{ proto long http_request_method_register(string method)
1090 * Register a custom request method.
1092 PHP_FUNCTION(http_request_method_register
)
1096 unsigned long existing
;
1098 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1101 if (existing
= http_request_method_exists(1, 0, method
)) {
1102 RETURN_LONG((long) existing
);
1105 RETVAL_LONG((long) http_request_method_register(method
));
1109 /* {{{ proto bool http_request_method_unregister(mixed method)
1111 * Unregister a previously registered custom request method.
1113 PHP_FUNCTION(http_request_method_unregister
)
1117 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1121 switch (Z_TYPE_P(method
))
1124 convert_to_string(method
);
1126 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1127 convert_to_long(method
);
1130 if (!(mn
= http_request_method_exists(1, 0, Z_STRVAL_P(method
)))) {
1134 ZVAL_LONG(method
, (long)mn
);
1137 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method
)));
1144 /* {{{ proto long http_request_method_exists(mixed method)
1146 * Check if a request method is registered (or available by default).
1148 PHP_FUNCTION(http_request_method_exists
)
1153 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1157 switch (Z_TYPE_P(method
))
1160 convert_to_string(method
);
1162 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1163 convert_to_long(method
);
1165 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method
)));
1168 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method
), NULL
));
1176 /* {{{ proto string http_request_method_name(long method)
1178 * Get the literal string representation of a standard or registered request method.
1180 PHP_FUNCTION(http_request_method_name
)
1185 if ((SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &method
)) || (method
< 0)) {
1189 RETURN_STRING(estrdup(http_request_method_name((unsigned long) method
)), 0);
1194 /* {{{ Sara Golemons http_build_query() */
1195 #ifndef ZEND_ENGINE_2
1197 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1198 Generates a form-encoded query string from an associative array or object. */
1199 PHP_FUNCTION(http_build_query
)
1202 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
1203 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
1206 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
1210 if (Z_TYPE_P(formdata
) != IS_ARRAY
&& Z_TYPE_P(formdata
) != IS_OBJECT
) {
1211 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Parameter 1 expected to be Array or Object. Incorrect value given.");
1216 arg_sep
= HTTP_URL_ARGSEP
;
1219 formstr
= phpstr_new();
1220 if (SUCCESS
!= http_urlencode_hash_implementation_ex(HASH_OF(formdata
), formstr
, arg_sep
, prefix
, prefix_len
, NULL
, 0, NULL
, 0, (Z_TYPE_P(formdata
) == IS_OBJECT
? formdata
: NULL
))) {
1221 phpstr_free(&formstr
);
1225 if (!formstr
->used
) {
1226 phpstr_free(&formstr
);
1230 RETURN_PHPSTR_PTR(formstr
);
1233 #endif /* !ZEND_ENGINE_2 */
1236 PHP_FUNCTION(http_test
)
1245 * vim600: noet sw=4 ts=4 fdm=marker
1246 * vim<600: noet sw=4 ts=4