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 +----------------------------------------------------------------------+
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
27 #include "php_http_api.h"
28 #include "php_http_curl_api.h"
29 #include "ext/standard/php_smart_str.h"
33 /* {{{ HTTPi_Response */
35 /* {{{ proto void HTTPi_Response::__construct(bool cache, bool gzip)
38 PHP_METHOD(HTTPi_Response
, __construct
)
40 zend_bool do_cache
= 0, do_gzip
= 0;
41 getObject(httpi_response_object
, obj
);
43 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
48 UPD_PROP(obj
, long, cache
, do_cache
);
49 UPD_PROP(obj
, long, gzip
, do_gzip
);
53 /* {{{ proto bool HTTPi_Response::setCache(bool cache)
56 PHP_METHOD(HTTPi_Response
, setCache
)
58 zend_bool do_cache
= 0;
59 getObject(httpi_response_object
, obj
);
61 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
65 UPD_PROP(obj
, long, cache
, do_cache
);
70 /* {{{ proto bool HTTPi_Response::getCache()
73 PHP_METHOD(HTTPi_Response
, getCache
)
75 zval
*do_cache
= NULL
;
76 getObject(httpi_response_object
, obj
);
80 do_cache
= GET_PROP(obj
, cache
);
81 RETURN_BOOL(Z_LVAL_P(do_cache
));
85 /* {{{ proto bool HTTPi_Response::setGzip(bool gzip)
88 PHP_METHOD(HTTPi_Response
, setGzip
)
90 zend_bool do_gzip
= 0;
91 getObject(httpi_response_object
, obj
);
93 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
97 UPD_PROP(obj
, long, gzip
, do_gzip
);
102 /* {{{ proto bool HTTPi_Response::getGzip()
105 PHP_METHOD(HTTPi_Response
, getGzip
)
107 zval
*do_gzip
= NULL
;
108 getObject(httpi_response_object
, obj
);
112 do_gzip
= GET_PROP(obj
, gzip
);
113 RETURN_BOOL(Z_LVAL_P(do_gzip
));
117 /* {{{ proto bool HTTPi_Response::setCacheControl(string control[, bool raw = false])
120 PHP_METHOD(HTTPi_Response
, setCacheControl
)
125 getObject(httpi_response_object
, obj
);
127 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
131 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
132 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
136 UPD_PROP(obj
, long, raw_cache_header
, raw
);
137 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
142 /* {{{ proto string HTTPi_Response::getCacheControl()
145 PHP_METHOD(HTTPi_Response
, getCacheControl
)
148 getObject(httpi_response_object
, obj
);
152 ccontrol
= GET_PROP(obj
, cacheControl
);
153 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
157 /* {{{ proto bool HTTPi::setContentType(string content_type)
160 PHP_METHOD(HTTPi_Response
, setContentType
)
164 getObject(httpi_response_object
, obj
);
166 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
170 if (!strchr(ctype
, '/')) {
171 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
172 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
176 UPD_PROP(obj
, string
, contentType
, ctype
);
182 /* {{{ proto string HTTPi_Response::getContentType()
185 PHP_METHOD(HTTPi_Response
, getContentType
)
188 getObject(httpi_response_object
, obj
);
192 ctype
= GET_PROP(obj
, contentType
);
193 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
197 /* {{{ proto bool HTTPi_Response::setContentDisposition(string filename[, bool inline = false])
200 PHP_METHOD(HTTPi_Response
, setContentDisposition
)
204 zend_bool is_inline
= 0;
205 getObject(httpi_response_object
, obj
);
207 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
211 UPD_PROP(obj
, string
, dispoFile
, file
);
212 UPD_PROP(obj
, long, dispoInline
, is_inline
);
217 /* {{{ proto array HTTPi_Response::getContentDisposition()
220 PHP_METHOD(HTTPi_Response
, getContentDisposition
)
224 getObject(httpi_response_object
, obj
);
226 if (ZEND_NUM_ARGS()) {
230 file
= GET_PROP(obj
, dispoFile
);
231 is_inline
= GET_PROP(obj
, dispoInline
);
233 array_init(return_value
);
234 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
235 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
239 /* {{{ proto bool HTTPi_Response::setETag(string etag)
242 PHP_METHOD(HTTPi_Response
, setETag
)
246 getObject(httpi_response_object
, obj
);
248 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
252 UPD_PROP(obj
, string
, eTag
, etag
);
257 /* {{{ proto string HTTPi_Response::getETag()
260 PHP_METHOD(HTTPi_Response
, getETag
)
263 getObject(httpi_response_object
, obj
);
267 etag
= GET_PROP(obj
, eTag
);
268 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
272 /* {{{ proto bool HTTPi_Response::setData(string data)
275 PHP_METHOD(HTTPi_Response
, setData
)
279 getObject(httpi_response_object
, obj
);
281 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_data
)) {
285 convert_to_string_ex(&the_data
);
286 SET_PROP(obj
, data
, the_data
);
287 UPD_PROP(obj
, long, lastModified
, http_lmod(the_data
, SEND_DATA
));
288 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
293 /* {{{ proto string HTTPi_Response::getData()
296 PHP_METHOD(HTTPi_Response
, getData
)
299 getObject(httpi_response_object
, obj
);
303 the_data
= GET_PROP(obj
, data
);
304 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
308 /* {{{ proto bool HTTPi_Response::setStream(resource stream)
311 PHP_METHOD(HTTPi_Response
, setStream
)
314 php_stream
*the_real_stream
;
316 getObject(httpi_response_object
, obj
);
318 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
322 php_stream_from_zval(the_real_stream
, &the_stream
);
324 SET_PROP(obj
, stream
, the_stream
);
325 UPD_PROP(obj
, long, lastModified
, http_lmod(the_real_stream
, SEND_RSRC
));
326 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
331 /* {{{ proto resource HTTPi_Response::getStream()
334 PHP_METHOD(HTTPi_Response
, getStream
)
337 getObject(httpi_response_object
, obj
);
341 the_stream
= GET_PROP(obj
, stream
);
342 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
346 /* {{{ proto bool HTTPi_Response::setFile(string file)
349 PHP_METHOD(HTTPi_Response
, setFile
)
352 getObject(httpi_response_object
, obj
);
354 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
358 convert_to_string_ex(&the_file
);
360 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
361 UPD_PROP(obj
, long, lastModified
, http_lmod(the_file
, -1));
362 UPD_PROP(obj
, long, send_mode
, -1);
367 /* {{{ proto string HTTPi_Response::getFile()
370 PHP_METHOD(HTTPi_Response
, getFile
)
373 getObject(httpi_response_object
, obj
);
377 the_file
= GET_PROP(obj
, file
);
378 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
382 PHP_METHOD(HTTPi_Response
, send
)
384 zval
*do_cache
, *do_gzip
;
385 getObject(httpi_response_object
, obj
);
387 do_cache
= GET_PROP(obj
, cache
);
388 do_gzip
= GET_PROP(obj
, gzip
);
391 if (Z_LVAL_P(do_cache
)) {
392 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
394 etag
= GET_PROP(obj
, eTag
);
395 lmod
= GET_PROP(obj
, lastModified
);
396 cctrl
= GET_PROP(obj
, cacheControl
);
397 ccraw
= GET_PROP(obj
, raw_cache_header
);
399 if (Z_LVAL_P(ccraw
)) {
400 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
401 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
));
403 char cc_header
[42] = {0};
404 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
405 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_header
, strlen(cc_header
));
406 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_header
, strlen(cc_header
));
411 if (Z_LVAL_P(do_gzip
)) {
417 zval
*ctype
= GET_PROP(obj
, contentType
);
418 if (Z_STRLEN_P(ctype
)) {
419 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
421 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
425 /* content disposition */
427 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
428 if (Z_STRLEN_P(dispo_file
)) {
429 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
430 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), Z_LVAL_P(dispo_inline
));
436 zval
*send_mode
= GET_PROP(obj
, send_mode
);
437 switch (Z_LVAL_P(send_mode
))
441 RETURN_SUCCESS(http_send_data(GET_PROP(obj
, data
)));
446 php_stream
*the_real_stream
;
447 zval
*the_stream
= GET_PROP(obj
, stream
);
448 php_stream_from_zval(the_real_stream
, &the_stream
);
449 RETURN_SUCCESS(http_send_stream(the_real_stream
));
454 RETURN_SUCCESS(http_send_file(GET_PROP(obj
, file
)));
462 #ifdef HTTP_HAVE_CURL
463 /* {{{ HTTPi_Request */
465 /* {{{ proto void HTTPi_Request::__construct([string url[, long request_method = HTTP_GET]])
468 PHP_METHOD(HTTPi_Request
, __construct
)
473 zval
*info
, *opts
, *resp
;
474 getObject(httpi_request_object
, obj
);
476 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
480 INIT_PARR(obj
, options
);
481 INIT_PARR(obj
, responseInfo
);
482 INIT_PARR(obj
, responseData
);
483 INIT_PARR(obj
, postData
);
484 INIT_PARR(obj
, postFiles
);
487 UPD_PROP(obj
, string
, url
, URL
);
490 UPD_PROP(obj
, long, method
, meth
);
495 /* {{{ proto void HTTPi_Request::__destruct()
498 PHP_METHOD(HTTPi_Request
, __destruct
)
500 getObject(httpi_request_object
, obj
);
504 FREE_PARR(obj
, options
);
505 FREE_PARR(obj
, responseInfo
);
506 FREE_PARR(obj
, responseData
);
507 FREE_PARR(obj
, postData
);
508 FREE_PARR(obj
, postFiles
);
512 /* {{{ proto bool HTTPi_Request::setOptions(array options)
515 PHP_METHOD(HTTPi_Request
, setOptions
)
517 zval
*opts
, *old_opts
, **opt
;
518 getObject(httpi_request_object
, obj
);
520 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
524 old_opts
= GET_PROP(obj
, options
);
526 /* headers and cookies need extra attention -- thus cannot use zend_hash_merge() or php_array_merge() directly */
527 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(opts
));
528 zend_hash_get_current_data(Z_ARRVAL_P(opts
), (void **) &opt
) == SUCCESS
;
529 zend_hash_move_forward(Z_ARRVAL_P(opts
))) {
532 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(Z_ARRVAL_P(opts
), &key
, &idx
, 0)) {
533 if (!strcmp(key
, "headers")) {
535 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
536 array_merge(*opt
, *headers
);
539 } else if (!strcmp(key
, "cookies")) {
541 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
542 array_merge(*opt
, *cookies
);
547 add_assoc_zval(old_opts
, key
, *opt
);
554 /* {{{ proto array HTTPi_Request::getOptions()
557 PHP_METHOD(HTTPi_Request
, getOptions
)
560 getObject(httpi_request_object
, obj
);
564 opts
= GET_PROP(obj
, options
);
565 array_init(return_value
);
566 array_copy(opts
, return_value
);
570 /* {{{ proto bool HTTPi_Request::setURL(string url)
573 PHP_METHOD(HTTPi_Request
, setURL
)
577 getObject(httpi_request_object
, obj
);
579 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
583 UPD_PROP(obj
, string
, url
, URL
);
588 /* {{{ proto string HTTPi_Request::getUrl()
591 PHP_METHOD(HTTPi_Request
, getURL
)
594 getObject(httpi_request_object
, obj
);
598 URL
= GET_PROP(obj
, url
);
599 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
603 /* {{{ proto bool HTTPi_Request::setMethod(long request_method)
606 PHP_METHOD(HTTPi_Request
, setMethod
)
609 getObject(httpi_request_object
, obj
);
611 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
615 UPD_PROP(obj
, long, method
, meth
);
620 /* {{{ proto long HTTPi_Request::getMethod()
623 PHP_METHOD(HTTPi_Request
, getMethod
)
626 getObject(httpi_request_object
, obj
);
630 meth
= GET_PROP(obj
, method
);
631 RETURN_LONG(Z_LVAL_P(meth
));
635 /* {{{ proto bool HTTPi_Request::setContentType(string content_type)
638 PHP_METHOD(HTTPi_Request
, setContentType
)
642 getObject(httpi_request_object
, obj
);
644 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
648 if (!strchr(ctype
, '/')) {
649 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
650 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
655 UPD_PROP(obj
, string
, contentType
, ctype
);
660 /* {{{ proto string HTTPi_Request::getContentType()
663 PHP_METHOD(HTTPi_Request
, getContentType
)
666 getObject(httpi_request_object
, obj
);
670 ctype
= GET_PROP(obj
, contentType
);
671 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
675 /* {{{ proto bool HTTPi_Request::setQueryData(mixed query_data)
678 PHP_METHOD(HTTPi_Request
, setQueryData
)
681 getObject(httpi_request_object
, obj
);
683 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
687 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
688 smart_str qstr
= {0};
689 HTTP_URL_ARGSEP_OVERRIDE
;
690 if (SUCCESS
!= php_url_encode_hash_ex(HASH_OF(qdata
), &qstr
, NULL
, 0, NULL
, 0, NULL
, 0, NULL TSRMLS_CC
)) {
691 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't encode query data");
695 HTTP_URL_ARGSEP_RESTORE
;
698 HTTP_URL_ARGSEP_RESTORE
;
700 UPD_PROP(obj
, string
, queryData
, qstr
.c
);
705 convert_to_string(qdata
);
706 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
711 /* {{{ proto string HTTPi_Request::getQueryData()
714 PHP_METHOD(HTTPi_Request
, getQueryData
)
717 getObject(httpi_request_object
, obj
);
721 qdata
= GET_PROP(obj
, queryData
);
722 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
726 /* {{{ proto bool HTTPi_Request::addQueryData(array query_params)
729 PHP_METHOD(HTTPi_Request
, addQueryData
)
731 zval
*qdata
, *old_qdata
;
732 smart_str qstr
= {0};
734 getObject(httpi_request_object
, obj
);
736 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
740 old_qdata
= GET_PROP(obj
, queryData
);
741 if (Z_STRLEN_P(old_qdata
)) {
742 smart_str_appendl(&qstr
, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
));
745 HTTP_URL_ARGSEP_OVERRIDE
;
746 if (SUCCESS
!= php_url_encode_hash_ex(HASH_OF(qdata
), &qstr
, NULL
, 0, NULL
, 0, NULL
, 0, NULL TSRMLS_CC
)) {
747 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't encode query data");
751 HTTP_URL_ARGSEP_RESTORE
;
754 HTTP_URL_ARGSEP_RESTORE
;
758 UPD_PROP(obj
, string
, queryData
, qstr
.c
);
764 /* {{{ proto void HTTPi_Request::unsetQueryData()
767 PHP_METHOD(HTTPi_Request
, unsetQueryData
)
769 getObject(httpi_request_object
, obj
);
773 UPD_PROP(obj
, string
, queryData
, "");
777 /* {{{ proto bool HTTPi_Request::addPostData(array post_data)
780 PHP_METHOD(HTTPi_Request
, addPostData
)
782 zval
*post
, *post_data
;
783 getObject(httpi_request_object
, obj
);
785 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
789 post
= GET_PROP(obj
, postData
);
790 array_merge(post_data
, post
);
796 /* {{{ proto bool HTTPi_Request::setPostData(array post_data)
799 PHP_METHOD(HTTPi_Request
, setPostData
)
801 zval
*post
, *post_data
;
802 getObject(httpi_request_object
, obj
);
804 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
808 post
= GET_PROP(obj
, postData
);
809 zend_hash_clean(Z_ARRVAL_P(post
));
810 array_copy(post_data
, post
);
816 /* {{{ proto array HTTPi_Request::getPostData()
819 PHP_METHOD(HTTPi_Request
, getPostData
)
822 getObject(httpi_request_object
, obj
);
826 post_data
= GET_PROP(obj
, postData
);
827 array_init(return_value
);
828 array_copy(post_data
, return_value
);
832 /* {{{ proto void HTTPi_Request::unsetPostData()
835 PHP_METHOD(HTTPi_Request
, unsetPostData
)
838 getObject(httpi_request_object
, obj
);
842 post_data
= GET_PROP(obj
, postData
);
843 zend_hash_clean(Z_ARRVAL_P(post_data
));
847 /* {{{ proto bool HTTPi_Request::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
850 PHP_METHOD(HTTPi_Request
, addPostFile
)
853 char *name
, *file
, *type
= NULL
;
854 int name_len
, file_len
, type_len
= 0;
855 getObject(httpi_request_object
, obj
);
857 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
862 if (!strchr(type
, '/')) {
863 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
867 type
= "application/x-octetstream";
868 type_len
= sizeof("application/x-octetstream") - 1;
871 MAKE_STD_ZVAL(entry
);
874 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
875 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
876 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
878 files
= GET_PROP(obj
, postFiles
);
879 add_next_index_zval(files
, entry
);
885 /* {{{ proto array HTTPi_Request::getPostFiles()
888 PHP_METHOD(HTTPi_Request
, getPostFiles
)
891 getObject(httpi_request_object
, obj
);
895 files
= GET_PROP(obj
, postFiles
);
897 array_init(return_value
);
898 array_copy(files
, return_value
);
902 /* {{{ proto void HTTPi_Request::unsetPostFiles()
905 PHP_METHOD(HTTPi_Request
, unsetPostFiles
)
908 getObject(httpi_request_object
, obj
);
912 files
= GET_PROP(obj
, postFiles
);
913 zend_hash_clean(Z_ARRVAL_P(files
));
917 /* {{{ proto array HTTPi_Request::getResponseData()
920 PHP_METHOD(HTTPi_Request
, getResponseData
)
923 getObject(httpi_request_object
, obj
);
927 data
= GET_PROP(obj
, responseData
);
928 array_init(return_value
);
929 array_copy(data
, return_value
);
933 /* {{{ proto array HTTPi_Request::getResponseHeaders()
936 PHP_METHOD(HTTPi_Request
, getResponseHeaders
)
938 zval
*data
, **headers
;
939 getObject(httpi_request_object
, obj
);
943 array_init(return_value
);
944 data
= GET_PROP(obj
, responseData
);
945 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
946 array_copy(*headers
, return_value
);
951 /* {{{ proto string HTTPi_Request::getResponseBody()
954 PHP_METHOD(HTTPi_Request
, getResponseBody
)
957 getObject(httpi_request_object
, obj
);
961 data
= GET_PROP(obj
, responseData
);
962 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
963 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
965 Z_TYPE_P(return_value
) = IS_NULL
;
970 /* {{{ proto array HTTPi_Request::getResponseInfo()
973 PHP_METHOD(HTTPi_Request
, getResponseInfo
)
976 getObject(httpi_request_object
, obj
);
980 info
= GET_PROP(obj
, responseInfo
);
981 array_init(return_value
);
982 array_copy(info
, return_value
);
986 /* {{{ proto bool HTTPi_Request::send()
989 PHP_METHOD(HTTPi_Request
, send
)
991 STATUS status
= FAILURE
;
992 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
993 char *response_data
, *request_uri
;
995 getObject(httpi_request_object
, obj
);
999 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
1000 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initilaize cURL");
1004 meth
= GET_PROP(obj
, method
);
1005 URL
= GET_PROP(obj
, url
);
1006 qdata
= GET_PROP(obj
, queryData
);
1007 opts
= GET_PROP(obj
, options
);
1008 info
= GET_PROP(obj
, responseInfo
);
1009 resp
= GET_PROP(obj
, responseData
);
1011 // HTTP_URI_MAXLEN+1 big char *
1012 request_uri
= http_absolute_uri(Z_STRVAL_P(URL
), NULL
);
1014 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
1015 if (!strchr(request_uri
, '?')) {
1016 strcat(request_uri
, "?");
1018 strcat(request_uri
, "&");
1020 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
1023 switch (Z_LVAL_P(meth
))
1026 status
= http_get_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1030 status
= http_head_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1035 zval
*post_files
, *post_data
, **data
;
1037 post_files
= GET_PROP(obj
, postFiles
);
1038 post_data
= GET_PROP(obj
, postData
);
1040 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files
))) {
1042 /* urlencoded post */
1043 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
);
1050 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
1053 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(post_data
));
1054 zend_hash_get_current_data(Z_ARRVAL_P(post_data
), (void **) &data
) == SUCCESS
;
1055 zend_hash_move_forward(Z_ARRVAL_P(post_data
))) {
1060 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(Z_ARRVAL_P(post_data
), &key
, &idx
, 0)) {
1061 convert_to_string_ex(data
);
1062 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1063 CURLFORM_COPYNAME
, key
,
1064 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
1065 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
1072 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(post_files
));
1073 zend_hash_get_current_data(Z_ARRVAL_P(post_files
), (void **) &data
) == SUCCESS
;
1074 zend_hash_move_forward(Z_ARRVAL_P(post_files
))) {
1076 zval
**file
, **type
, **name
;
1079 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
1080 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
1081 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)
1084 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1085 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
1086 CURLFORM_FILENAME
, Z_STRVAL_PP(name
),
1087 CURLFORM_FILE
, Z_STRVAL_PP(file
),
1088 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
1094 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
);
1095 curl_formfree(http_post_data
[0]);
1106 /* final data handling */
1107 if (status
!= SUCCESS
) {
1110 zval
*zheaders
, *zbody
;
1112 MAKE_STD_ZVAL(zbody
);
1113 MAKE_STD_ZVAL(zheaders
)
1114 array_init(zheaders
);
1116 if (SUCCESS
!= http_split_response_ex(response_data
, response_len
, zheaders
, zbody
)) {
1117 zval_dtor(zheaders
);
1120 efree(response_data
);
1124 add_assoc_zval(resp
, "headers", zheaders
);
1125 add_assoc_zval(resp
, "body", zbody
);
1127 efree(response_data
);
1135 #endif /* HTTP_HAVE_CURL */
1137 #endif /* ZEND_ENGINE_2 */
1144 * vim600: noet sw=4 ts=4 fdm=marker
1145 * vim<600: noet sw=4 ts=4