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 +----------------------------------------------------------------------+
24 #include "php_http_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_cache_api.h"
27 #include "php_http_curl_api.h"
28 #include "php_http_date_api.h"
29 #include "php_http_headers_api.h"
30 #include "php_http_send_api.h"
31 #include "php_http_url_api.h"
35 /* {{{ HttpResponse */
37 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
39 * Instantiates a new HttpResponse object, which can be used to send
40 * any data/resource/file to an HTTP client with caching and multiple
41 * ranges/resuming support.
43 * NOTE: GZIPping is not implemented yet.
45 PHP_METHOD(HttpResponse
, __construct
)
47 zend_bool do_cache
= 0, do_gzip
= 0;
48 getObject(http_response_object
, obj
);
50 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
55 UPD_PROP(obj
, long, cache
, do_cache
);
56 UPD_PROP(obj
, long, gzip
, do_gzip
);
60 /* {{{ proto bool HttpResponse::setCache(bool cache)
62 * Whether it sould be attempted to cache the entitity.
63 * This will result in necessary caching headers and checks of clients
64 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
65 * matches a "304 Not Modified" status code will be issued.
67 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
68 * to something more appropriate than "no-cache"!
70 PHP_METHOD(HttpResponse
, setCache
)
72 zend_bool do_cache
= 0;
73 getObject(http_response_object
, obj
);
75 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
79 UPD_PROP(obj
, long, cache
, do_cache
);
84 /* {{{ proto bool HttpResponse::getCache()
86 * Get current caching setting.
88 PHP_METHOD(HttpResponse
, getCache
)
90 zval
*do_cache
= NULL
;
91 getObject(http_response_object
, obj
);
95 do_cache
= GET_PROP(obj
, cache
);
96 RETURN_BOOL(Z_LVAL_P(do_cache
));
100 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
102 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
104 PHP_METHOD(HttpResponse
, setGzip
)
106 zend_bool do_gzip
= 0;
107 getObject(http_response_object
, obj
);
109 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
113 UPD_PROP(obj
, long, gzip
, do_gzip
);
118 /* {{{ proto bool HttpResponse::getGzip()
120 * Get current gzipping setting.
122 PHP_METHOD(HttpResponse
, getGzip
)
124 zval
*do_gzip
= NULL
;
125 getObject(http_response_object
, obj
);
129 do_gzip
= GET_PROP(obj
, gzip
);
130 RETURN_BOOL(Z_LVAL_P(do_gzip
));
134 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
136 * Set a custom cache-control header, usually being "private" or "public"; if
137 * $raw is set to true the header will be sent as-is.
139 PHP_METHOD(HttpResponse
, setCacheControl
)
144 getObject(http_response_object
, obj
);
146 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
150 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
151 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
155 UPD_PROP(obj
, long, raw_cache_header
, raw
);
156 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
161 /* {{{ proto string HttpResponse::getCacheControl()
163 * Get current Cache-Control header setting.
165 PHP_METHOD(HttpResponse
, getCacheControl
)
168 getObject(http_response_object
, obj
);
172 ccontrol
= GET_PROP(obj
, cacheControl
);
173 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
177 /* {{{ proto bool HttpResponse::setContentType(string content_type)
179 * Set the content-type of the sent entity.
181 PHP_METHOD(HttpResponse
, setContentType
)
185 getObject(http_response_object
, obj
);
187 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
191 if (!strchr(ctype
, '/')) {
192 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
193 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
197 UPD_PROP(obj
, string
, contentType
, ctype
);
203 /* {{{ proto string HttpResponse::getContentType()
205 * Get current Content-Type header setting.
207 PHP_METHOD(HttpResponse
, getContentType
)
210 getObject(http_response_object
, obj
);
214 ctype
= GET_PROP(obj
, contentType
);
215 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
219 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
221 * Set the Content-Disposition of the sent entity. This setting aims to suggest
222 * the receiveing user agent how to handle the sent entity; usually the client
223 * will show the user a "Save As..." popup.
225 PHP_METHOD(HttpResponse
, setContentDisposition
)
229 zend_bool is_inline
= 0;
230 getObject(http_response_object
, obj
);
232 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
236 UPD_PROP(obj
, string
, dispoFile
, file
);
237 UPD_PROP(obj
, long, dispoInline
, is_inline
);
242 /* {{{ proto array HttpResponse::getContentDisposition()
244 * Get current Content-Disposition setting.
245 * Will return an associative array like:
248 * 'filename' => 'foo.bar',
253 PHP_METHOD(HttpResponse
, getContentDisposition
)
257 getObject(http_response_object
, obj
);
259 if (ZEND_NUM_ARGS()) {
263 file
= GET_PROP(obj
, dispoFile
);
264 is_inline
= GET_PROP(obj
, dispoInline
);
266 array_init(return_value
);
267 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
268 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
272 /* {{{ proto bool HttpResponse::setETag(string etag)
274 * Set a custom ETag. Use this only if you know what you're doing.
276 PHP_METHOD(HttpResponse
, setETag
)
280 getObject(http_response_object
, obj
);
282 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
286 UPD_PROP(obj
, string
, eTag
, etag
);
291 /* {{{ proto string HttpResponse::getETag()
293 * Get the previously set custom ETag.
295 PHP_METHOD(HttpResponse
, getETag
)
298 getObject(http_response_object
, obj
);
302 etag
= GET_PROP(obj
, eTag
);
303 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
307 /* {{{ proto bool HttpResponse::setData(string data)
309 * Set the data to be sent.
311 PHP_METHOD(HttpResponse
, setData
)
314 getObject(http_response_object
, obj
);
316 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_data
)) {
320 convert_to_string_ex(&the_data
);
321 SET_PROP(obj
, data
, the_data
);
322 UPD_PROP(obj
, long, lastModified
, http_lmod(the_data
, SEND_DATA
));
323 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
328 /* {{{ proto string HttpResponse::getData()
330 * Get the previously set data to be sent.
332 PHP_METHOD(HttpResponse
, getData
)
335 getObject(http_response_object
, obj
);
339 the_data
= GET_PROP(obj
, data
);
340 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
344 /* {{{ proto bool HttpResponse::setStream(resource stream)
346 * Set the resource to be sent.
348 PHP_METHOD(HttpResponse
, setStream
)
351 php_stream
*the_real_stream
;
352 getObject(http_response_object
, obj
);
354 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
358 php_stream_from_zval(the_real_stream
, &the_stream
);
360 SET_PROP(obj
, stream
, the_stream
);
361 UPD_PROP(obj
, long, lastModified
, http_lmod(the_real_stream
, SEND_RSRC
));
362 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
367 /* {{{ proto resource HttpResponse::getStream()
369 * Get the previously set resource to be sent.
371 PHP_METHOD(HttpResponse
, getStream
)
374 getObject(http_response_object
, obj
);
378 the_stream
= GET_PROP(obj
, stream
);
379 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
383 /* {{{ proto bool HttpResponse::setFile(string file)
385 * Set the file to be sent.
387 PHP_METHOD(HttpResponse
, setFile
)
390 getObject(http_response_object
, obj
);
392 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
396 convert_to_string_ex(&the_file
);
398 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
399 UPD_PROP(obj
, long, lastModified
, http_lmod(the_file
, -1));
400 UPD_PROP(obj
, long, send_mode
, -1);
405 /* {{{ proto string HttpResponse::getFile()
407 * Get the previously set file to be sent.
409 PHP_METHOD(HttpResponse
, getFile
)
412 getObject(http_response_object
, obj
);
416 the_file
= GET_PROP(obj
, file
);
417 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
421 /* {{{ proto bool HttpResponse::send()
423 * Finally send the entity.
428 * $r = new HttpResponse(true);
429 * $r->setFile('../hidden/contract.pdf');
430 * $r->setContentType('application/pdf');
436 PHP_METHOD(HttpResponse
, send
)
438 zval
*do_cache
, *do_gzip
;
439 getObject(http_response_object
, obj
);
443 do_cache
= GET_PROP(obj
, cache
);
444 do_gzip
= GET_PROP(obj
, gzip
);
447 if (Z_LVAL_P(do_gzip
)) {
448 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC
);
452 if (Z_LVAL_P(do_cache
)) {
453 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
455 etag
= GET_PROP(obj
, eTag
);
456 lmod
= GET_PROP(obj
, lastModified
);
457 cctrl
= GET_PROP(obj
, cacheControl
);
458 ccraw
= GET_PROP(obj
, raw_cache_header
);
460 if (Z_LVAL_P(ccraw
)) {
461 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
462 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
464 char cc_header
[42] = {0};
465 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
466 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_header
, strlen(cc_header
));
467 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_header
, strlen(cc_header
));
473 zval
*ctype
= GET_PROP(obj
, contentType
);
474 if (Z_STRLEN_P(ctype
)) {
475 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
477 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
481 /* content disposition */
483 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
484 if (Z_STRLEN_P(dispo_file
)) {
485 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
486 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), (zend_bool
) Z_LVAL_P(dispo_inline
));
492 zval
*send_mode
= GET_PROP(obj
, send_mode
);
493 switch (Z_LVAL_P(send_mode
))
497 zval
*zdata
= GET_PROP(obj
, data
);
498 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
503 php_stream
*the_real_stream
;
504 zval
*the_stream
= GET_PROP(obj
, stream
);
505 php_stream_from_zval(the_real_stream
, &the_stream
);
506 RETURN_SUCCESS(http_send_stream(the_real_stream
));
511 zval
*zfile
= GET_PROP(obj
, file
);
512 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile
)));
520 /* {{{ HttpMessage */
522 /* {{{ void HttpMessage::__construct([string raw_message]) */
523 PHP_METHOD(HttpMessage
, __construct
)
525 zval
*message
= NULL
;
527 getObject(http_message_object
, obj
);
528 http_message
*msg
= obj
->message
;
530 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|z/", &message
)) {
535 SET_PROP(obj
, raw
, message
);
540 /* {{{ void HttpMessage::__destruct() */
541 PHP_METHOD(HttpMessage
, __destruct
)
543 getObject(http_message_object
, obj
);
552 #ifdef HTTP_HAVE_CURL
553 /* {{{ HttpRequest */
555 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
557 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
558 * and POST (including posting files) HTTP requests.
560 PHP_METHOD(HttpRequest
, __construct
)
565 getObject(http_request_object
, obj
);
567 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
571 INIT_PARR(obj
, options
);
572 INIT_PARR(obj
, responseInfo
);
573 INIT_PARR(obj
, responseData
);
574 INIT_PARR(obj
, postData
);
575 INIT_PARR(obj
, postFiles
);
578 UPD_PROP(obj
, string
, url
, URL
);
581 UPD_PROP(obj
, long, method
, meth
);
586 /* {{{ proto void HttpRequest::__destruct()
588 * Destroys the HttpRequest object.
590 PHP_METHOD(HttpRequest
, __destruct
)
592 getObject(http_request_object
, obj
);
596 FREE_PARR(obj
, options
);
597 FREE_PARR(obj
, responseInfo
);
598 FREE_PARR(obj
, responseData
);
599 FREE_PARR(obj
, postData
);
600 FREE_PARR(obj
, postFiles
);
604 /* {{{ proto bool HttpRequest::setOptions(array options)
606 * Set the request options to use. See http_get() for a full list of available options.
608 PHP_METHOD(HttpRequest
, setOptions
)
612 zval
*opts
, *old_opts
, **opt
;
613 getObject(http_request_object
, obj
);
615 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
619 old_opts
= GET_PROP(obj
, options
);
621 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
622 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
624 if (!strcmp(key
, "headers")) {
626 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
627 array_merge(*opt
, *headers
);
630 } else if (!strcmp(key
, "cookies")) {
632 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
633 array_merge(*opt
, *cookies
);
638 add_assoc_zval(old_opts
, key
, *opt
);
649 /* {{{ proto array HttpRequest::getOptions()
651 * Get current set options.
653 PHP_METHOD(HttpRequest
, getOptions
)
656 getObject(http_request_object
, obj
);
660 opts
= GET_PROP(obj
, options
);
661 array_init(return_value
);
662 array_copy(opts
, return_value
);
666 /* {{{ proto void HttpRequest::unsetOptions()
668 * Unset all options/headers/cookies.
670 PHP_METHOD(HttpRequest
, unsetOptions
)
672 getObject(http_request_object
, obj
);
676 FREE_PARR(obj
, options
);
677 INIT_PARR(obj
, options
);
681 /* {{{ proto bool HttpRequest::setSslOptions(array options)
683 * Set additional SSL options.
685 PHP_METHOD(HttpRequest
, setSslOptions
)
687 zval
*opts
, *old_opts
, **ssl_options
;
688 getObject(http_request_object
, obj
);
690 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
694 old_opts
= GET_PROP(obj
, options
);
696 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
697 array_merge(opts
, *ssl_options
);
700 add_assoc_zval(old_opts
, "ssl", opts
);
707 /* {{{ proto array HttpRequest::getSslOtpions()
709 * Get previously set SSL options.
711 PHP_METHOD(HttpRequest
, getSslOptions
)
713 zval
*opts
, **ssl_options
;
714 getObject(http_request_object
, obj
);
718 opts
= GET_PROP(obj
, options
);
720 array_init(return_value
);
722 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
723 array_copy(*ssl_options
, return_value
);
728 /* {{{ proto void HttpRequest::unsetSslOptions()
730 * Unset previously set SSL options.
732 PHP_METHOD(HttpRequest
, unsetSslOptions
)
735 getObject(http_request_object
, obj
);
739 opts
= GET_PROP(obj
, options
);
740 zend_hash_del(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"));
744 /* {{{ proto bool HttpRequest::addHeaders(array headers)
746 * Add request header name/value pairs.
748 PHP_METHOD(HttpRequest
, addHeaders
)
750 zval
*opts
, **headers
, *new_headers
;
751 getObject(http_request_object
, obj
);
753 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
757 opts
= GET_PROP(obj
, options
);
759 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
760 array_merge(new_headers
, *headers
);
762 zval_add_ref(&new_headers
);
763 add_assoc_zval(opts
, "headers", new_headers
);
770 /* {{{ proto array HttpRequest::getHeaders()
772 * Get previously set request headers.
774 PHP_METHOD(HttpRequest
, getHeaders
)
776 zval
*opts
, **headers
;
777 getObject(http_request_object
, obj
);
781 opts
= GET_PROP(obj
, options
);
783 array_init(return_value
);
785 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
786 array_copy(*headers
, return_value
);
791 /* {{{ proto void HttpRequest::unsetHeaders()
793 * Unset previously set request headers.
795 PHP_METHOD(HttpRequest
, unsetHeaders
)
798 getObject(http_request_object
, obj
);
802 opts
= GET_PROP(obj
, options
);
803 zend_hash_del(Z_ARRVAL_P(opts
), "headers", sizeof("headers"));
807 /* {{{ proto bool HttpRequest::addCookies(array cookies)
811 PHP_METHOD(HttpRequest
, addCookies
)
813 zval
*opts
, **cookies
, *new_cookies
;
814 getObject(http_request_object
, obj
);
816 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
820 opts
= GET_PROP(obj
, options
);
822 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
823 array_merge(new_cookies
, *cookies
);
825 zval_add_ref(&new_cookies
);
826 add_assoc_zval(opts
, "cookies", new_cookies
);
833 /* {{{ proto array HttpRequest::getCookies()
835 * Get previously set cookies.
837 PHP_METHOD(HttpRequest
, getCookies
)
839 zval
*opts
, **cookies
;
840 getObject(http_request_object
, obj
);
844 opts
= GET_PROP(obj
, options
);
846 array_init(return_value
);
848 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
849 array_copy(*cookies
, return_value
);
854 /* {{{ proto void HttpRequest::unsetCookies()
857 PHP_METHOD(HttpRequest
, unsetCookies
)
860 getObject(http_request_object
, obj
);
864 opts
= GET_PROP(obj
, options
);
865 zend_hash_del(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"));
869 /* {{{ proto bool HttpRequest::setURL(string url)
871 * Set the request URL.
873 PHP_METHOD(HttpRequest
, setURL
)
877 getObject(http_request_object
, obj
);
879 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
883 UPD_PROP(obj
, string
, url
, URL
);
888 /* {{{ proto string HttpRequest::getUrl()
890 * Get the previously set request URL.
892 PHP_METHOD(HttpRequest
, getURL
)
895 getObject(http_request_object
, obj
);
899 URL
= GET_PROP(obj
, url
);
900 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
904 /* {{{ proto bool HttpRequest::setMethod(long request_method)
906 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
907 * <tt>HTTP_POST</tt> constants.
909 PHP_METHOD(HttpRequest
, setMethod
)
912 getObject(http_request_object
, obj
);
914 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
918 UPD_PROP(obj
, long, method
, meth
);
923 /* {{{ proto long HttpRequest::getMethod()
925 * Get the previously set request method.
927 PHP_METHOD(HttpRequest
, getMethod
)
930 getObject(http_request_object
, obj
);
934 meth
= GET_PROP(obj
, method
);
935 RETURN_LONG(Z_LVAL_P(meth
));
939 /* {{{ proto bool HttpRequest::setContentType(string content_type)
941 * Set the content type the post request should have.
942 * Use this only if you know what you're doing.
944 PHP_METHOD(HttpRequest
, setContentType
)
948 getObject(http_request_object
, obj
);
950 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
954 if (!strchr(ctype
, '/')) {
955 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
956 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
961 UPD_PROP(obj
, string
, contentType
, ctype
);
966 /* {{{ proto string HttpRequest::getContentType()
968 * Get the previously content type.
970 PHP_METHOD(HttpRequest
, getContentType
)
973 getObject(http_request_object
, obj
);
977 ctype
= GET_PROP(obj
, contentType
);
978 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
982 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
984 * Set the URL query parameters to use.
985 * Overwrites previously set query parameters.
986 * Affects any request types.
988 PHP_METHOD(HttpRequest
, setQueryData
)
991 char *query_data
= NULL
;
992 getObject(http_request_object
, obj
);
994 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
998 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
999 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1002 UPD_PROP(obj
, string
, queryData
, query_data
);
1007 convert_to_string(qdata
);
1008 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
1013 /* {{{ proto string HttpRequest::getQueryData()
1015 * Get the current query data in form of an urlencoded query string.
1017 PHP_METHOD(HttpRequest
, getQueryData
)
1020 getObject(http_request_object
, obj
);
1024 qdata
= GET_PROP(obj
, queryData
);
1025 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
1029 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1031 * Add parameters to the query parameter list.
1032 * Affects any request type.
1034 PHP_METHOD(HttpRequest
, addQueryData
)
1036 zval
*qdata
, *old_qdata
;
1037 char *query_data
= NULL
;
1038 getObject(http_request_object
, obj
);
1040 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1044 old_qdata
= GET_PROP(obj
, queryData
);
1046 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
1050 UPD_PROP(obj
, string
, queryData
, query_data
);
1057 /* {{{ proto void HttpRequest::unsetQueryData()
1059 * Clean the query parameters.
1060 * Affects any request type.
1062 PHP_METHOD(HttpRequest
, unsetQueryData
)
1064 getObject(http_request_object
, obj
);
1068 UPD_PROP(obj
, string
, queryData
, "");
1072 /* {{{ proto bool HttpRequest::addPostData(array post_data)
1074 * Adds POST data entries.
1075 * Affects only POST requests.
1077 PHP_METHOD(HttpRequest
, addPostData
)
1079 zval
*post
, *post_data
;
1080 getObject(http_request_object
, obj
);
1082 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1086 post
= GET_PROP(obj
, postData
);
1087 array_merge(post_data
, post
);
1093 /* {{{ proto bool HttpRequest::setPostData(array post_data)
1095 * Set the POST data entries.
1096 * Overwrites previously set POST data.
1097 * Affects only POST requests.
1099 PHP_METHOD(HttpRequest
, setPostData
)
1101 zval
*post
, *post_data
;
1102 getObject(http_request_object
, obj
);
1104 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1108 post
= GET_PROP(obj
, postData
);
1109 zend_hash_clean(Z_ARRVAL_P(post
));
1110 array_copy(post_data
, post
);
1116 /* {{{ proto array HttpRequest::getPostData()
1118 * Get previously set POST data.
1120 PHP_METHOD(HttpRequest
, getPostData
)
1123 getObject(http_request_object
, obj
);
1127 post_data
= GET_PROP(obj
, postData
);
1128 array_init(return_value
);
1129 array_copy(post_data
, return_value
);
1133 /* {{{ proto void HttpRequest::unsetPostData()
1135 * Clean POST data entires.
1136 * Affects only POST requests.
1138 PHP_METHOD(HttpRequest
, unsetPostData
)
1141 getObject(http_request_object
, obj
);
1145 post_data
= GET_PROP(obj
, postData
);
1146 zend_hash_clean(Z_ARRVAL_P(post_data
));
1150 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1152 * Add a file to the POST request.
1153 * Affects only POST requests.
1155 PHP_METHOD(HttpRequest
, addPostFile
)
1157 zval
*files
, *entry
;
1158 char *name
, *file
, *type
= NULL
;
1159 int name_len
, file_len
, type_len
= 0;
1160 getObject(http_request_object
, obj
);
1162 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1167 if (!strchr(type
, '/')) {
1168 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
1172 type
= "application/x-octetstream";
1173 type_len
= sizeof("application/x-octetstream") - 1;
1176 MAKE_STD_ZVAL(entry
);
1179 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1180 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1181 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1183 files
= GET_PROP(obj
, postFiles
);
1184 add_next_index_zval(files
, entry
);
1190 /* {{{ proto array HttpRequest::getPostFiles()
1192 * Get all previously added POST files.
1194 PHP_METHOD(HttpRequest
, getPostFiles
)
1197 getObject(http_request_object
, obj
);
1201 files
= GET_PROP(obj
, postFiles
);
1203 array_init(return_value
);
1204 array_copy(files
, return_value
);
1208 /* {{{ proto void HttpRequest::unsetPostFiles()
1210 * Unset the POST files list.
1211 * Affects only POST requests.
1213 PHP_METHOD(HttpRequest
, unsetPostFiles
)
1216 getObject(http_request_object
, obj
);
1220 files
= GET_PROP(obj
, postFiles
);
1221 zend_hash_clean(Z_ARRVAL_P(files
));
1225 /* {{{ proto array HttpRequest::getResponseData()
1227 * Get all response data after the request has been sent.
1229 PHP_METHOD(HttpRequest
, getResponseData
)
1232 getObject(http_request_object
, obj
);
1236 data
= GET_PROP(obj
, responseData
);
1237 array_init(return_value
);
1238 array_copy(data
, return_value
);
1242 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1244 * Get response header(s) after the request has been sent.
1246 PHP_METHOD(HttpRequest
, getResponseHeader
)
1248 zval
*data
, **headers
, **header
;
1249 char *header_name
= NULL
;
1251 getObject(http_response_object
, obj
);
1253 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1257 data
= GET_PROP(obj
, responseData
);
1258 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1262 if (!header_len
|| !header_name
) {
1263 array_init(return_value
);
1264 array_copy(*headers
, return_value
);
1265 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1266 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1273 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1275 * Get response cookie(s) after the request has been sent.
1277 PHP_METHOD(HttpRequest
, getResponseCookie
)
1279 zval
*data
, **headers
;
1280 char *cookie_name
= NULL
;
1282 getObject(http_request_object
, obj
);
1284 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1288 array_init(return_value
);
1290 data
= GET_PROP(obj
, responseData
);
1291 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1294 zval
**header
= NULL
;
1296 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1297 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1298 /* several cookies? */
1299 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1302 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1304 MAKE_STD_ZVAL(cookie_hash
);
1305 array_init(cookie_hash
);
1307 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1309 add_next_index_zval(return_value
, cookie_hash
);
1313 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1314 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1315 add_next_index_zval(return_value
, cookie_hash
);
1316 return; /* <<< FOUND >>> */
1318 zval_dtor(cookie_hash
);
1323 zval_dtor(cookie_hash
);
1329 MAKE_STD_ZVAL(cookie_hash
);
1330 array_init(cookie_hash
);
1332 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1334 add_next_index_zval(return_value
, cookie_hash
);
1338 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1339 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1340 add_next_index_zval(return_value
, cookie_hash
);
1342 zval_dtor(cookie_hash
);
1347 zval_dtor(cookie_hash
);
1360 /* {{{ proto string HttpRequest::getResponseBody()
1362 * Get the response body after the request has been sent.
1364 PHP_METHOD(HttpRequest
, getResponseBody
)
1367 getObject(http_request_object
, obj
);
1371 data
= GET_PROP(obj
, responseData
);
1372 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1373 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
1380 /* {{{ proto int HttpRequest::getResponseCode()
1382 * Get the response code after the request has been sent.
1384 PHP_METHOD(HttpRequest
, getResponseCode
)
1386 zval
**code
, **hdrs
, *data
;
1387 getObject(http_request_object
, obj
);
1391 data
= GET_PROP(obj
, responseData
);
1392 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &hdrs
)) &&
1393 (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(hdrs
), "Response Status", sizeof("Response Status"), (void **) &code
))) {
1394 RETVAL_STRINGL(Z_STRVAL_PP(code
), Z_STRLEN_PP(code
), 1);
1395 convert_to_long(return_value
);
1402 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1404 * Get response info after the request has been sent.
1405 * See http_get() for a full list of returned info.
1407 PHP_METHOD(HttpRequest
, getResponseInfo
)
1409 zval
*info
, **infop
;
1410 char *info_name
= NULL
;
1412 getObject(http_request_object
, obj
);
1414 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1418 info
= GET_PROP(obj
, responseInfo
);
1420 if (info_len
&& info_name
) {
1421 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1422 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
1424 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Could not find response info named %s", info_name
);
1428 array_init(return_value
);
1429 array_copy(info
, return_value
);
1434 /* {{{ proto bool HttpRequest::send()
1436 * Send the HTTP request.
1441 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1442 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1443 * $r->addQueryData(array('category' => 3));
1444 * if ($r->send() && $r->getResponseCode() == 200) {
1445 * file_put_contents('local.rss', $r->getResponseBody());
1453 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1454 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1455 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
1456 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1458 * echo $r->getResponseBody();
1463 PHP_METHOD(HttpRequest
, send
)
1465 STATUS status
= FAILURE
;
1466 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
1467 char *response_data
, *request_uri
;
1468 size_t response_len
;
1469 getObject(http_request_object
, obj
);
1473 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
1474 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initilaize cURL");
1478 meth
= GET_PROP(obj
, method
);
1479 URL
= GET_PROP(obj
, url
);
1480 qdata
= GET_PROP(obj
, queryData
);
1481 opts
= GET_PROP(obj
, options
);
1482 info
= GET_PROP(obj
, responseInfo
);
1483 resp
= GET_PROP(obj
, responseData
);
1485 // HTTP_URI_MAXLEN+1 long char *
1486 request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0);
1488 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
1489 if (!strchr(request_uri
, '?')) {
1490 strcat(request_uri
, "?");
1492 strcat(request_uri
, "&");
1494 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
1497 switch (Z_LVAL_P(meth
))
1500 status
= http_get_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1504 status
= http_head_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1509 zval
*post_files
, *post_data
;
1511 post_files
= GET_PROP(obj
, postFiles
);
1512 post_data
= GET_PROP(obj
, postData
);
1514 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files
))) {
1516 /* urlencoded post */
1517 status
= http_post_array_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(post_data
), Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1527 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
1530 FOREACH_KEYVAL(post_data
, key
, idx
, data
) {
1532 convert_to_string_ex(data
);
1533 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1534 CURLFORM_COPYNAME
, key
,
1535 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
1536 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
1546 FOREACH_VAL(post_files
, data
) {
1547 zval
**file
, **type
, **name
;
1549 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
1550 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
1551 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
1553 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1554 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
1555 CURLFORM_FILE
, Z_STRVAL_PP(file
),
1556 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
1562 status
= http_post_curldata_ex(obj
->ch
, request_uri
, http_post_data
[0], Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1563 curl_formfree(http_post_data
[0]);
1574 /* final data handling */
1575 if (status
!= SUCCESS
) {
1579 size_t body_len
= 0;
1582 MAKE_STD_ZVAL(zheaders
)
1583 array_init(zheaders
);
1585 if (SUCCESS
!= http_split_response_ex(response_data
, response_len
, Z_ARRVAL_P(zheaders
), &body
, &body_len
)) {
1586 zval_dtor(zheaders
);
1588 efree(response_data
);
1592 add_assoc_zval(resp
, "headers", zheaders
);
1593 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
1595 efree(response_data
);
1603 #endif /* HTTP_HAVE_CURL */
1605 #endif /* ZEND_ENGINE_2 */
1612 * vim600: noet sw=4 ts=4 fdm=marker
1613 * vim<600: noet sw=4 ts=4