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
)
280 getObject(httpi_response_object
, obj
);
282 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_data
)) {
286 convert_to_string_ex(&the_data
);
287 SET_PROP(obj
, data
, the_data
);
288 UPD_PROP(obj
, long, lastModified
, http_lmod(the_data
, SEND_DATA
));
289 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
294 /* {{{ proto string HTTPi_Response::getData()
297 PHP_METHOD(HTTPi_Response
, getData
)
300 getObject(httpi_response_object
, obj
);
304 the_data
= GET_PROP(obj
, data
);
305 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
309 /* {{{ proto bool HTTPi_Response::setStream(resource stream)
312 PHP_METHOD(HTTPi_Response
, setStream
)
315 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 zval
*zdata
= GET_PROP(obj
, data
);
442 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
447 php_stream
*the_real_stream
;
448 zval
*the_stream
= GET_PROP(obj
, stream
);
449 php_stream_from_zval(the_real_stream
, &the_stream
);
450 RETURN_SUCCESS(http_send_stream(the_real_stream
));
455 zval
*zfile
= GET_PROP(obj
, file
);
456 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile
)));
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 getObject(httpi_request_object
, obj
);
477 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
481 INIT_PARR(obj
, options
);
482 INIT_PARR(obj
, responseInfo
);
483 INIT_PARR(obj
, responseData
);
484 INIT_PARR(obj
, postData
);
485 INIT_PARR(obj
, postFiles
);
488 UPD_PROP(obj
, string
, url
, URL
);
491 UPD_PROP(obj
, long, method
, meth
);
496 /* {{{ proto void HTTPi_Request::__destruct()
499 PHP_METHOD(HTTPi_Request
, __destruct
)
501 getObject(httpi_request_object
, obj
);
505 FREE_PARR(obj
, options
);
506 FREE_PARR(obj
, responseInfo
);
507 FREE_PARR(obj
, responseData
);
508 FREE_PARR(obj
, postData
);
509 FREE_PARR(obj
, postFiles
);
513 /* {{{ proto bool HTTPi_Request::setOptions(array options)
516 PHP_METHOD(HTTPi_Request
, setOptions
)
520 zval
*opts
, *old_opts
, **opt
;
521 getObject(httpi_request_object
, obj
);
523 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
527 old_opts
= GET_PROP(obj
, options
);
529 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
530 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
532 if (!strcmp(key
, "headers")) {
534 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
535 array_merge(*opt
, *headers
);
538 } else if (!strcmp(key
, "cookies")) {
540 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
541 array_merge(*opt
, *cookies
);
546 add_assoc_zval(old_opts
, key
, *opt
);
557 /* {{{ proto array HTTPi_Request::getOptions()
560 PHP_METHOD(HTTPi_Request
, getOptions
)
563 getObject(httpi_request_object
, obj
);
567 opts
= GET_PROP(obj
, options
);
568 array_init(return_value
);
569 array_copy(opts
, return_value
);
573 /* {{{ proto bool HTTPi_Request::setURL(string url)
576 PHP_METHOD(HTTPi_Request
, setURL
)
580 getObject(httpi_request_object
, obj
);
582 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
586 UPD_PROP(obj
, string
, url
, URL
);
591 /* {{{ proto string HTTPi_Request::getUrl()
594 PHP_METHOD(HTTPi_Request
, getURL
)
597 getObject(httpi_request_object
, obj
);
601 URL
= GET_PROP(obj
, url
);
602 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
606 /* {{{ proto bool HTTPi_Request::setMethod(long request_method)
609 PHP_METHOD(HTTPi_Request
, setMethod
)
612 getObject(httpi_request_object
, obj
);
614 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
618 UPD_PROP(obj
, long, method
, meth
);
623 /* {{{ proto long HTTPi_Request::getMethod()
626 PHP_METHOD(HTTPi_Request
, getMethod
)
629 getObject(httpi_request_object
, obj
);
633 meth
= GET_PROP(obj
, method
);
634 RETURN_LONG(Z_LVAL_P(meth
));
638 /* {{{ proto bool HTTPi_Request::setContentType(string content_type)
641 PHP_METHOD(HTTPi_Request
, setContentType
)
645 getObject(httpi_request_object
, obj
);
647 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
651 if (!strchr(ctype
, '/')) {
652 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
653 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
658 UPD_PROP(obj
, string
, contentType
, ctype
);
663 /* {{{ proto string HTTPi_Request::getContentType()
666 PHP_METHOD(HTTPi_Request
, getContentType
)
669 getObject(httpi_request_object
, obj
);
673 ctype
= GET_PROP(obj
, contentType
);
674 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
678 /* {{{ proto bool HTTPi_Request::setQueryData(mixed query_data)
681 PHP_METHOD(HTTPi_Request
, setQueryData
)
684 char *query_data
= NULL
;
685 getObject(httpi_request_object
, obj
);
687 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
691 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
692 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
695 UPD_PROP(obj
, string
, queryData
, query_data
);
700 convert_to_string(qdata
);
701 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
706 /* {{{ proto string HTTPi_Request::getQueryData()
709 PHP_METHOD(HTTPi_Request
, getQueryData
)
712 getObject(httpi_request_object
, obj
);
716 qdata
= GET_PROP(obj
, queryData
);
717 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
721 /* {{{ proto bool HTTPi_Request::addQueryData(array query_params)
724 PHP_METHOD(HTTPi_Request
, addQueryData
)
726 zval
*qdata
, *old_qdata
;
727 char *query_data
= NULL
;
728 getObject(httpi_request_object
, obj
);
730 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
734 old_qdata
= GET_PROP(obj
, queryData
);
736 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
740 UPD_PROP(obj
, string
, queryData
, query_data
);
747 /* {{{ proto void HTTPi_Request::unsetQueryData()
750 PHP_METHOD(HTTPi_Request
, unsetQueryData
)
752 getObject(httpi_request_object
, obj
);
756 UPD_PROP(obj
, string
, queryData
, "");
760 /* {{{ proto bool HTTPi_Request::addPostData(array post_data)
763 PHP_METHOD(HTTPi_Request
, addPostData
)
765 zval
*post
, *post_data
;
766 getObject(httpi_request_object
, obj
);
768 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
772 post
= GET_PROP(obj
, postData
);
773 array_merge(post_data
, post
);
779 /* {{{ proto bool HTTPi_Request::setPostData(array post_data)
782 PHP_METHOD(HTTPi_Request
, setPostData
)
784 zval
*post
, *post_data
;
785 getObject(httpi_request_object
, obj
);
787 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
791 post
= GET_PROP(obj
, postData
);
792 zend_hash_clean(Z_ARRVAL_P(post
));
793 array_copy(post_data
, post
);
799 /* {{{ proto array HTTPi_Request::getPostData()
802 PHP_METHOD(HTTPi_Request
, getPostData
)
805 getObject(httpi_request_object
, obj
);
809 post_data
= GET_PROP(obj
, postData
);
810 array_init(return_value
);
811 array_copy(post_data
, return_value
);
815 /* {{{ proto void HTTPi_Request::unsetPostData()
818 PHP_METHOD(HTTPi_Request
, unsetPostData
)
821 getObject(httpi_request_object
, obj
);
825 post_data
= GET_PROP(obj
, postData
);
826 zend_hash_clean(Z_ARRVAL_P(post_data
));
830 /* {{{ proto bool HTTPi_Request::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
833 PHP_METHOD(HTTPi_Request
, addPostFile
)
836 char *name
, *file
, *type
= NULL
;
837 int name_len
, file_len
, type_len
= 0;
838 getObject(httpi_request_object
, obj
);
840 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
845 if (!strchr(type
, '/')) {
846 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
850 type
= "application/x-octetstream";
851 type_len
= sizeof("application/x-octetstream") - 1;
854 MAKE_STD_ZVAL(entry
);
857 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
858 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
859 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
861 files
= GET_PROP(obj
, postFiles
);
862 add_next_index_zval(files
, entry
);
868 /* {{{ proto array HTTPi_Request::getPostFiles()
871 PHP_METHOD(HTTPi_Request
, getPostFiles
)
874 getObject(httpi_request_object
, obj
);
878 files
= GET_PROP(obj
, postFiles
);
880 array_init(return_value
);
881 array_copy(files
, return_value
);
885 /* {{{ proto void HTTPi_Request::unsetPostFiles()
888 PHP_METHOD(HTTPi_Request
, unsetPostFiles
)
891 getObject(httpi_request_object
, obj
);
895 files
= GET_PROP(obj
, postFiles
);
896 zend_hash_clean(Z_ARRVAL_P(files
));
900 /* {{{ proto array HTTPi_Request::getResponseData()
903 PHP_METHOD(HTTPi_Request
, getResponseData
)
906 getObject(httpi_request_object
, obj
);
910 data
= GET_PROP(obj
, responseData
);
911 array_init(return_value
);
912 array_copy(data
, return_value
);
916 /* {{{ proto array HTTPi_Request::getResponseHeaders()
919 PHP_METHOD(HTTPi_Request
, getResponseHeaders
)
921 zval
*data
, **headers
;
922 getObject(httpi_request_object
, obj
);
926 array_init(return_value
);
927 data
= GET_PROP(obj
, responseData
);
928 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
929 array_copy(*headers
, return_value
);
934 /* {{{ proto string HTTPi_Request::getResponseBody()
937 PHP_METHOD(HTTPi_Request
, getResponseBody
)
940 getObject(httpi_request_object
, obj
);
944 data
= GET_PROP(obj
, responseData
);
945 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
946 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
948 Z_TYPE_P(return_value
) = IS_NULL
;
953 /* {{{ proto array HTTPi_Request::getResponseInfo()
956 PHP_METHOD(HTTPi_Request
, getResponseInfo
)
959 getObject(httpi_request_object
, obj
);
963 info
= GET_PROP(obj
, responseInfo
);
964 array_init(return_value
);
965 array_copy(info
, return_value
);
969 /* {{{ proto bool HTTPi_Request::send()
972 PHP_METHOD(HTTPi_Request
, send
)
974 STATUS status
= FAILURE
;
975 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
976 char *response_data
, *request_uri
;
978 getObject(httpi_request_object
, obj
);
982 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
983 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initilaize cURL");
987 meth
= GET_PROP(obj
, method
);
988 URL
= GET_PROP(obj
, url
);
989 qdata
= GET_PROP(obj
, queryData
);
990 opts
= GET_PROP(obj
, options
);
991 info
= GET_PROP(obj
, responseInfo
);
992 resp
= GET_PROP(obj
, responseData
);
994 // HTTP_URI_MAXLEN+1 big char *
995 request_uri
= http_absolute_uri(Z_STRVAL_P(URL
), NULL
);
997 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
998 if (!strchr(request_uri
, '?')) {
999 strcat(request_uri
, "?");
1001 strcat(request_uri
, "&");
1003 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
1006 switch (Z_LVAL_P(meth
))
1009 status
= http_get_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1013 status
= http_head_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1018 zval
*post_files
, *post_data
;
1020 post_files
= GET_PROP(obj
, postFiles
);
1021 post_data
= GET_PROP(obj
, postData
);
1023 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files
))) {
1025 /* urlencoded post */
1026 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
);
1036 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
1039 FOREACH_KEYVAL(post_data
, key
, idx
, data
) {
1041 convert_to_string_ex(data
);
1042 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1043 CURLFORM_COPYNAME
, key
,
1044 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
1045 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
1055 FOREACH_VAL(post_files
, data
) {
1056 zval
**file
, **type
, **name
;
1058 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
1059 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
1060 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
1062 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1063 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
1064 CURLFORM_FILE
, Z_STRVAL_PP(file
),
1065 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
1071 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
);
1072 curl_formfree(http_post_data
[0]);
1083 /* final data handling */
1084 if (status
!= SUCCESS
) {
1087 zval
*zheaders
, *zbody
;
1089 MAKE_STD_ZVAL(zbody
);
1090 MAKE_STD_ZVAL(zheaders
)
1091 array_init(zheaders
);
1093 if (SUCCESS
!= http_split_response_ex(response_data
, response_len
, zheaders
, zbody
)) {
1094 zval_dtor(zheaders
);
1097 efree(response_data
);
1101 add_assoc_zval(resp
, "headers", zheaders
);
1102 add_assoc_zval(resp
, "body", zbody
);
1104 efree(response_data
);
1112 #endif /* HTTP_HAVE_CURL */
1114 #endif /* ZEND_ENGINE_2 */
1121 * vim600: noet sw=4 ts=4 fdm=marker
1122 * vim<600: noet sw=4 ts=4