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_api.h"
25 #include "php_http_curl_api.h"
26 #include "ext/standard/php_smart_str.h"
30 /* {{{ HTTPi_Response */
32 /* {{{ proto void HTTPi_Response::__construct(bool cache, bool gzip)
35 PHP_METHOD(HTTPi_Response
, __construct
)
37 zend_bool do_cache
= 0, do_gzip
= 0;
38 getObject(httpi_response_object
, obj
);
40 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
45 UPD_PROP(obj
, long, cache
, do_cache
);
46 UPD_PROP(obj
, long, gzip
, do_gzip
);
50 /* {{{ proto bool HTTPi_Response::setCache(bool cache)
53 PHP_METHOD(HTTPi_Response
, setCache
)
55 zend_bool do_cache
= 0;
56 getObject(httpi_response_object
, obj
);
58 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
62 UPD_PROP(obj
, long, cache
, do_cache
);
67 /* {{{ proto bool HTTPi_Response::getCache()
70 PHP_METHOD(HTTPi_Response
, getCache
)
72 zval
*do_cache
= NULL
;
73 getObject(httpi_response_object
, obj
);
77 do_cache
= GET_PROP(obj
, cache
);
78 RETURN_BOOL(Z_LVAL_P(do_cache
));
82 /* {{{ proto bool HTTPi_Response::setGzip(bool gzip)
85 PHP_METHOD(HTTPi_Response
, setGzip
)
87 zend_bool do_gzip
= 0;
88 getObject(httpi_response_object
, obj
);
90 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
94 UPD_PROP(obj
, long, gzip
, do_gzip
);
99 /* {{{ proto bool HTTPi_Response::getGzip()
102 PHP_METHOD(HTTPi_Response
, getGzip
)
104 zval
*do_gzip
= NULL
;
105 getObject(httpi_response_object
, obj
);
109 do_gzip
= GET_PROP(obj
, gzip
);
110 RETURN_BOOL(Z_LVAL_P(do_gzip
));
114 /* {{{ proto bool HTTPi_Response::setCacheControl(string control[, bool raw = false])
117 PHP_METHOD(HTTPi_Response
, setCacheControl
)
122 getObject(httpi_response_object
, obj
);
124 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
128 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
129 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
133 UPD_PROP(obj
, long, raw_cache_header
, raw
);
134 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
139 /* {{{ proto string HTTPi_Response::getCacheControl()
142 PHP_METHOD(HTTPi_Response
, getCacheControl
)
145 getObject(httpi_response_object
, obj
);
149 ccontrol
= GET_PROP(obj
, cacheControl
);
150 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
154 /* {{{ proto bool HTTPi::setContentType(string content_type)
157 PHP_METHOD(HTTPi_Response
, setContentType
)
161 getObject(httpi_response_object
, obj
);
163 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
167 if (!strchr(ctype
, '/')) {
168 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
169 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
173 UPD_PROP(obj
, string
, contentType
, ctype
);
179 /* {{{ proto string HTTPi_Response::getContentType()
182 PHP_METHOD(HTTPi_Response
, getContentType
)
185 getObject(httpi_response_object
, obj
);
189 ctype
= GET_PROP(obj
, contentType
);
190 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
194 /* {{{ proto bool HTTPi_Response::setContentDisposition(string filename[, bool inline = false])
197 PHP_METHOD(HTTPi_Response
, setContentDisposition
)
201 zend_bool is_inline
= 0;
202 getObject(httpi_response_object
, obj
);
204 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
208 UPD_PROP(obj
, string
, dispoFile
, file
);
209 UPD_PROP(obj
, long, dispoInline
, is_inline
);
214 /* {{{ proto array HTTPi_Response::getContentDisposition()
217 PHP_METHOD(HTTPi_Response
, getContentDisposition
)
221 getObject(httpi_response_object
, obj
);
223 if (ZEND_NUM_ARGS()) {
227 file
= GET_PROP(obj
, dispoFile
);
228 is_inline
= GET_PROP(obj
, dispoInline
);
230 array_init(return_value
);
231 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
232 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
236 /* {{{ proto bool HTTPi_Response::setETag(string etag)
239 PHP_METHOD(HTTPi_Response
, setETag
)
243 getObject(httpi_response_object
, obj
);
245 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
249 UPD_PROP(obj
, string
, eTag
, etag
);
254 /* {{{ proto string HTTPi_Response::getETag()
257 PHP_METHOD(HTTPi_Response
, getETag
)
260 getObject(httpi_response_object
, obj
);
264 etag
= GET_PROP(obj
, eTag
);
265 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
269 /* {{{ proto bool HTTPi_Response::setData(string data)
272 PHP_METHOD(HTTPi_Response
, setData
)
276 getObject(httpi_response_object
, obj
);
278 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_data
)) {
282 convert_to_string_ex(&the_data
);
283 SET_PROP(obj
, data
, the_data
);
284 UPD_PROP(obj
, long, lastModified
, http_lmod(the_data
, SEND_DATA
));
285 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
290 /* {{{ proto string HTTPi_Response::getData()
293 PHP_METHOD(HTTPi_Response
, getData
)
296 getObject(httpi_response_object
, obj
);
300 the_data
= GET_PROP(obj
, data
);
301 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
305 /* {{{ proto bool HTTPi_Response::setStream(resource stream)
308 PHP_METHOD(HTTPi_Response
, setStream
)
311 php_stream
*the_real_stream
;
313 getObject(httpi_response_object
, obj
);
315 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
319 php_stream_from_zval(the_real_stream
, &the_stream
);
321 SET_PROP(obj
, stream
, the_stream
);
322 UPD_PROP(obj
, long, lastModified
, http_lmod(the_real_stream
, SEND_RSRC
));
323 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
328 /* {{{ proto resource HTTPi_Response::getStream()
331 PHP_METHOD(HTTPi_Response
, getStream
)
334 getObject(httpi_response_object
, obj
);
338 the_stream
= GET_PROP(obj
, stream
);
339 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
343 /* {{{ proto bool HTTPi_Response::setFile(string file)
346 PHP_METHOD(HTTPi_Response
, setFile
)
349 getObject(httpi_response_object
, obj
);
351 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
355 convert_to_string_ex(&the_file
);
357 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
358 UPD_PROP(obj
, long, lastModified
, http_lmod(the_file
, -1));
359 UPD_PROP(obj
, long, send_mode
, -1);
364 /* {{{ proto string HTTPi_Response::getFile()
367 PHP_METHOD(HTTPi_Response
, getFile
)
370 getObject(httpi_response_object
, obj
);
374 the_file
= GET_PROP(obj
, file
);
375 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
379 PHP_METHOD(HTTPi_Response
, send
)
381 zval
*do_cache
, *do_gzip
;
382 getObject(httpi_response_object
, obj
);
384 do_cache
= GET_PROP(obj
, cache
);
385 do_gzip
= GET_PROP(obj
, gzip
);
388 if (Z_LVAL_P(do_cache
)) {
389 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
391 etag
= GET_PROP(obj
, eTag
);
392 lmod
= GET_PROP(obj
, lastModified
);
393 cctrl
= GET_PROP(obj
, cacheControl
);
394 ccraw
= GET_PROP(obj
, raw_cache_header
);
396 if (Z_LVAL_P(ccraw
)) {
397 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
398 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
));
400 char cc_header
[42] = {0};
401 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
402 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_header
, strlen(cc_header
));
403 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_header
, strlen(cc_header
));
408 if (Z_LVAL_P(do_gzip
)) {
414 zval
*ctype
= GET_PROP(obj
, contentType
);
415 if (Z_STRLEN_P(ctype
)) {
416 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
418 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
422 /* content disposition */
424 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
425 if (Z_STRLEN_P(dispo_file
)) {
426 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
427 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), Z_LVAL_P(dispo_inline
));
433 zval
*send_mode
= GET_PROP(obj
, send_mode
);
434 switch (Z_LVAL_P(send_mode
))
438 RETURN_SUCCESS(http_send_data(GET_PROP(obj
, data
)));
443 php_stream
*the_real_stream
;
444 zval
*the_stream
= GET_PROP(obj
, stream
);
445 php_stream_from_zval(the_real_stream
, &the_stream
);
446 RETURN_SUCCESS(http_send_stream(the_real_stream
));
451 RETURN_SUCCESS(http_send_file(GET_PROP(obj
, file
)));
459 #ifdef HTTP_HAVE_CURL
460 /* {{{ HTTPi_Request */
462 /* {{{ proto void HTTPi_Request::__construct([string url[, long request_method = HTTP_GET]])
465 PHP_METHOD(HTTPi_Request
, __construct
)
470 zval
*info
, *opts
, *resp
;
471 getObject(httpi_request_object
, obj
);
473 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
477 INIT_PARR(obj
, options
);
478 INIT_PARR(obj
, responseInfo
);
479 INIT_PARR(obj
, responseData
);
480 INIT_PARR(obj
, postData
);
481 INIT_PARR(obj
, postFiles
);
484 UPD_PROP(obj
, string
, url
, URL
);
487 UPD_PROP(obj
, long, method
, meth
);
492 /* {{{ proto void HTTPi_Request::__destruct()
495 PHP_METHOD(HTTPi_Request
, __destruct
)
497 getObject(httpi_request_object
, obj
);
501 FREE_PARR(obj
, options
);
502 FREE_PARR(obj
, responseInfo
);
503 FREE_PARR(obj
, responseData
);
504 FREE_PARR(obj
, postData
);
505 FREE_PARR(obj
, postFiles
);
509 /* {{{ proto bool HTTPi_Request::setOptions(array options)
512 PHP_METHOD(HTTPi_Request
, setOptions
)
514 zval
*opts
, *old_opts
, **opt
;
515 getObject(httpi_request_object
, obj
);
517 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
521 old_opts
= GET_PROP(obj
, options
);
523 /* headers and cookies need extra attention -- thus cannot use zend_hash_merge() or php_array_merge() directly */
524 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(opts
));
525 zend_hash_get_current_data(Z_ARRVAL_P(opts
), (void **) &opt
) == SUCCESS
;
526 zend_hash_move_forward(Z_ARRVAL_P(opts
))) {
529 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(Z_ARRVAL_P(opts
), &key
, &idx
, 0)) {
530 if (!strcmp(key
, "headers")) {
532 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
533 array_merge(*opt
, *headers
);
536 } else if (!strcmp(key
, "cookies")) {
538 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
539 array_merge(*opt
, *cookies
);
544 add_assoc_zval(old_opts
, key
, *opt
);
551 /* {{{ proto array HTTPi_Request::getOptions()
554 PHP_METHOD(HTTPi_Request
, getOptions
)
557 getObject(httpi_request_object
, obj
);
561 opts
= GET_PROP(obj
, options
);
562 array_init(return_value
);
563 array_copy(opts
, return_value
);
567 /* {{{ proto bool HTTPi_Request::setURL(string url)
570 PHP_METHOD(HTTPi_Request
, setURL
)
574 getObject(httpi_request_object
, obj
);
576 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
580 UPD_PROP(obj
, string
, url
, URL
);
585 /* {{{ proto string HTTPi_Request::getUrl()
588 PHP_METHOD(HTTPi_Request
, getURL
)
591 getObject(httpi_request_object
, obj
);
595 URL
= GET_PROP(obj
, url
);
596 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
600 /* {{{ proto bool HTTPi_Request::setMethod(long request_method)
603 PHP_METHOD(HTTPi_Request
, setMethod
)
606 getObject(httpi_request_object
, obj
);
608 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
612 UPD_PROP(obj
, long, method
, meth
);
617 /* {{{ proto long HTTPi_Request::getMethod()
620 PHP_METHOD(HTTPi_Request
, getMethod
)
623 getObject(httpi_request_object
, obj
);
627 meth
= GET_PROP(obj
, method
);
628 RETURN_LONG(Z_LVAL_P(meth
));
632 /* {{{ proto bool HTTPi_Request::setContentType(string content_type)
635 PHP_METHOD(HTTPi_Request
, setContentType
)
639 getObject(httpi_request_object
, obj
);
641 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
645 if (!strchr(ctype
, '/')) {
646 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
647 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
652 UPD_PROP(obj
, string
, contentType
, ctype
);
657 /* {{{ proto string HTTPi_Request::getContentType()
660 PHP_METHOD(HTTPi_Request
, getContentType
)
663 getObject(httpi_request_object
, obj
);
667 ctype
= GET_PROP(obj
, contentType
);
668 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
672 /* {{{ proto bool HTTPi_Request::setQueryData(mixed query_data)
675 PHP_METHOD(HTTPi_Request
, setQueryData
)
678 getObject(httpi_request_object
, obj
);
680 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
684 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
685 smart_str qstr
= {0};
686 HTTP_URL_ARGSEP_OVERRIDE
;
687 if (SUCCESS
!= php_url_encode_hash_ex(HASH_OF(qdata
), &qstr
, NULL
, 0, NULL
, 0, NULL
, 0, NULL TSRMLS_CC
)) {
688 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't encode query data");
692 HTTP_URL_ARGSEP_RESTORE
;
695 HTTP_URL_ARGSEP_RESTORE
;
697 UPD_PROP(obj
, string
, queryData
, qstr
.c
);
702 convert_to_string(qdata
);
703 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
708 /* {{{ proto string HTTPi_Request::getQueryData()
711 PHP_METHOD(HTTPi_Request
, getQueryData
)
714 getObject(httpi_request_object
, obj
);
718 qdata
= GET_PROP(obj
, queryData
);
719 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
723 /* {{{ proto bool HTTPi_Request::addQueryData(array query_params)
726 PHP_METHOD(HTTPi_Request
, addQueryData
)
728 zval
*qdata
, *old_qdata
;
729 smart_str qstr
= {0};
731 getObject(httpi_request_object
, obj
);
733 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
737 old_qdata
= GET_PROP(obj
, queryData
);
738 if (Z_STRLEN_P(old_qdata
)) {
739 smart_str_appendl(&qstr
, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
));
742 HTTP_URL_ARGSEP_OVERRIDE
;
743 if (SUCCESS
!= php_url_encode_hash_ex(HASH_OF(qdata
), &qstr
, NULL
, 0, NULL
, 0, NULL
, 0, NULL TSRMLS_CC
)) {
744 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't encode query data");
748 HTTP_URL_ARGSEP_RESTORE
;
751 HTTP_URL_ARGSEP_RESTORE
;
755 UPD_PROP(obj
, string
, queryData
, qstr
.c
);
761 /* {{{ proto void HTTPi_Request::unsetQueryData()
764 PHP_METHOD(HTTPi_Request
, unsetQueryData
)
766 getObject(httpi_request_object
, obj
);
770 UPD_PROP(obj
, string
, queryData
, "");
774 /* {{{ proto bool HTTPi_Request::addPostData(array post_data)
777 PHP_METHOD(HTTPi_Request
, addPostData
)
779 zval
*post
, *post_data
;
780 getObject(httpi_request_object
, obj
);
782 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
786 post
= GET_PROP(obj
, postData
);
787 array_merge(post_data
, post
);
793 /* {{{ proto bool HTTPi_Request::setPostData(array post_data)
796 PHP_METHOD(HTTPi_Request
, setPostData
)
798 zval
*post
, *post_data
;
799 getObject(httpi_request_object
, obj
);
801 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
805 post
= GET_PROP(obj
, postData
);
806 zend_hash_clean(Z_ARRVAL_P(post
));
807 array_copy(post_data
, post
);
813 /* {{{ proto array HTTPi_Request::getPostData()
816 PHP_METHOD(HTTPi_Request
, getPostData
)
819 getObject(httpi_request_object
, obj
);
823 post_data
= GET_PROP(obj
, postData
);
824 array_init(return_value
);
825 array_copy(post_data
, return_value
);
829 /* {{{ proto void HTTPi_Request::unsetPostData()
832 PHP_METHOD(HTTPi_Request
, unsetPostData
)
835 getObject(httpi_request_object
, obj
);
839 post_data
= GET_PROP(obj
, postData
);
840 zend_hash_clean(Z_ARRVAL_P(post_data
));
844 /* {{{ proto bool HTTPi_Request::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
847 PHP_METHOD(HTTPi_Request
, addPostFile
)
850 char *name
, *file
, *type
= NULL
;
851 int name_len
, file_len
, type_len
= 0;
852 getObject(httpi_request_object
, obj
);
854 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
859 if (!strchr(type
, '/')) {
860 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
864 type
= "application/x-octetstream";
865 type_len
= sizeof("application/x-octetstream") - 1;
868 MAKE_STD_ZVAL(entry
);
871 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
872 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
873 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
875 files
= GET_PROP(obj
, postFiles
);
876 add_next_index_zval(files
, entry
);
882 /* {{{ proto array HTTPi_Request::getPostFiles()
885 PHP_METHOD(HTTPi_Request
, getPostFiles
)
888 getObject(httpi_request_object
, obj
);
892 files
= GET_PROP(obj
, postFiles
);
894 array_init(return_value
);
895 array_copy(files
, return_value
);
899 /* {{{ proto void HTTPi_Request::unsetPostFiles()
902 PHP_METHOD(HTTPi_Request
, unsetPostFiles
)
905 getObject(httpi_request_object
, obj
);
909 files
= GET_PROP(obj
, postFiles
);
910 zend_hash_clean(Z_ARRVAL_P(files
));
914 /* {{{ proto array HTTPi_Request::getResponseData()
917 PHP_METHOD(HTTPi_Request
, getResponseData
)
920 getObject(httpi_request_object
, obj
);
924 data
= GET_PROP(obj
, responseData
);
925 array_init(return_value
);
926 array_copy(data
, return_value
);
930 /* {{{ proto array HTTPi_Request::getResponseHeaders()
933 PHP_METHOD(HTTPi_Request
, getResponseHeaders
)
935 zval
*data
, **headers
;
936 getObject(httpi_request_object
, obj
);
940 array_init(return_value
);
941 data
= GET_PROP(obj
, responseData
);
942 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
943 array_copy(*headers
, return_value
);
948 /* {{{ proto string HTTPi_Request::getResponseBody()
951 PHP_METHOD(HTTPi_Request
, getResponseBody
)
954 getObject(httpi_request_object
, obj
);
958 data
= GET_PROP(obj
, responseData
);
959 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
960 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
962 Z_TYPE_P(return_value
) = IS_NULL
;
967 /* {{{ proto array HTTPi_Request::getResponseInfo()
970 PHP_METHOD(HTTPi_Request
, getResponseInfo
)
973 getObject(httpi_request_object
, obj
);
977 info
= GET_PROP(obj
, responseInfo
);
978 array_init(return_value
);
979 array_copy(info
, return_value
);
983 /* {{{ proto bool HTTPi_Request::send()
986 PHP_METHOD(HTTPi_Request
, send
)
988 STATUS status
= FAILURE
;
989 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
990 char *response_data
, *request_uri
;
992 getObject(httpi_request_object
, obj
);
996 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
997 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initilaize cURL");
1001 meth
= GET_PROP(obj
, method
);
1002 URL
= GET_PROP(obj
, url
);
1003 qdata
= GET_PROP(obj
, queryData
);
1004 opts
= GET_PROP(obj
, options
);
1005 info
= GET_PROP(obj
, responseInfo
);
1006 resp
= GET_PROP(obj
, responseData
);
1008 // HTTP_URI_MAXLEN+1 big char *
1009 request_uri
= http_absolute_uri(Z_STRVAL_P(URL
), NULL
);
1011 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
1012 if (!strchr(request_uri
, '?')) {
1013 strcat(request_uri
, "?");
1015 strcat(request_uri
, "&");
1017 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
1020 switch (Z_LVAL_P(meth
))
1023 status
= http_get_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1027 status
= http_head_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1032 zval
*post_files
, *post_data
, **data
;
1034 post_files
= GET_PROP(obj
, postFiles
);
1035 post_data
= GET_PROP(obj
, postData
);
1037 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files
))) {
1039 /* urlencoded post */
1040 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
);
1047 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
1050 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(post_data
));
1051 zend_hash_get_current_data(Z_ARRVAL_P(post_data
), (void **) &data
) == SUCCESS
;
1052 zend_hash_move_forward(Z_ARRVAL_P(post_data
))) {
1057 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(Z_ARRVAL_P(post_data
), &key
, &idx
, 0)) {
1058 convert_to_string_ex(data
);
1059 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1060 CURLFORM_COPYNAME
, key
,
1061 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
1062 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
1069 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(post_files
));
1070 zend_hash_get_current_data(Z_ARRVAL_P(post_files
), (void **) &data
) == SUCCESS
;
1071 zend_hash_move_forward(Z_ARRVAL_P(post_files
))) {
1073 zval
**file
, **type
, **name
;
1076 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
1077 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
1078 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)
1081 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1082 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
1083 CURLFORM_FILENAME
, Z_STRVAL_PP(name
),
1084 CURLFORM_FILE
, Z_STRVAL_PP(file
),
1085 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
1091 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
);
1092 curl_formfree(http_post_data
[0]);
1103 /* final data handling */
1104 if (status
!= SUCCESS
) {
1107 zval
*zheaders
, *zbody
;
1109 MAKE_STD_ZVAL(zbody
);
1110 MAKE_STD_ZVAL(zheaders
)
1111 array_init(zheaders
);
1113 if (SUCCESS
!= http_split_response_ex(response_data
, response_len
, zheaders
, zbody
)) {
1114 zval_dtor(zheaders
);
1117 efree(response_data
);
1121 add_assoc_zval(resp
, "headers", zheaders
);
1122 add_assoc_zval(resp
, "body", zbody
);
1124 efree(response_data
);
1132 #endif /* HTTP_HAVE_CURL */
1134 #endif /* ZEND_ENGINE_2 */
1141 * vim600: noet sw=4 ts=4 fdm=marker
1142 * vim<600: noet sw=4 ts=4