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 "php_http_std_defs.h"
30 /* {{{ HttpResponse */
32 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
34 * Instantiates a new HttpResponse object, which can be used to send
35 * any data/resource/file to an HTTP client with caching and multiple
36 * ranges/resuming support.
38 * NOTE: GZIPping is not implemented yet.
40 PHP_METHOD(HttpResponse
, __construct
)
42 zend_bool do_cache
= 0, do_gzip
= 0;
43 getObject(http_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 HttpResponse::setCache(bool cache)
57 * Whether it sould be attempted to cache the entitity.
58 * This will result in necessary caching headers and checks of clients
59 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
60 * matches a "304 Not Modified" status code will be issued.
62 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
63 * to something more appropriate than "no-cache"!
65 PHP_METHOD(HttpResponse
, setCache
)
67 zend_bool do_cache
= 0;
68 getObject(http_response_object
, obj
);
70 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
74 UPD_PROP(obj
, long, cache
, do_cache
);
79 /* {{{ proto bool HttpResponse::getCache()
81 * Get current caching setting.
83 PHP_METHOD(HttpResponse
, getCache
)
85 zval
*do_cache
= NULL
;
86 getObject(http_response_object
, obj
);
90 do_cache
= GET_PROP(obj
, cache
);
91 RETURN_BOOL(Z_LVAL_P(do_cache
));
95 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
97 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
99 PHP_METHOD(HttpResponse
, setGzip
)
101 zend_bool do_gzip
= 0;
102 getObject(http_response_object
, obj
);
104 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
108 UPD_PROP(obj
, long, gzip
, do_gzip
);
113 /* {{{ proto bool HttpResponse::getGzip()
115 * Get current gzipping setting.
117 PHP_METHOD(HttpResponse
, getGzip
)
119 zval
*do_gzip
= NULL
;
120 getObject(http_response_object
, obj
);
124 do_gzip
= GET_PROP(obj
, gzip
);
125 RETURN_BOOL(Z_LVAL_P(do_gzip
));
129 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
131 * Set a custom cache-control header, usually being "private" or "public"; if
132 * $raw is set to true the header will be sent as-is.
134 PHP_METHOD(HttpResponse
, setCacheControl
)
139 getObject(http_response_object
, obj
);
141 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
145 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
146 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
150 UPD_PROP(obj
, long, raw_cache_header
, raw
);
151 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
156 /* {{{ proto string HttpResponse::getCacheControl()
158 * Get current Cache-Control header setting.
160 PHP_METHOD(HttpResponse
, getCacheControl
)
163 getObject(http_response_object
, obj
);
167 ccontrol
= GET_PROP(obj
, cacheControl
);
168 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
172 /* {{{ proto bool HttpResponse::setContentType(string content_type)
174 * Set the content-type of the sent entity.
176 PHP_METHOD(HttpResponse
, setContentType
)
180 getObject(http_response_object
, obj
);
182 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
186 if (!strchr(ctype
, '/')) {
187 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
188 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
192 UPD_PROP(obj
, string
, contentType
, ctype
);
198 /* {{{ proto string HttpResponse::getContentType()
200 * Get current Content-Type header setting.
202 PHP_METHOD(HttpResponse
, getContentType
)
205 getObject(http_response_object
, obj
);
209 ctype
= GET_PROP(obj
, contentType
);
210 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
214 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
216 * Set the Content-Disposition of the sent entity. This setting aims to suggest
217 * the receiveing user agent how to handle the sent entity; usually the client
218 * will show the user a "Save As..." popup.
220 PHP_METHOD(HttpResponse
, setContentDisposition
)
224 zend_bool is_inline
= 0;
225 getObject(http_response_object
, obj
);
227 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
231 UPD_PROP(obj
, string
, dispoFile
, file
);
232 UPD_PROP(obj
, long, dispoInline
, is_inline
);
237 /* {{{ proto array HttpResponse::getContentDisposition()
239 * Get current Content-Disposition setting.
240 * Will return an associative array like:
243 * 'filename' => 'foo.bar',
248 PHP_METHOD(HttpResponse
, getContentDisposition
)
252 getObject(http_response_object
, obj
);
254 if (ZEND_NUM_ARGS()) {
258 file
= GET_PROP(obj
, dispoFile
);
259 is_inline
= GET_PROP(obj
, dispoInline
);
261 array_init(return_value
);
262 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
263 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
267 /* {{{ proto bool HttpResponse::setETag(string etag)
269 * Set a custom ETag. Use this only if you know what you're doing.
271 PHP_METHOD(HttpResponse
, setETag
)
275 getObject(http_response_object
, obj
);
277 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
281 UPD_PROP(obj
, string
, eTag
, etag
);
286 /* {{{ proto string HttpResponse::getETag()
288 * Get the previously set custom ETag.
290 PHP_METHOD(HttpResponse
, getETag
)
293 getObject(http_response_object
, obj
);
297 etag
= GET_PROP(obj
, eTag
);
298 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
302 /* {{{ proto bool HttpResponse::setData(string data)
304 * Set the data to be sent.
306 PHP_METHOD(HttpResponse
, setData
)
309 getObject(http_response_object
, obj
);
311 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_data
)) {
315 convert_to_string_ex(&the_data
);
316 SET_PROP(obj
, data
, the_data
);
317 UPD_PROP(obj
, long, lastModified
, http_lmod(the_data
, SEND_DATA
));
318 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
323 /* {{{ proto string HttpResponse::getData()
325 * Get the previously set data to be sent.
327 PHP_METHOD(HttpResponse
, getData
)
330 getObject(http_response_object
, obj
);
334 the_data
= GET_PROP(obj
, data
);
335 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
339 /* {{{ proto bool HttpResponse::setStream(resource stream)
341 * Set the resource to be sent.
343 PHP_METHOD(HttpResponse
, setStream
)
346 php_stream
*the_real_stream
;
347 getObject(http_response_object
, obj
);
349 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
353 php_stream_from_zval(the_real_stream
, &the_stream
);
355 SET_PROP(obj
, stream
, the_stream
);
356 UPD_PROP(obj
, long, lastModified
, http_lmod(the_real_stream
, SEND_RSRC
));
357 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
362 /* {{{ proto resource HttpResponse::getStream()
364 * Get the previously set resource to be sent.
366 PHP_METHOD(HttpResponse
, getStream
)
369 getObject(http_response_object
, obj
);
373 the_stream
= GET_PROP(obj
, stream
);
374 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
378 /* {{{ proto bool HttpResponse::setFile(string file)
380 * Set the file to be sent.
382 PHP_METHOD(HttpResponse
, setFile
)
385 getObject(http_response_object
, obj
);
387 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
391 convert_to_string_ex(&the_file
);
393 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
394 UPD_PROP(obj
, long, lastModified
, http_lmod(the_file
, -1));
395 UPD_PROP(obj
, long, send_mode
, -1);
400 /* {{{ proto string HttpResponse::getFile()
402 * Get the previously set file to be sent.
404 PHP_METHOD(HttpResponse
, getFile
)
407 getObject(http_response_object
, obj
);
411 the_file
= GET_PROP(obj
, file
);
412 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
416 /* {{{ proto bool HttpResponse::send()
418 * Finally send the entity.
423 * $r = new HttpResponse(true);
424 * $r->setFile('../hidden/contract.pdf');
425 * $r->setContentType('application/pdf');
431 PHP_METHOD(HttpResponse
, send
)
433 zval
*do_cache
, *do_gzip
;
434 getObject(http_response_object
, obj
);
438 do_cache
= GET_PROP(obj
, cache
);
439 do_gzip
= GET_PROP(obj
, gzip
);
442 if (Z_LVAL_P(do_cache
)) {
443 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
445 etag
= GET_PROP(obj
, eTag
);
446 lmod
= GET_PROP(obj
, lastModified
);
447 cctrl
= GET_PROP(obj
, cacheControl
);
448 ccraw
= GET_PROP(obj
, raw_cache_header
);
450 if (Z_LVAL_P(ccraw
)) {
451 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
452 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
));
454 char cc_header
[42] = {0};
455 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
456 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_header
, strlen(cc_header
));
457 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_header
, strlen(cc_header
));
462 if (Z_LVAL_P(do_gzip
)) {
468 zval
*ctype
= GET_PROP(obj
, contentType
);
469 if (Z_STRLEN_P(ctype
)) {
470 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
472 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
476 /* content disposition */
478 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
479 if (Z_STRLEN_P(dispo_file
)) {
480 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
481 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), (zend_bool
) Z_LVAL_P(dispo_inline
));
487 zval
*send_mode
= GET_PROP(obj
, send_mode
);
488 switch (Z_LVAL_P(send_mode
))
492 zval
*zdata
= GET_PROP(obj
, data
);
493 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
498 php_stream
*the_real_stream
;
499 zval
*the_stream
= GET_PROP(obj
, stream
);
500 php_stream_from_zval(the_real_stream
, &the_stream
);
501 RETURN_SUCCESS(http_send_stream(the_real_stream
));
506 zval
*zfile
= GET_PROP(obj
, file
);
507 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile
)));
515 #ifdef HTTP_HAVE_CURL
516 /* {{{ HttpRequest */
518 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
520 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
521 * and POST (including posting files) HTTP requests.
523 PHP_METHOD(HttpRequest
, __construct
)
528 getObject(http_request_object
, obj
);
530 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
534 INIT_PARR(obj
, options
);
535 INIT_PARR(obj
, responseInfo
);
536 INIT_PARR(obj
, responseData
);
537 INIT_PARR(obj
, postData
);
538 INIT_PARR(obj
, postFiles
);
541 UPD_PROP(obj
, string
, url
, URL
);
544 UPD_PROP(obj
, long, method
, meth
);
549 /* {{{ proto void HttpRequest::__destruct()
551 * Destroys the HttpRequest object.
553 PHP_METHOD(HttpRequest
, __destruct
)
555 getObject(http_request_object
, obj
);
559 FREE_PARR(obj
, options
);
560 FREE_PARR(obj
, responseInfo
);
561 FREE_PARR(obj
, responseData
);
562 FREE_PARR(obj
, postData
);
563 FREE_PARR(obj
, postFiles
);
567 /* {{{ proto bool HttpRequest::setOptions(array options)
569 * Set the request options to use. See http_get() for a full list of available options.
571 PHP_METHOD(HttpRequest
, setOptions
)
575 zval
*opts
, *old_opts
, **opt
;
576 getObject(http_request_object
, obj
);
578 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
582 old_opts
= GET_PROP(obj
, options
);
584 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
585 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
587 if (!strcmp(key
, "headers")) {
589 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
590 array_merge(*opt
, *headers
);
593 } else if (!strcmp(key
, "cookies")) {
595 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
596 array_merge(*opt
, *cookies
);
601 add_assoc_zval(old_opts
, key
, *opt
);
612 /* {{{ proto array HttpRequest::getOptions()
614 * Get current set options.
616 PHP_METHOD(HttpRequest
, getOptions
)
619 getObject(http_request_object
, obj
);
623 opts
= GET_PROP(obj
, options
);
624 array_init(return_value
);
625 array_copy(opts
, return_value
);
629 /* {{{ proto void HttpRequest::unsetOptions()
631 * Unset all options/headers/cookies.
633 PHP_METHOD(HttpRequest
, unsetOptions
)
635 getObject(http_request_object
, obj
);
639 FREE_PARR(obj
, options
);
640 INIT_PARR(obj
, options
);
644 /* {{{ proto bool HttpRequest::addHeader(array header)
646 * Add (a) request header name/value pair(s).
648 PHP_METHOD(HttpRequest
, addHeader
)
650 zval
*opts
, **headers
, *new_headers
;
651 getObject(http_request_object
, obj
);
653 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
657 opts
= GET_PROP(obj
, options
);
659 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
660 array_merge(new_headers
, *headers
);
662 zval_add_ref(&new_headers
);
663 add_assoc_zval(opts
, "headers", new_headers
);
670 /* {{{ proto bool HttpRequest::addCookie(array cookie)
674 PHP_METHOD(HttpRequest
, addCookie
)
676 zval
*opts
, **cookies
, *new_cookies
;
677 getObject(http_request_object
, obj
);
679 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
683 opts
= GET_PROP(obj
, options
);
685 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
686 array_merge(new_cookies
, *cookies
);
688 zval_add_ref(&new_cookies
);
689 add_assoc_zval(opts
, "cookies", new_cookies
);
696 /* {{{ proto bool HttpRequest::setURL(string url)
698 * Set the request URL.
700 PHP_METHOD(HttpRequest
, setURL
)
704 getObject(http_request_object
, obj
);
706 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
710 UPD_PROP(obj
, string
, url
, URL
);
715 /* {{{ proto string HttpRequest::getUrl()
717 * Get the previously set request URL.
719 PHP_METHOD(HttpRequest
, getURL
)
722 getObject(http_request_object
, obj
);
726 URL
= GET_PROP(obj
, url
);
727 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
731 /* {{{ proto bool HttpRequest::setMethod(long request_method)
733 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
734 * <tt>HTTP_POST</tt> constants.
736 PHP_METHOD(HttpRequest
, setMethod
)
739 getObject(http_request_object
, obj
);
741 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
745 UPD_PROP(obj
, long, method
, meth
);
750 /* {{{ proto long HttpRequest::getMethod()
752 * Get the previously set request method.
754 PHP_METHOD(HttpRequest
, getMethod
)
757 getObject(http_request_object
, obj
);
761 meth
= GET_PROP(obj
, method
);
762 RETURN_LONG(Z_LVAL_P(meth
));
766 /* {{{ proto bool HttpRequest::setContentType(string content_type)
768 * Set the content type the post request should have.
769 * Use this only if you know what you're doing.
771 PHP_METHOD(HttpRequest
, setContentType
)
775 getObject(http_request_object
, obj
);
777 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
781 if (!strchr(ctype
, '/')) {
782 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
783 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
788 UPD_PROP(obj
, string
, contentType
, ctype
);
793 /* {{{ proto string HttpRequest::getContentType()
795 * Get the previously content type.
797 PHP_METHOD(HttpRequest
, getContentType
)
800 getObject(http_request_object
, obj
);
804 ctype
= GET_PROP(obj
, contentType
);
805 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
809 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
811 * Set the URL query parameters to use.
812 * Overwrites previously set query parameters.
813 * Affects any request types.
815 PHP_METHOD(HttpRequest
, setQueryData
)
818 char *query_data
= NULL
;
819 getObject(http_request_object
, obj
);
821 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
825 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
826 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
829 UPD_PROP(obj
, string
, queryData
, query_data
);
834 convert_to_string(qdata
);
835 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
840 /* {{{ proto string HttpRequest::getQueryData()
842 * Get the current query data in form of an urlencoded query string.
844 PHP_METHOD(HttpRequest
, getQueryData
)
847 getObject(http_request_object
, obj
);
851 qdata
= GET_PROP(obj
, queryData
);
852 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
856 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
858 * Add parameters to the query parameter list.
859 * Affects any request type.
861 PHP_METHOD(HttpRequest
, addQueryData
)
863 zval
*qdata
, *old_qdata
;
864 char *query_data
= NULL
;
865 getObject(http_request_object
, obj
);
867 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
871 old_qdata
= GET_PROP(obj
, queryData
);
873 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
877 UPD_PROP(obj
, string
, queryData
, query_data
);
884 /* {{{ proto void HttpRequest::unsetQueryData()
886 * Clean the query parameters.
887 * Affects any request type.
889 PHP_METHOD(HttpRequest
, unsetQueryData
)
891 getObject(http_request_object
, obj
);
895 UPD_PROP(obj
, string
, queryData
, "");
899 /* {{{ proto bool HttpRequest::addPostData(array post_data)
901 * Adds POST data entries.
902 * Affects only POST requests.
904 PHP_METHOD(HttpRequest
, addPostData
)
906 zval
*post
, *post_data
;
907 getObject(http_request_object
, obj
);
909 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
913 post
= GET_PROP(obj
, postData
);
914 array_merge(post_data
, post
);
920 /* {{{ proto bool HttpRequest::setPostData(array post_data)
922 * Set the POST data entries.
923 * Overwrites previously set POST data.
924 * Affects only POST requests.
926 PHP_METHOD(HttpRequest
, setPostData
)
928 zval
*post
, *post_data
;
929 getObject(http_request_object
, obj
);
931 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
935 post
= GET_PROP(obj
, postData
);
936 zend_hash_clean(Z_ARRVAL_P(post
));
937 array_copy(post_data
, post
);
943 /* {{{ proto array HttpRequest::getPostData()
945 * Get previously set POST data.
947 PHP_METHOD(HttpRequest
, getPostData
)
950 getObject(http_request_object
, obj
);
954 post_data
= GET_PROP(obj
, postData
);
955 array_init(return_value
);
956 array_copy(post_data
, return_value
);
960 /* {{{ proto void HttpRequest::unsetPostData()
962 * Clean POST data entires.
963 * Affects only POST requests.
965 PHP_METHOD(HttpRequest
, unsetPostData
)
968 getObject(http_request_object
, obj
);
972 post_data
= GET_PROP(obj
, postData
);
973 zend_hash_clean(Z_ARRVAL_P(post_data
));
977 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
979 * Add a file to the POST request.
980 * Affects only POST requests.
982 PHP_METHOD(HttpRequest
, addPostFile
)
985 char *name
, *file
, *type
= NULL
;
986 int name_len
, file_len
, type_len
= 0;
987 getObject(http_request_object
, obj
);
989 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
994 if (!strchr(type
, '/')) {
995 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
999 type
= "application/x-octetstream";
1000 type_len
= sizeof("application/x-octetstream") - 1;
1003 MAKE_STD_ZVAL(entry
);
1006 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1007 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1008 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1010 files
= GET_PROP(obj
, postFiles
);
1011 add_next_index_zval(files
, entry
);
1017 /* {{{ proto array HttpRequest::getPostFiles()
1019 * Get all previously added POST files.
1021 PHP_METHOD(HttpRequest
, getPostFiles
)
1024 getObject(http_request_object
, obj
);
1028 files
= GET_PROP(obj
, postFiles
);
1030 array_init(return_value
);
1031 array_copy(files
, return_value
);
1035 /* {{{ proto void HttpRequest::unsetPostFiles()
1037 * Unset the POST files list.
1038 * Affects only POST requests.
1040 PHP_METHOD(HttpRequest
, unsetPostFiles
)
1043 getObject(http_request_object
, obj
);
1047 files
= GET_PROP(obj
, postFiles
);
1048 zend_hash_clean(Z_ARRVAL_P(files
));
1052 /* {{{ proto array HttpRequest::getResponseData()
1054 * Get all response data after the request has been sent.
1056 PHP_METHOD(HttpRequest
, getResponseData
)
1059 getObject(http_request_object
, obj
);
1063 data
= GET_PROP(obj
, responseData
);
1064 array_init(return_value
);
1065 array_copy(data
, return_value
);
1069 /* {{{ proto string HttpRequest::getResponseHeader([string name])
1071 * Get response header(s) after the request has been sent.
1073 PHP_METHOD(HttpRequest
, getResponseHeader
)
1075 zval
*data
, **headers
, **header
;
1076 char *header_name
= NULL
;
1078 getObject(http_response_object
, obj
);
1080 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1084 data
= GET_PROP(obj
, responseData
);
1085 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1089 if (!header_len
|| !header_name
) {
1090 array_init(return_value
);
1091 array_copy(*headers
, return_value
);
1092 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1093 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1099 /* {{{ proto string HttpRequest::getResponseBody()
1101 * Get the response body after the request has been sent.
1103 PHP_METHOD(HttpRequest
, getResponseBody
)
1106 getObject(http_request_object
, obj
);
1110 data
= GET_PROP(obj
, responseData
);
1111 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1112 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
1119 /* {{{ proto int HttpRequest::getResponseCode()
1121 * Get the response code after the request has been sent.
1123 PHP_METHOD(HttpRequest
, getResponseCode
)
1125 zval
**code
, **hdrs
, *data
;
1126 getObject(http_request_object
, obj
);
1130 data
= GET_PROP(obj
, responseData
);
1131 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &hdrs
)) &&
1132 (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(hdrs
), "Status", sizeof("Status"), (void **) &code
))) {
1133 RETVAL_STRINGL(Z_STRVAL_PP(code
), Z_STRLEN_PP(code
), 1);
1134 convert_to_long(return_value
);
1141 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1143 * Get response info after the request has been sent.
1144 * See http_get() for a full list of returned info.
1146 PHP_METHOD(HttpRequest
, getResponseInfo
)
1148 zval
*info
, **infop
;
1149 char *info_name
= NULL
;
1151 getObject(http_request_object
, obj
);
1153 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1157 info
= GET_PROP(obj
, responseInfo
);
1159 if (info_len
&& info_name
) {
1160 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1161 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
1163 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Could not find response info named %s", info_name
);
1167 array_init(return_value
);
1168 array_copy(info
, return_value
);
1173 /* {{{ proto bool HttpRequest::send()
1175 * Send the HTTP request.
1180 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1181 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1182 * $r->addQueryData(array('category' => 3));
1183 * if ($r->send() && $r->getResponseCode() == 200) {
1184 * file_put_contents('local.rss', $r->getResponseBody());
1192 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1193 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1194 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
1195 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1197 * echo $r->getResponseBody();
1202 PHP_METHOD(HttpRequest
, send
)
1204 STATUS status
= FAILURE
;
1205 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
1206 char *response_data
, *request_uri
;
1207 size_t response_len
;
1208 getObject(http_request_object
, obj
);
1212 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
1213 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initilaize cURL");
1217 meth
= GET_PROP(obj
, method
);
1218 URL
= GET_PROP(obj
, url
);
1219 qdata
= GET_PROP(obj
, queryData
);
1220 opts
= GET_PROP(obj
, options
);
1221 info
= GET_PROP(obj
, responseInfo
);
1222 resp
= GET_PROP(obj
, responseData
);
1224 // HTTP_URI_MAXLEN+1 long char *
1225 request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0);
1227 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
1228 if (!strchr(request_uri
, '?')) {
1229 strcat(request_uri
, "?");
1231 strcat(request_uri
, "&");
1233 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
1236 switch (Z_LVAL_P(meth
))
1239 status
= http_get_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1243 status
= http_head_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1248 zval
*post_files
, *post_data
;
1250 post_files
= GET_PROP(obj
, postFiles
);
1251 post_data
= GET_PROP(obj
, postData
);
1253 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files
))) {
1255 /* urlencoded post */
1256 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
);
1266 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
1269 FOREACH_KEYVAL(post_data
, key
, idx
, data
) {
1271 convert_to_string_ex(data
);
1272 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1273 CURLFORM_COPYNAME
, key
,
1274 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
1275 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
1285 FOREACH_VAL(post_files
, data
) {
1286 zval
**file
, **type
, **name
;
1288 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
1289 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
1290 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
1292 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1293 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
1294 CURLFORM_FILE
, Z_STRVAL_PP(file
),
1295 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
1301 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
);
1302 curl_formfree(http_post_data
[0]);
1313 /* final data handling */
1314 if (status
!= SUCCESS
) {
1318 size_t body_len
= 0;
1321 MAKE_STD_ZVAL(zheaders
)
1322 array_init(zheaders
);
1324 if (SUCCESS
!= http_split_response_ex(response_data
, response_len
, Z_ARRVAL_P(zheaders
), &body
, &body_len
)) {
1325 zval_dtor(zheaders
);
1327 efree(response_data
);
1331 add_assoc_zval(resp
, "headers", zheaders
);
1332 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
1334 efree(response_data
);
1342 #endif /* HTTP_HAVE_CURL */
1344 #endif /* ZEND_ENGINE_2 */
1351 * vim600: noet sw=4 ts=4 fdm=marker
1352 * vim<600: noet sw=4 ts=4