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 +----------------------------------------------------------------------+
25 #include "ext/standard/info.h"
26 #include "ext/standard/php_string.h"
27 #if defined(HAVE_PHP_SESSION) && !defined(COMPILE_DL_SESSION)
28 # include "ext/session/php_session.h"
32 #include "php_http_std_defs.h"
33 #include "php_http_api.h"
34 #include "php_http_request_api.h"
35 #include "php_http_cache_api.h"
36 #include "php_http_request_method_api.h"
37 #include "php_http_request_api.h"
38 #include "php_http_date_api.h"
39 #include "php_http_headers_api.h"
40 #include "php_http_message_api.h"
41 #include "php_http_send_api.h"
42 #include "php_http_url_api.h"
44 #include "phpstr/phpstr.h"
46 ZEND_EXTERN_MODULE_GLOBALS(http
)
48 /* {{{ proto string http_date([int timestamp])
50 * This function returns a valid HTTP date regarding RFC 822/1123
51 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
54 PHP_FUNCTION(http_date
)
58 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
63 t
= (long) time(NULL
);
66 RETURN_STRING(http_date(t
), 0);
70 /* {{{ proto string http_absolute_uri(string url[, string proto[, string host[, int port]]])
72 * This function returns an absolute URI constructed from url.
73 * If the url is already abolute but a different proto was supplied,
74 * only the proto part of the URI will be updated. If url has no
75 * path specified, the path of the current REQUEST_URI will be taken.
76 * The host will be taken either from the Host HTTP header of the client
77 * the SERVER_NAME or just localhost if prior are not available.
81 * url = "page.php" => http://www.example.com/current/path/page.php
82 * url = "/page.php" => http://www.example.com/page.php
83 * url = "/page.php", proto = "https" => https://www.example.com/page.php
87 PHP_FUNCTION(http_absolute_uri
)
89 char *url
= NULL
, *proto
= NULL
, *host
= NULL
;
90 int url_len
= 0, proto_len
= 0, host_len
= 0;
93 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|ssl", &url
, &url_len
, &proto
, &proto_len
, &host
, &host_len
, &port
) != SUCCESS
) {
97 RETURN_STRING(http_absolute_uri_ex(url
, url_len
, proto
, proto_len
, host
, host_len
, port
), 0);
101 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
103 * This function negotiates the clients preferred language based on its
104 * Accept-Language HTTP header. It returns the negotiated language or
105 * the default language if none match.
107 * The qualifier is recognized and languages without qualifier are rated highest.
109 * The supported parameter is expected to be an array having
110 * the supported languages as array values.
124 * include './langs/'. http_negotiate_language($langs) .'.php';
129 PHP_FUNCTION(http_negotiate_language
)
135 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
143 RETURN_STRING(http_negotiate_language(supported
, def
), 0);
147 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
149 * This function negotiates the clients preferred charset based on its
150 * Accept-Charset HTTP header. It returns the negotiated charset or
151 * the default charset if none match.
153 * The qualifier is recognized and charset without qualifier are rated highest.
155 * The supported parameter is expected to be an array having
156 * the supported charsets as array values.
162 * 'iso-8859-1', // default
167 * $pref = http_negotiate_charset($charsets);
168 * if (!strcmp($pref, 'iso-8859-1')) {
169 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
170 * iconv_set_encoding('output_encoding', $pref);
171 * ob_start('ob_iconv_handler');
176 PHP_FUNCTION(http_negotiate_charset
)
182 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
190 RETURN_STRING(http_negotiate_charset(supported
, def
), 0);
194 /* {{{ proto bool http_send_status(int status)
196 * Send HTTP status code.
199 PHP_FUNCTION(http_send_status
)
203 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
206 if (status
< 100 || status
> 510) {
207 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
211 RETURN_SUCCESS(http_send_status(status
));
215 /* {{{ proto bool http_send_last_modified([int timestamp])
217 * This converts the given timestamp to a valid HTTP date and
218 * sends it as "Last-Modified" HTTP header. If timestamp is
219 * omitted, current time is sent.
222 PHP_FUNCTION(http_send_last_modified
)
226 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
231 t
= (long) time(NULL
);
234 RETURN_SUCCESS(http_send_last_modified(t
));
238 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
240 * Sets the content type.
243 PHP_FUNCTION(http_send_content_type
)
248 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
253 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", lenof("application/x-octetstream")));
255 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
259 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
261 * Set the Content Disposition. The Content-Disposition header is very useful
262 * if the data actually sent came from a file or something similar, that should
263 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
266 PHP_FUNCTION(http_send_content_disposition
)
270 zend_bool send_inline
= 0;
272 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
275 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
279 /* {{{ proto bool http_match_modified([int timestamp[, for_range = false]])
281 * Matches the given timestamp against the clients "If-Modified-Since" resp.
282 * "If-Unmodified-Since" HTTP headers.
285 PHP_FUNCTION(http_match_modified
)
288 zend_bool for_range
= 0;
290 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
294 // current time if not supplied (senseless though)
296 t
= (long) time(NULL
);
300 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t
));
302 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t
));
306 /* {{{ proto bool http_match_etag(string etag[, for_range = false])
308 * This matches the given ETag against the clients
309 * "If-Match" resp. "If-None-Match" HTTP headers.
312 PHP_FUNCTION(http_match_etag
)
316 zend_bool for_range
= 0;
318 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
323 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag
));
325 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag
));
329 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
331 * If timestamp_or_expires is greater than 0, it is handled as timestamp
332 * and will be sent as date of last modification. If it is 0 or omitted,
333 * the current time will be sent as Last-Modified date. If it's negative,
334 * it is handled as expiration time in seconds, which means that if the
335 * requested last modification date is not between the calculated timespan,
336 * the Last-Modified header is updated and the actual body will be sent.
339 PHP_FUNCTION(http_cache_last_modified
)
341 long last_modified
= 0, send_modified
= 0, t
;
344 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
348 t
= (long) time(NULL
);
351 if (!last_modified
) {
352 /* does the client have? (att: caching "forever") */
353 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
354 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
355 /* send current time */
359 /* negative value is supposed to be expiration time */
360 } else if (last_modified
< 0) {
363 /* send supplied time explicitly */
365 send_modified
= last_modified
;
368 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
372 /* {{{ proto bool http_cache_etag([string etag])
374 * This function attempts to cache the HTTP body based on an ETag,
375 * either supplied or generated through calculation of the MD5
376 * checksum of the output (uses output buffering).
378 * If clients "If-None-Match" header matches the supplied/calculated
379 * ETag, the body is considered cached on the clients side and
380 * a "304 Not Modified" status code is issued.
383 PHP_FUNCTION(http_cache_etag
)
388 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
392 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
396 /* {{{ proto string ob_etaghandler(string data, int mode)
398 * For use with ob_start().
400 PHP_FUNCTION(ob_etaghandler
)
406 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
410 Z_TYPE_P(return_value
) = IS_STRING
;
411 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
415 /* {{{ proto void http_throttle(double sec[, long bytes = 2097152])
417 * Use with http_send() API.
423 * # http_throttle(1, 20000);
424 * # http_throttle(0.5, 10000);
425 * # http_throttle(0.1, 2000);
426 * http_send_file('document.pdf');
430 PHP_FUNCTION(http_throttle
)
435 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "dl", &interval
, &chunk_size
)) {
439 HTTP_G(send
).throttle_delay
= interval
;
440 HTTP_G(send
).buffer_size
= chunk_size
;
444 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
446 * Redirect to a given url.
447 * The supplied url will be expanded with http_absolute_uri(), the params array will
448 * be treated with http_build_query() and the session identification will be appended
449 * if session is true.
451 * Depending on permanent the redirection will be issued with a permanent
452 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
455 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
456 * if the client doesn't redirect immediatly.
458 PHP_FUNCTION(http_redirect
)
461 size_t query_len
= 0;
462 zend_bool session
= 0, permanent
= 0, free_params
= 0;
464 char *query
= NULL
, *url
= NULL
, *URI
, *LOC
, *RED
= NULL
;
466 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bb", &url
, &url_len
, ¶ms
, &session
, &permanent
) != SUCCESS
) {
470 /* append session info */
474 MAKE_STD_ZVAL(params
);
477 #ifdef HAVE_PHP_SESSION
478 # ifdef COMPILE_DL_SESSION
479 if (SUCCESS
== zend_get_module_started("session")) {
480 zval nm_retval
, id_retval
, func
;
483 INIT_PZVAL(&nm_retval
);
484 INIT_PZVAL(&id_retval
);
485 ZVAL_NULL(&nm_retval
);
486 ZVAL_NULL(&id_retval
);
488 ZVAL_STRINGL(&func
, "session_id", lenof("session_id"), 0);
489 call_user_function(EG(function_table
), NULL
, &func
, &id_retval
, 0, NULL TSRMLS_CC
);
490 ZVAL_STRINGL(&func
, "session_name", lenof("session_name"), 0);
491 call_user_function(EG(function_table
), NULL
, &func
, &nm_retval
, 0, NULL TSRMLS_CC
);
493 if ( Z_TYPE(nm_retval
) == IS_STRING
&& Z_STRLEN(nm_retval
) &&
494 Z_TYPE(id_retval
) == IS_STRING
&& Z_STRLEN(id_retval
)) {
495 if (add_assoc_stringl_ex(params
, Z_STRVAL(nm_retval
), Z_STRLEN(nm_retval
)+1,
496 Z_STRVAL(id_retval
), Z_STRLEN(id_retval
), 0) != SUCCESS
) {
497 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
502 if (PS(session_status
) == php_session_active
) {
503 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
504 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
511 /* treat params array with http_build_query() */
513 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
525 URI
= http_absolute_uri(url
);
528 spprintf(&LOC
, 0, "Location: %s?%s", URI
, query
);
529 if (SG(request_info
).request_method
&& strcmp(SG(request_info
).request_method
, "HEAD")) {
530 spprintf(&RED
, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
533 spprintf(&LOC
, 0, "Location: %s", URI
);
534 if (SG(request_info
).request_method
&& strcmp(SG(request_info
).request_method
, "HEAD")) {
535 spprintf(&RED
, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
548 RETURN_SUCCESS(http_exit_ex(permanent
? 301 : 302, LOC
, RED
, 1));
552 /* {{{ proto bool http_send_data(string data)
554 * Sends raw data with support for (multiple) range requests.
557 PHP_FUNCTION(http_send_data
)
561 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
565 convert_to_string_ex(&zdata
);
566 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
570 /* {{{ proto bool http_send_file(string file)
572 * Sends a file with support for (multiple) range requests.
575 PHP_FUNCTION(http_send_file
)
580 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
587 RETURN_SUCCESS(http_send_file(file
));
591 /* {{{ proto bool http_send_stream(resource stream)
593 * Sends an already opened stream with support for (multiple) range requests.
596 PHP_FUNCTION(http_send_stream
)
601 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
605 php_stream_from_zval(file
, &zstream
);
606 RETURN_SUCCESS(http_send_stream(file
));
610 /* {{{ proto string http_chunked_decode(string encoded)
612 * This function decodes a string that was HTTP-chunked encoded.
613 * Returns false on failure.
615 PHP_FUNCTION(http_chunked_decode
)
617 char *encoded
= NULL
, *decoded
= NULL
;
618 size_t decoded_len
= 0;
621 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
625 if (NULL
!= http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
626 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
633 /* {{{ proto object http_parse_message(string message)
635 * Parses (a) http_message(s) into a simple recursive object structure:
639 * print_r(http_parse_message(http_get(URL, array('redirect' => 3)));
644 * [httpVersion] => 1.1
645 * [responseCode] => 200
648 * [Content-Length] => 3
652 * [parentMessage] => stdClass object
655 * [httpVersion] => 1.1
656 * [responseCode] => 302
659 * [Content-Length] => 0
663 * [parentMessage] => ...
669 PHP_FUNCTION(http_parse_message
)
673 http_message
*msg
= NULL
;
675 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &message
, &message_len
)) {
680 if (msg
= http_message_parse(message
, message_len
)) {
681 object_init(return_value
);
682 http_message_tostruct_recursive(msg
, return_value
);
683 http_message_free(&msg
);
690 /* {{{ proto array http_parse_headers(string header)
693 PHP_FUNCTION(http_parse_headers
)
698 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
702 array_init(return_value
);
703 if (SUCCESS
!= http_parse_headers(header
, return_value
)) {
704 zval_dtor(return_value
);
710 /* {{{ proto array http_get_request_headers(void)
712 * Get a list of incoming HTTP headers.
714 PHP_FUNCTION(http_get_request_headers
)
718 array_init(return_value
);
719 http_get_request_headers(return_value
);
723 /* {{{ proto string http_get_request_body(void)
725 * Get the raw request body (e.g. POST or PUT data).
727 PHP_FUNCTION(http_get_request_body
)
734 if (SUCCESS
== http_get_request_body(&body
, &length
)) {
735 RETURN_STRINGL(body
, (int) length
, 0);
742 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
744 * Match an incoming HTTP header.
746 PHP_FUNCTION(http_match_request_header
)
748 char *header
, *value
;
749 int header_len
, value_len
;
750 zend_bool match_case
= 0;
752 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|b", &header
, &header_len
, &value
, &value_len
, &match_case
)) {
756 RETURN_BOOL(http_match_request_header_ex(header
, value
, match_case
));
761 #ifdef HTTP_HAVE_CURL
763 /* {{{ proto string http_get(string url[, array options[, array &info]])
765 * Performs an HTTP GET request on the supplied url.
767 * The second parameter is expected to be an associative
768 * array where the following keys will be recognized:
770 * - redirect: int, whether and how many redirects to follow
771 * - unrestrictedauth: bool, whether to continue sending credentials on
772 * redirects to a different host
773 * - proxyhost: string, proxy host in "host[:port]" format
774 * - proxyport: int, use another proxy port as specified in proxyhost
775 * - proxyauth: string, proxy credentials in "user:pass" format
776 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
777 * - httpauth: string, http credentials in "user:pass" format
778 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
779 * - compress: bool, whether to allow gzip/deflate content encoding
781 * - port: int, use another port as specified in the url
782 * - referer: string, the referer to sends
783 * - useragent: string, the user agent to send
784 * (defaults to PECL::HTTP/version (PHP/version)))
785 * - headers: array, list of custom headers as associative array
786 * like array("header" => "value")
787 * - cookies: array, list of cookies as associative array
788 * like array("cookie" => "value")
789 * - cookiestore: string, path to a file where cookies are/will be stored
790 * - resume: int, byte offset to start the download from;
791 * if the server supports ranges
792 * - maxfilesize: int, maximum file size that should be downloaded;
793 * has no effect, if the size of the requested entity is not known
794 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
795 * - timeout: int, seconds the request may take
796 * - connecttimeout: int, seconds the connect may take
797 * - onprogress: mixed, progress callback
800 * The optional third parameter will be filled with some additional information
801 * in form af an associative array, if supplied, like the following example:
805 * 'effective_url' => 'http://localhost',
806 * 'response_code' => 403,
807 * 'total_time' => 0.017,
808 * 'namelookup_time' => 0.013,
809 * 'connect_time' => 0.014,
810 * 'pretransfer_time' => 0.014,
811 * 'size_upload' => 0,
812 * 'size_download' => 202,
813 * 'speed_download' => 11882,
814 * 'speed_upload' => 0,
815 * 'header_size' => 145,
816 * 'request_size' => 62,
817 * 'ssl_verifyresult' => 0,
819 * 'content_length_download' => 202,
820 * 'content_length_upload' => 0,
821 * 'starttransfer_time' => 0.017,
822 * 'content_type' => 'text/html; charset=iso-8859-1',
823 * 'redirect_time' => 0,
824 * 'redirect_count' => 0,
825 * 'http_connectcode' => 0,
826 * 'httpauth_avail' => 0,
827 * 'proxyauth_avail' => 0,
832 PHP_FUNCTION(http_get
)
834 zval
*options
= NULL
, *info
= NULL
;
839 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
848 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
849 if (SUCCESS
== http_get(URL
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
850 RETURN_PHPSTR_VAL(&response
);
857 /* {{{ proto string http_head(string url[, array options[, array &info]])
859 * Performs an HTTP HEAD request on the suppied url.
860 * Returns the HTTP response as string.
861 * See http_get() for a full list of available options.
863 PHP_FUNCTION(http_head
)
865 zval
*options
= NULL
, *info
= NULL
;
870 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
879 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
880 if (SUCCESS
== http_head(URL
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
881 RETURN_PHPSTR_VAL(&response
);
888 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
890 * Performs an HTTP POST request, posting data.
891 * Returns the HTTP response as string.
892 * See http_get() for a full list of available options.
894 PHP_FUNCTION(http_post_data
)
896 zval
*options
= NULL
, *info
= NULL
;
897 char *URL
, *postdata
;
898 int postdata_len
, URL_len
;
900 http_request_body body
;
902 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
911 body
.type
= HTTP_REQUEST_BODY_CSTRING
;
912 body
.data
= postdata
;
913 body
.size
= postdata_len
;
915 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
916 if (SUCCESS
== http_post(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
917 RETVAL_PHPSTR_VAL(&response
);
924 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
926 * Performs an HTTP POST request, posting www-form-urlencoded array data.
927 * Returns the HTTP response as string.
928 * See http_get() for a full list of available options.
930 PHP_FUNCTION(http_post_fields
)
932 zval
*options
= NULL
, *info
= NULL
, *fields
, *files
= NULL
;
936 http_request_body body
;
938 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|aa/!z", &URL
, &URL_len
, &fields
, &files
, &options
, &info
) != SUCCESS
) {
942 if (SUCCESS
!= http_request_body_fill(&body
, Z_ARRVAL_P(fields
), files
? Z_ARRVAL_P(files
) : NULL
)) {
951 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
952 if (SUCCESS
== http_post(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
953 RETVAL_PHPSTR_VAL(&response
);
957 http_request_body_dtor(&body
);
961 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
963 * Performs an HTTP PUT request, uploading file.
964 * Returns the HTTP response as string.
965 * See http_get() for a full list of available options.
967 PHP_FUNCTION(http_put_file
)
971 zval
*options
= NULL
, *info
= NULL
;
974 php_stream_statbuf ssb
;
975 http_request_body body
;
977 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &file
, &f_len
, &options
, &info
)) {
981 if (!(stream
= php_stream_open_wrapper(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
984 if (php_stream_stat(stream
, &ssb
)) {
985 php_stream_close(stream
);
994 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
996 body
.size
= ssb
.sb
.st_size
;
998 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
999 if (SUCCESS
== http_put(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
1000 RETVAL_PHPSTR_VAL(&response
);
1004 http_request_body_dtor(&body
);
1008 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1010 * Performs an HTTP PUT request, uploading stream.
1011 * Returns the HTTP response as string.
1012 * See http_get() for a full list of available options.
1014 PHP_FUNCTION(http_put_stream
)
1016 zval
*resource
, *options
= NULL
, *info
= NULL
;
1021 php_stream_statbuf ssb
;
1022 http_request_body body
;
1024 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sr|a/!z", &URL
, &URL_len
, &resource
, &options
, &info
)) {
1028 php_stream_from_zval(stream
, &resource
);
1029 if (php_stream_stat(stream
, &ssb
)) {
1038 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
1040 body
.size
= ssb
.sb
.st_size
;
1042 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
1043 if (SUCCESS
== http_put(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
1044 RETURN_PHPSTR_VAL(&response
);
1051 /* {{{ proto long http_request_method_register(string method)
1053 * Register a custom request method.
1055 PHP_FUNCTION(http_request_method_register
)
1059 unsigned long existing
;
1061 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1064 if (existing
= http_request_method_exists(1, 0, method
)) {
1065 RETURN_LONG((long) existing
);
1068 RETVAL_LONG((long) http_request_method_register(method
));
1072 /* {{{ proto bool http_request_method_unregister(mixed method)
1074 * Unregister a previously registered custom request method.
1076 PHP_FUNCTION(http_request_method_unregister
)
1080 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1084 switch (Z_TYPE_P(method
))
1087 convert_to_string(method
);
1089 #include "zend_operators.h"
1090 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1091 convert_to_long(method
);
1094 if (!(mn
= http_request_method_exists(1, 0, Z_STRVAL_P(method
)))) {
1098 ZVAL_LONG(method
, (long)mn
);
1101 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method
)));
1108 /* {{{ proto long http_request_method_exists(mixed method)
1110 * Check if a request method is registered (or available by default).
1112 PHP_FUNCTION(http_request_method_exists
)
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
);
1129 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method
)));
1132 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method
), NULL
));
1140 /* {{{ proto string http_request_method_name(long method)
1142 * Get the literal string representation of a standard or registered request method.
1144 PHP_FUNCTION(http_request_method_name
)
1149 if ((SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &method
)) || (method
< 0)) {
1153 RETURN_STRING(estrdup(http_request_method_name((unsigned long) method
)), 0);
1160 /* {{{ Sara Golemons http_build_query() */
1161 #ifndef ZEND_ENGINE_2
1163 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1164 Generates a form-encoded query string from an associative array or object. */
1165 PHP_FUNCTION(http_build_query
)
1168 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
1169 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
1172 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
1176 if (Z_TYPE_P(formdata
) != IS_ARRAY
&& Z_TYPE_P(formdata
) != IS_OBJECT
) {
1177 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Parameter 1 expected to be Array or Object. Incorrect value given.");
1182 arg_sep
= HTTP_URL_ARGSEP
;
1185 formstr
= phpstr_new();
1186 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
))) {
1187 phpstr_free(&formstr
);
1191 if (!formstr
->used
) {
1192 phpstr_free(&formstr
);
1196 RETURN_PHPSTR_PTR(formstr
);
1199 #endif /* !ZEND_ENGINE_2 */
1202 PHP_FUNCTION(http_test
)
1211 * vim600: noet sw=4 ts=4 fdm=marker
1212 * vim<600: noet sw=4 ts=4