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 "php_http_std_defs.h"
31 #include "ext/standard/php_smart_str.h"
35 /* {{{ HTTPi_Response */
37 /* {{{ proto void HTTPi_Response::__construct(bool cache, bool gzip)
40 PHP_METHOD(HTTPi_Response
, __construct
)
42 zend_bool do_cache
= 0, do_gzip
= 0;
43 getObject(httpi_response_object
, obj
);
45 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
50 UPD_PROP(obj
, long, cache
, do_cache
);
51 UPD_PROP(obj
, long, gzip
, do_gzip
);
55 /* {{{ proto bool HTTPi_Response::setCache(bool cache)
58 PHP_METHOD(HTTPi_Response
, setCache
)
60 zend_bool do_cache
= 0;
61 getObject(httpi_response_object
, obj
);
63 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
67 UPD_PROP(obj
, long, cache
, do_cache
);
72 /* {{{ proto bool HTTPi_Response::getCache()
75 PHP_METHOD(HTTPi_Response
, getCache
)
77 zval
*do_cache
= NULL
;
78 getObject(httpi_response_object
, obj
);
82 do_cache
= GET_PROP(obj
, cache
);
83 RETURN_BOOL(Z_LVAL_P(do_cache
));
87 /* {{{ proto bool HTTPi_Response::setGzip(bool gzip)
90 PHP_METHOD(HTTPi_Response
, setGzip
)
92 zend_bool do_gzip
= 0;
93 getObject(httpi_response_object
, obj
);
95 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
99 UPD_PROP(obj
, long, gzip
, do_gzip
);
104 /* {{{ proto bool HTTPi_Response::getGzip()
107 PHP_METHOD(HTTPi_Response
, getGzip
)
109 zval
*do_gzip
= NULL
;
110 getObject(httpi_response_object
, obj
);
114 do_gzip
= GET_PROP(obj
, gzip
);
115 RETURN_BOOL(Z_LVAL_P(do_gzip
));
119 /* {{{ proto bool HTTPi_Response::setCacheControl(string control[, bool raw = false])
122 PHP_METHOD(HTTPi_Response
, setCacheControl
)
127 getObject(httpi_response_object
, obj
);
129 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
133 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
134 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
138 UPD_PROP(obj
, long, raw_cache_header
, raw
);
139 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
144 /* {{{ proto string HTTPi_Response::getCacheControl()
147 PHP_METHOD(HTTPi_Response
, getCacheControl
)
150 getObject(httpi_response_object
, obj
);
154 ccontrol
= GET_PROP(obj
, cacheControl
);
155 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
159 /* {{{ proto bool HTTPi::setContentType(string content_type)
162 PHP_METHOD(HTTPi_Response
, setContentType
)
166 getObject(httpi_response_object
, obj
);
168 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
172 if (!strchr(ctype
, '/')) {
173 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
174 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
178 UPD_PROP(obj
, string
, contentType
, ctype
);
184 /* {{{ proto string HTTPi_Response::getContentType()
187 PHP_METHOD(HTTPi_Response
, getContentType
)
190 getObject(httpi_response_object
, obj
);
194 ctype
= GET_PROP(obj
, contentType
);
195 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
199 /* {{{ proto bool HTTPi_Response::setContentDisposition(string filename[, bool inline = false])
202 PHP_METHOD(HTTPi_Response
, setContentDisposition
)
206 zend_bool is_inline
= 0;
207 getObject(httpi_response_object
, obj
);
209 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
213 UPD_PROP(obj
, string
, dispoFile
, file
);
214 UPD_PROP(obj
, long, dispoInline
, is_inline
);
219 /* {{{ proto array HTTPi_Response::getContentDisposition()
222 PHP_METHOD(HTTPi_Response
, getContentDisposition
)
226 getObject(httpi_response_object
, obj
);
228 if (ZEND_NUM_ARGS()) {
232 file
= GET_PROP(obj
, dispoFile
);
233 is_inline
= GET_PROP(obj
, dispoInline
);
235 array_init(return_value
);
236 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
237 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
241 /* {{{ proto bool HTTPi_Response::setETag(string etag)
244 PHP_METHOD(HTTPi_Response
, setETag
)
248 getObject(httpi_response_object
, obj
);
250 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
254 UPD_PROP(obj
, string
, eTag
, etag
);
259 /* {{{ proto string HTTPi_Response::getETag()
262 PHP_METHOD(HTTPi_Response
, getETag
)
265 getObject(httpi_response_object
, obj
);
269 etag
= GET_PROP(obj
, eTag
);
270 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
274 /* {{{ proto bool HTTPi_Response::setData(string data)
277 PHP_METHOD(HTTPi_Response
, setData
)
281 getObject(httpi_response_object
, obj
);
283 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_data
)) {
287 convert_to_string_ex(&the_data
);
288 SET_PROP(obj
, data
, the_data
);
289 UPD_PROP(obj
, long, lastModified
, http_lmod(the_data
, SEND_DATA
));
290 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
295 /* {{{ proto string HTTPi_Response::getData()
298 PHP_METHOD(HTTPi_Response
, getData
)
301 getObject(httpi_response_object
, obj
);
305 the_data
= GET_PROP(obj
, data
);
306 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
310 /* {{{ proto bool HTTPi_Response::setStream(resource stream)
313 PHP_METHOD(HTTPi_Response
, setStream
)
316 php_stream
*the_real_stream
;
318 getObject(httpi_response_object
, obj
);
320 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
324 php_stream_from_zval(the_real_stream
, &the_stream
);
326 SET_PROP(obj
, stream
, the_stream
);
327 UPD_PROP(obj
, long, lastModified
, http_lmod(the_real_stream
, SEND_RSRC
));
328 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
333 /* {{{ proto resource HTTPi_Response::getStream()
336 PHP_METHOD(HTTPi_Response
, getStream
)
339 getObject(httpi_response_object
, obj
);
343 the_stream
= GET_PROP(obj
, stream
);
344 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
348 /* {{{ proto bool HTTPi_Response::setFile(string file)
351 PHP_METHOD(HTTPi_Response
, setFile
)
354 getObject(httpi_response_object
, obj
);
356 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
360 convert_to_string_ex(&the_file
);
362 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
363 UPD_PROP(obj
, long, lastModified
, http_lmod(the_file
, -1));
364 UPD_PROP(obj
, long, send_mode
, -1);
369 /* {{{ proto string HTTPi_Response::getFile()
372 PHP_METHOD(HTTPi_Response
, getFile
)
375 getObject(httpi_response_object
, obj
);
379 the_file
= GET_PROP(obj
, file
);
380 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
384 PHP_METHOD(HTTPi_Response
, send
)
386 zval
*do_cache
, *do_gzip
;
387 getObject(httpi_response_object
, obj
);
389 do_cache
= GET_PROP(obj
, cache
);
390 do_gzip
= GET_PROP(obj
, gzip
);
393 if (Z_LVAL_P(do_cache
)) {
394 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
396 etag
= GET_PROP(obj
, eTag
);
397 lmod
= GET_PROP(obj
, lastModified
);
398 cctrl
= GET_PROP(obj
, cacheControl
);
399 ccraw
= GET_PROP(obj
, raw_cache_header
);
401 if (Z_LVAL_P(ccraw
)) {
402 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
403 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
));
405 char cc_header
[42] = {0};
406 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
407 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_header
, strlen(cc_header
));
408 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_header
, strlen(cc_header
));
413 if (Z_LVAL_P(do_gzip
)) {
419 zval
*ctype
= GET_PROP(obj
, contentType
);
420 if (Z_STRLEN_P(ctype
)) {
421 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
423 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
427 /* content disposition */
429 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
430 if (Z_STRLEN_P(dispo_file
)) {
431 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
432 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), Z_LVAL_P(dispo_inline
));
438 zval
*send_mode
= GET_PROP(obj
, send_mode
);
439 switch (Z_LVAL_P(send_mode
))
443 RETURN_SUCCESS(http_send_data(GET_PROP(obj
, data
)));
448 php_stream
*the_real_stream
;
449 zval
*the_stream
= GET_PROP(obj
, stream
);
450 php_stream_from_zval(the_real_stream
, &the_stream
);
451 RETURN_SUCCESS(http_send_stream(the_real_stream
));
456 RETURN_SUCCESS(http_send_file(GET_PROP(obj
, file
)));
464 #ifdef HTTP_HAVE_CURL
465 /* {{{ HTTPi_Request */
467 /* {{{ proto void HTTPi_Request::__construct([string url[, long request_method = HTTP_GET]])
470 PHP_METHOD(HTTPi_Request
, __construct
)
475 zval
*info
, *opts
, *resp
;
476 getObject(httpi_request_object
, obj
);
478 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
482 INIT_PARR(obj
, options
);
483 INIT_PARR(obj
, responseInfo
);
484 INIT_PARR(obj
, responseData
);
485 INIT_PARR(obj
, postData
);
486 INIT_PARR(obj
, postFiles
);
489 UPD_PROP(obj
, string
, url
, URL
);
492 UPD_PROP(obj
, long, method
, meth
);
497 /* {{{ proto void HTTPi_Request::__destruct()
500 PHP_METHOD(HTTPi_Request
, __destruct
)
502 getObject(httpi_request_object
, obj
);
506 FREE_PARR(obj
, options
);
507 FREE_PARR(obj
, responseInfo
);
508 FREE_PARR(obj
, responseData
);
509 FREE_PARR(obj
, postData
);
510 FREE_PARR(obj
, postFiles
);
514 /* {{{ proto bool HTTPi_Request::setOptions(array options)
517 PHP_METHOD(HTTPi_Request
, setOptions
)
521 zval
*opts
, *old_opts
, **opt
;
522 getObject(httpi_request_object
, obj
);
524 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
528 old_opts
= GET_PROP(obj
, options
);
530 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
531 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
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
);
558 /* {{{ proto array HTTPi_Request::getOptions()
561 PHP_METHOD(HTTPi_Request
, getOptions
)
564 getObject(httpi_request_object
, obj
);
568 opts
= GET_PROP(obj
, options
);
569 array_init(return_value
);
570 array_copy(opts
, return_value
);
574 /* {{{ proto bool HTTPi_Request::setURL(string url)
577 PHP_METHOD(HTTPi_Request
, setURL
)
581 getObject(httpi_request_object
, obj
);
583 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
587 UPD_PROP(obj
, string
, url
, URL
);
592 /* {{{ proto string HTTPi_Request::getUrl()
595 PHP_METHOD(HTTPi_Request
, getURL
)
598 getObject(httpi_request_object
, obj
);
602 URL
= GET_PROP(obj
, url
);
603 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
607 /* {{{ proto bool HTTPi_Request::setMethod(long request_method)
610 PHP_METHOD(HTTPi_Request
, setMethod
)
613 getObject(httpi_request_object
, obj
);
615 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
619 UPD_PROP(obj
, long, method
, meth
);
624 /* {{{ proto long HTTPi_Request::getMethod()
627 PHP_METHOD(HTTPi_Request
, getMethod
)
630 getObject(httpi_request_object
, obj
);
634 meth
= GET_PROP(obj
, method
);
635 RETURN_LONG(Z_LVAL_P(meth
));
639 /* {{{ proto bool HTTPi_Request::setContentType(string content_type)
642 PHP_METHOD(HTTPi_Request
, setContentType
)
646 getObject(httpi_request_object
, obj
);
648 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
652 if (!strchr(ctype
, '/')) {
653 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
654 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
659 UPD_PROP(obj
, string
, contentType
, ctype
);
664 /* {{{ proto string HTTPi_Request::getContentType()
667 PHP_METHOD(HTTPi_Request
, getContentType
)
670 getObject(httpi_request_object
, obj
);
674 ctype
= GET_PROP(obj
, contentType
);
675 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
679 /* {{{ proto bool HTTPi_Request::setQueryData(mixed query_data)
682 PHP_METHOD(HTTPi_Request
, setQueryData
)
685 char *query_data
= NULL
;
686 getObject(httpi_request_object
, obj
);
688 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
692 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
693 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
696 UPD_PROP(obj
, string
, queryData
, query_data
);
701 convert_to_string(qdata
);
702 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
707 /* {{{ proto string HTTPi_Request::getQueryData()
710 PHP_METHOD(HTTPi_Request
, getQueryData
)
713 getObject(httpi_request_object
, obj
);
717 qdata
= GET_PROP(obj
, queryData
);
718 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
722 /* {{{ proto bool HTTPi_Request::addQueryData(array query_params)
725 PHP_METHOD(HTTPi_Request
, addQueryData
)
727 zval
*qdata
, *old_qdata
;
728 char *query_data
= NULL
;
729 getObject(httpi_request_object
, obj
);
731 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
735 old_qdata
= GET_PROP(obj
, queryData
);
737 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
741 UPD_PROP(obj
, string
, queryData
, query_data
);
748 /* {{{ proto void HTTPi_Request::unsetQueryData()
751 PHP_METHOD(HTTPi_Request
, unsetQueryData
)
753 getObject(httpi_request_object
, obj
);
757 UPD_PROP(obj
, string
, queryData
, "");
761 /* {{{ proto bool HTTPi_Request::addPostData(array post_data)
764 PHP_METHOD(HTTPi_Request
, addPostData
)
766 zval
*post
, *post_data
;
767 getObject(httpi_request_object
, obj
);
769 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
773 post
= GET_PROP(obj
, postData
);
774 array_merge(post_data
, post
);
780 /* {{{ proto bool HTTPi_Request::setPostData(array post_data)
783 PHP_METHOD(HTTPi_Request
, setPostData
)
785 zval
*post
, *post_data
;
786 getObject(httpi_request_object
, obj
);
788 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
792 post
= GET_PROP(obj
, postData
);
793 zend_hash_clean(Z_ARRVAL_P(post
));
794 array_copy(post_data
, post
);
800 /* {{{ proto array HTTPi_Request::getPostData()
803 PHP_METHOD(HTTPi_Request
, getPostData
)
806 getObject(httpi_request_object
, obj
);
810 post_data
= GET_PROP(obj
, postData
);
811 array_init(return_value
);
812 array_copy(post_data
, return_value
);
816 /* {{{ proto void HTTPi_Request::unsetPostData()
819 PHP_METHOD(HTTPi_Request
, unsetPostData
)
822 getObject(httpi_request_object
, obj
);
826 post_data
= GET_PROP(obj
, postData
);
827 zend_hash_clean(Z_ARRVAL_P(post_data
));
831 /* {{{ proto bool HTTPi_Request::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
834 PHP_METHOD(HTTPi_Request
, addPostFile
)
837 char *name
, *file
, *type
= NULL
;
838 int name_len
, file_len
, type_len
= 0;
839 getObject(httpi_request_object
, obj
);
841 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
846 if (!strchr(type
, '/')) {
847 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
851 type
= "application/x-octetstream";
852 type_len
= sizeof("application/x-octetstream") - 1;
855 MAKE_STD_ZVAL(entry
);
858 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
859 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
860 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
862 files
= GET_PROP(obj
, postFiles
);
863 add_next_index_zval(files
, entry
);
869 /* {{{ proto array HTTPi_Request::getPostFiles()
872 PHP_METHOD(HTTPi_Request
, getPostFiles
)
875 getObject(httpi_request_object
, obj
);
879 files
= GET_PROP(obj
, postFiles
);
881 array_init(return_value
);
882 array_copy(files
, return_value
);
886 /* {{{ proto void HTTPi_Request::unsetPostFiles()
889 PHP_METHOD(HTTPi_Request
, unsetPostFiles
)
892 getObject(httpi_request_object
, obj
);
896 files
= GET_PROP(obj
, postFiles
);
897 zend_hash_clean(Z_ARRVAL_P(files
));
901 /* {{{ proto array HTTPi_Request::getResponseData()
904 PHP_METHOD(HTTPi_Request
, getResponseData
)
907 getObject(httpi_request_object
, obj
);
911 data
= GET_PROP(obj
, responseData
);
912 array_init(return_value
);
913 array_copy(data
, return_value
);
917 /* {{{ proto array HTTPi_Request::getResponseHeaders()
920 PHP_METHOD(HTTPi_Request
, getResponseHeaders
)
922 zval
*data
, **headers
;
923 getObject(httpi_request_object
, obj
);
927 array_init(return_value
);
928 data
= GET_PROP(obj
, responseData
);
929 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
930 array_copy(*headers
, return_value
);
935 /* {{{ proto string HTTPi_Request::getResponseBody()
938 PHP_METHOD(HTTPi_Request
, getResponseBody
)
941 getObject(httpi_request_object
, obj
);
945 data
= GET_PROP(obj
, responseData
);
946 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
947 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
949 Z_TYPE_P(return_value
) = IS_NULL
;
954 /* {{{ proto array HTTPi_Request::getResponseInfo()
957 PHP_METHOD(HTTPi_Request
, getResponseInfo
)
960 getObject(httpi_request_object
, obj
);
964 info
= GET_PROP(obj
, responseInfo
);
965 array_init(return_value
);
966 array_copy(info
, return_value
);
970 /* {{{ proto bool HTTPi_Request::send()
973 PHP_METHOD(HTTPi_Request
, send
)
975 STATUS status
= FAILURE
;
976 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
977 char *response_data
, *request_uri
;
979 getObject(httpi_request_object
, obj
);
983 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
984 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initilaize cURL");
988 meth
= GET_PROP(obj
, method
);
989 URL
= GET_PROP(obj
, url
);
990 qdata
= GET_PROP(obj
, queryData
);
991 opts
= GET_PROP(obj
, options
);
992 info
= GET_PROP(obj
, responseInfo
);
993 resp
= GET_PROP(obj
, responseData
);
995 // HTTP_URI_MAXLEN+1 big char *
996 request_uri
= http_absolute_uri(Z_STRVAL_P(URL
), NULL
);
998 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
999 if (!strchr(request_uri
, '?')) {
1000 strcat(request_uri
, "?");
1002 strcat(request_uri
, "&");
1004 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
1007 switch (Z_LVAL_P(meth
))
1010 status
= http_get_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1014 status
= http_head_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1019 zval
*post_files
, *post_data
;
1021 post_files
= GET_PROP(obj
, postFiles
);
1022 post_data
= GET_PROP(obj
, postData
);
1024 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files
))) {
1026 /* urlencoded post */
1027 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
);
1037 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
1040 FOREACH_KEYVAL(post_data
, key
, idx
, data
) {
1042 convert_to_string_ex(data
);
1043 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1044 CURLFORM_COPYNAME
, key
,
1045 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
1046 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
1056 FOREACH_VAL(post_files
, data
) {
1057 zval
**file
, **type
, **name
;
1059 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
1060 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
1061 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
1063 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1064 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
1065 CURLFORM_FILE
, Z_STRVAL_PP(file
),
1066 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
1072 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
);
1073 curl_formfree(http_post_data
[0]);
1084 /* final data handling */
1085 if (status
!= SUCCESS
) {
1088 zval
*zheaders
, *zbody
;
1090 MAKE_STD_ZVAL(zbody
);
1091 MAKE_STD_ZVAL(zheaders
)
1092 array_init(zheaders
);
1094 if (SUCCESS
!= http_split_response_ex(response_data
, response_len
, zheaders
, zbody
)) {
1095 zval_dtor(zheaders
);
1098 efree(response_data
);
1102 add_assoc_zval(resp
, "headers", zheaders
);
1103 add_assoc_zval(resp
, "body", zbody
);
1105 efree(response_data
);
1113 #endif /* HTTP_HAVE_CURL */
1115 #endif /* ZEND_ENGINE_2 */
1122 * vim600: noet sw=4 ts=4 fdm=marker
1123 * vim<600: noet sw=4 ts=4