1b2d80b786dc9c1a701fbcb66f33338e4b522d3e
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_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_cache_api.h"
27 #include "php_http_curl_api.h"
28 #include "php_http_date_api.h"
29 #include "php_http_headers_api.h"
30 #include "php_http_send_api.h"
31 #include "php_http_url_api.h"
35 /* {{{ HttpResponse */
37 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
39 * Instantiates a new HttpResponse object, which can be used to send
40 * any data/resource/file to an HTTP client with caching and multiple
41 * ranges/resuming support.
43 * NOTE: GZIPping is not implemented yet.
45 PHP_METHOD(HttpResponse
, __construct
)
47 zend_bool do_cache
= 0, do_gzip
= 0;
48 getObject(http_response_object
, obj
);
50 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
55 UPD_PROP(obj
, long, cache
, do_cache
);
56 UPD_PROP(obj
, long, gzip
, do_gzip
);
60 /* {{{ proto bool HttpResponse::setCache(bool cache)
62 * Whether it sould be attempted to cache the entitity.
63 * This will result in necessary caching headers and checks of clients
64 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
65 * matches a "304 Not Modified" status code will be issued.
67 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
68 * to something more appropriate than "no-cache"!
70 PHP_METHOD(HttpResponse
, setCache
)
72 zend_bool do_cache
= 0;
73 getObject(http_response_object
, obj
);
75 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
79 UPD_PROP(obj
, long, cache
, do_cache
);
84 /* {{{ proto bool HttpResponse::getCache()
86 * Get current caching setting.
88 PHP_METHOD(HttpResponse
, getCache
)
90 zval
*do_cache
= NULL
;
91 getObject(http_response_object
, obj
);
95 do_cache
= GET_PROP(obj
, cache
);
96 RETURN_BOOL(Z_LVAL_P(do_cache
));
100 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
102 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
104 PHP_METHOD(HttpResponse
, setGzip
)
106 zend_bool do_gzip
= 0;
107 getObject(http_response_object
, obj
);
109 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
113 UPD_PROP(obj
, long, gzip
, do_gzip
);
118 /* {{{ proto bool HttpResponse::getGzip()
120 * Get current gzipping setting.
122 PHP_METHOD(HttpResponse
, getGzip
)
124 zval
*do_gzip
= NULL
;
125 getObject(http_response_object
, obj
);
129 do_gzip
= GET_PROP(obj
, gzip
);
130 RETURN_BOOL(Z_LVAL_P(do_gzip
));
134 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
136 * Set a custom cache-control header, usually being "private" or "public"; if
137 * $raw is set to true the header will be sent as-is.
139 PHP_METHOD(HttpResponse
, setCacheControl
)
144 getObject(http_response_object
, obj
);
146 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
150 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
151 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
155 UPD_PROP(obj
, long, raw_cache_header
, raw
);
156 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
161 /* {{{ proto string HttpResponse::getCacheControl()
163 * Get current Cache-Control header setting.
165 PHP_METHOD(HttpResponse
, getCacheControl
)
168 getObject(http_response_object
, obj
);
172 ccontrol
= GET_PROP(obj
, cacheControl
);
173 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
177 /* {{{ proto bool HttpResponse::setContentType(string content_type)
179 * Set the content-type of the sent entity.
181 PHP_METHOD(HttpResponse
, setContentType
)
185 getObject(http_response_object
, obj
);
187 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
191 if (!strchr(ctype
, '/')) {
192 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
193 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
197 UPD_PROP(obj
, string
, contentType
, ctype
);
203 /* {{{ proto string HttpResponse::getContentType()
205 * Get current Content-Type header setting.
207 PHP_METHOD(HttpResponse
, getContentType
)
210 getObject(http_response_object
, obj
);
214 ctype
= GET_PROP(obj
, contentType
);
215 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
219 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
221 * Set the Content-Disposition of the sent entity. This setting aims to suggest
222 * the receiveing user agent how to handle the sent entity; usually the client
223 * will show the user a "Save As..." popup.
225 PHP_METHOD(HttpResponse
, setContentDisposition
)
229 zend_bool is_inline
= 0;
230 getObject(http_response_object
, obj
);
232 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
236 UPD_PROP(obj
, string
, dispoFile
, file
);
237 UPD_PROP(obj
, long, dispoInline
, is_inline
);
242 /* {{{ proto array HttpResponse::getContentDisposition()
244 * Get current Content-Disposition setting.
245 * Will return an associative array like:
248 * 'filename' => 'foo.bar',
253 PHP_METHOD(HttpResponse
, getContentDisposition
)
257 getObject(http_response_object
, obj
);
259 if (ZEND_NUM_ARGS()) {
263 file
= GET_PROP(obj
, dispoFile
);
264 is_inline
= GET_PROP(obj
, dispoInline
);
266 array_init(return_value
);
267 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
268 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
272 /* {{{ proto bool HttpResponse::setETag(string etag)
274 * Set a custom ETag. Use this only if you know what you're doing.
276 PHP_METHOD(HttpResponse
, setETag
)
280 getObject(http_response_object
, obj
);
282 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
286 UPD_PROP(obj
, string
, eTag
, etag
);
291 /* {{{ proto string HttpResponse::getETag()
293 * Get the previously set custom ETag.
295 PHP_METHOD(HttpResponse
, getETag
)
298 getObject(http_response_object
, obj
);
302 etag
= GET_PROP(obj
, eTag
);
303 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
307 /* {{{ proto bool HttpResponse::setData(string data)
309 * Set the data to be sent.
311 PHP_METHOD(HttpResponse
, setData
)
314 getObject(http_response_object
, obj
);
316 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_data
)) {
320 convert_to_string_ex(&the_data
);
321 SET_PROP(obj
, data
, the_data
);
322 UPD_PROP(obj
, long, lastModified
, http_lmod(the_data
, SEND_DATA
));
323 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
328 /* {{{ proto string HttpResponse::getData()
330 * Get the previously set data to be sent.
332 PHP_METHOD(HttpResponse
, getData
)
335 getObject(http_response_object
, obj
);
339 the_data
= GET_PROP(obj
, data
);
340 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
344 /* {{{ proto bool HttpResponse::setStream(resource stream)
346 * Set the resource to be sent.
348 PHP_METHOD(HttpResponse
, setStream
)
351 php_stream
*the_real_stream
;
352 getObject(http_response_object
, obj
);
354 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
358 php_stream_from_zval(the_real_stream
, &the_stream
);
360 SET_PROP(obj
, stream
, the_stream
);
361 UPD_PROP(obj
, long, lastModified
, http_lmod(the_real_stream
, SEND_RSRC
));
362 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
367 /* {{{ proto resource HttpResponse::getStream()
369 * Get the previously set resource to be sent.
371 PHP_METHOD(HttpResponse
, getStream
)
374 getObject(http_response_object
, obj
);
378 the_stream
= GET_PROP(obj
, stream
);
379 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
383 /* {{{ proto bool HttpResponse::setFile(string file)
385 * Set the file to be sent.
387 PHP_METHOD(HttpResponse
, setFile
)
390 getObject(http_response_object
, obj
);
392 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
396 convert_to_string_ex(&the_file
);
398 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
399 UPD_PROP(obj
, long, lastModified
, http_lmod(the_file
, -1));
400 UPD_PROP(obj
, long, send_mode
, -1);
405 /* {{{ proto string HttpResponse::getFile()
407 * Get the previously set file to be sent.
409 PHP_METHOD(HttpResponse
, getFile
)
412 getObject(http_response_object
, obj
);
416 the_file
= GET_PROP(obj
, file
);
417 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
421 /* {{{ proto bool HttpResponse::send()
423 * Finally send the entity.
428 * $r = new HttpResponse(true);
429 * $r->setFile('../hidden/contract.pdf');
430 * $r->setContentType('application/pdf');
436 PHP_METHOD(HttpResponse
, send
)
438 zval
*do_cache
, *do_gzip
;
439 getObject(http_response_object
, obj
);
443 do_cache
= GET_PROP(obj
, cache
);
444 do_gzip
= GET_PROP(obj
, gzip
);
447 if (Z_LVAL_P(do_gzip
)) {
448 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC
);
452 if (Z_LVAL_P(do_cache
)) {
453 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
455 etag
= GET_PROP(obj
, eTag
);
456 lmod
= GET_PROP(obj
, lastModified
);
457 cctrl
= GET_PROP(obj
, cacheControl
);
458 ccraw
= GET_PROP(obj
, raw_cache_header
);
460 if (Z_LVAL_P(ccraw
)) {
461 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
462 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
));
464 char cc_header
[42] = {0};
465 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
466 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_header
, strlen(cc_header
));
467 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_header
, strlen(cc_header
));
473 zval
*ctype
= GET_PROP(obj
, contentType
);
474 if (Z_STRLEN_P(ctype
)) {
475 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
477 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
481 /* content disposition */
483 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
484 if (Z_STRLEN_P(dispo_file
)) {
485 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
486 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), (zend_bool
) Z_LVAL_P(dispo_inline
));
492 zval
*send_mode
= GET_PROP(obj
, send_mode
);
493 switch (Z_LVAL_P(send_mode
))
497 zval
*zdata
= GET_PROP(obj
, data
);
498 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
503 php_stream
*the_real_stream
;
504 zval
*the_stream
= GET_PROP(obj
, stream
);
505 php_stream_from_zval(the_real_stream
, &the_stream
);
506 RETURN_SUCCESS(http_send_stream(the_real_stream
));
511 zval
*zfile
= GET_PROP(obj
, file
);
512 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile
)));
520 #ifdef HTTP_HAVE_CURL
521 /* {{{ HttpRequest */
523 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
525 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
526 * and POST (including posting files) HTTP requests.
528 PHP_METHOD(HttpRequest
, __construct
)
533 getObject(http_request_object
, obj
);
535 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
539 INIT_PARR(obj
, options
);
540 INIT_PARR(obj
, responseInfo
);
541 INIT_PARR(obj
, responseData
);
542 INIT_PARR(obj
, postData
);
543 INIT_PARR(obj
, postFiles
);
546 UPD_PROP(obj
, string
, url
, URL
);
549 UPD_PROP(obj
, long, method
, meth
);
554 /* {{{ proto void HttpRequest::__destruct()
556 * Destroys the HttpRequest object.
558 PHP_METHOD(HttpRequest
, __destruct
)
560 getObject(http_request_object
, obj
);
564 FREE_PARR(obj
, options
);
565 FREE_PARR(obj
, responseInfo
);
566 FREE_PARR(obj
, responseData
);
567 FREE_PARR(obj
, postData
);
568 FREE_PARR(obj
, postFiles
);
572 /* {{{ proto bool HttpRequest::setOptions(array options)
574 * Set the request options to use. See http_get() for a full list of available options.
576 PHP_METHOD(HttpRequest
, setOptions
)
580 zval
*opts
, *old_opts
, **opt
;
581 getObject(http_request_object
, obj
);
583 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
587 old_opts
= GET_PROP(obj
, options
);
589 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
590 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
592 if (!strcmp(key
, "headers")) {
594 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
595 array_merge(*opt
, *headers
);
598 } else if (!strcmp(key
, "cookies")) {
600 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
601 array_merge(*opt
, *cookies
);
606 add_assoc_zval(old_opts
, key
, *opt
);
617 /* {{{ proto array HttpRequest::getOptions()
619 * Get current set options.
621 PHP_METHOD(HttpRequest
, getOptions
)
624 getObject(http_request_object
, obj
);
628 opts
= GET_PROP(obj
, options
);
629 array_init(return_value
);
630 array_copy(opts
, return_value
);
634 /* {{{ proto void HttpRequest::unsetOptions()
636 * Unset all options/headers/cookies.
638 PHP_METHOD(HttpRequest
, unsetOptions
)
640 getObject(http_request_object
, obj
);
644 FREE_PARR(obj
, options
);
645 INIT_PARR(obj
, options
);
649 /* {{{ proto bool HttpRequest::setSslOptions(array options)
651 * Set additional SSL options.
653 PHP_METHOD(HttpRequest
, setSslOptions
)
655 zval
*opts
, *old_opts
, **ssl_options
;
656 getObject(http_request_object
, obj
);
658 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
662 old_opts
= GET_PROP(obj
, options
);
664 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
665 array_merge(opts
, *ssl_options
);
668 add_assoc_zval(old_opts
, "ssl", opts
);
675 /* {{{ proto array HttpRequest::getSslOtpions()
677 * Get previously set SSL options.
679 PHP_METHOD(HttpRequest
, getSslOptions
)
681 zval
*opts
, **ssl_options
;
682 getObject(http_request_object
, obj
);
686 opts
= GET_PROP(obj
, options
);
688 array_init(return_value
);
690 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
691 array_copy(*ssl_options
, return_value
);
696 /* {{{ proto void HttpRequest::unsetSslOptions()
698 * Unset previously set SSL options.
700 PHP_METHOD(HttpRequest
, unsetSslOptions
)
703 getObject(http_request_object
, obj
);
707 opts
= GET_PROP(obj
, options
);
708 zend_hash_del(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"));
712 /* {{{ proto bool HttpRequest::addHeaders(array headers)
714 * Add request header name/value pairs.
716 PHP_METHOD(HttpRequest
, addHeaders
)
718 zval
*opts
, **headers
, *new_headers
;
719 getObject(http_request_object
, obj
);
721 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
725 opts
= GET_PROP(obj
, options
);
727 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
728 array_merge(new_headers
, *headers
);
730 zval_add_ref(&new_headers
);
731 add_assoc_zval(opts
, "headers", new_headers
);
738 /* {{{ proto array HttpRequest::getHeaders()
740 * Get previously set request headers.
742 PHP_METHOD(HttpRequest
, getHeaders
)
744 zval
*opts
, **headers
;
745 getObject(http_request_object
, obj
);
749 opts
= GET_PROP(obj
, options
);
751 array_init(return_value
);
753 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
754 array_copy(*headers
, return_value
);
759 /* {{{ proto void HttpRequest::unsetHeaders()
761 * Unset previously set request headers.
763 PHP_METHOD(HttpRequest
, unsetHeaders
)
766 getObject(http_request_object
, obj
);
770 opts
= GET_PROP(obj
, options
);
771 zend_hash_del(Z_ARRVAL_P(opts
), "headers", sizeof("headers"));
775 /* {{{ proto bool HttpRequest::addCookies(array cookies)
779 PHP_METHOD(HttpRequest
, addCookies
)
781 zval
*opts
, **cookies
, *new_cookies
;
782 getObject(http_request_object
, obj
);
784 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
788 opts
= GET_PROP(obj
, options
);
790 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
791 array_merge(new_cookies
, *cookies
);
793 zval_add_ref(&new_cookies
);
794 add_assoc_zval(opts
, "cookies", new_cookies
);
801 /* {{{ proto array HttpRequest::getCookies()
803 * Get previously set cookies.
805 PHP_METHOD(HttpRequest
, getCookies
)
807 zval
*opts
, **cookies
;
808 getObject(http_request_object
, obj
);
812 opts
= GET_PROP(obj
, options
);
814 array_init(return_value
);
816 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
817 array_copy(*cookies
, return_value
);
822 /* {{{ proto void HttpRequest::unsetCookies()
825 PHP_METHOD(HttpRequest
, unsetCookies
)
828 getObject(http_request_object
, obj
);
832 opts
= GET_PROP(obj
, options
);
833 zend_hash_del(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"));
837 /* {{{ proto bool HttpRequest::setURL(string url)
839 * Set the request URL.
841 PHP_METHOD(HttpRequest
, setURL
)
845 getObject(http_request_object
, obj
);
847 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
851 UPD_PROP(obj
, string
, url
, URL
);
856 /* {{{ proto string HttpRequest::getUrl()
858 * Get the previously set request URL.
860 PHP_METHOD(HttpRequest
, getURL
)
863 getObject(http_request_object
, obj
);
867 URL
= GET_PROP(obj
, url
);
868 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
872 /* {{{ proto bool HttpRequest::setMethod(long request_method)
874 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
875 * <tt>HTTP_POST</tt> constants.
877 PHP_METHOD(HttpRequest
, setMethod
)
880 getObject(http_request_object
, obj
);
882 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
886 UPD_PROP(obj
, long, method
, meth
);
891 /* {{{ proto long HttpRequest::getMethod()
893 * Get the previously set request method.
895 PHP_METHOD(HttpRequest
, getMethod
)
898 getObject(http_request_object
, obj
);
902 meth
= GET_PROP(obj
, method
);
903 RETURN_LONG(Z_LVAL_P(meth
));
907 /* {{{ proto bool HttpRequest::setContentType(string content_type)
909 * Set the content type the post request should have.
910 * Use this only if you know what you're doing.
912 PHP_METHOD(HttpRequest
, setContentType
)
916 getObject(http_request_object
, obj
);
918 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
922 if (!strchr(ctype
, '/')) {
923 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
924 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
929 UPD_PROP(obj
, string
, contentType
, ctype
);
934 /* {{{ proto string HttpRequest::getContentType()
936 * Get the previously content type.
938 PHP_METHOD(HttpRequest
, getContentType
)
941 getObject(http_request_object
, obj
);
945 ctype
= GET_PROP(obj
, contentType
);
946 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
950 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
952 * Set the URL query parameters to use.
953 * Overwrites previously set query parameters.
954 * Affects any request types.
956 PHP_METHOD(HttpRequest
, setQueryData
)
959 char *query_data
= NULL
;
960 getObject(http_request_object
, obj
);
962 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
966 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
967 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
970 UPD_PROP(obj
, string
, queryData
, query_data
);
975 convert_to_string(qdata
);
976 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
981 /* {{{ proto string HttpRequest::getQueryData()
983 * Get the current query data in form of an urlencoded query string.
985 PHP_METHOD(HttpRequest
, getQueryData
)
988 getObject(http_request_object
, obj
);
992 qdata
= GET_PROP(obj
, queryData
);
993 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
997 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
999 * Add parameters to the query parameter list.
1000 * Affects any request type.
1002 PHP_METHOD(HttpRequest
, addQueryData
)
1004 zval
*qdata
, *old_qdata
;
1005 char *query_data
= NULL
;
1006 getObject(http_request_object
, obj
);
1008 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1012 old_qdata
= GET_PROP(obj
, queryData
);
1014 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
1018 UPD_PROP(obj
, string
, queryData
, query_data
);
1025 /* {{{ proto void HttpRequest::unsetQueryData()
1027 * Clean the query parameters.
1028 * Affects any request type.
1030 PHP_METHOD(HttpRequest
, unsetQueryData
)
1032 getObject(http_request_object
, obj
);
1036 UPD_PROP(obj
, string
, queryData
, "");
1040 /* {{{ proto bool HttpRequest::addPostData(array post_data)
1042 * Adds POST data entries.
1043 * Affects only POST requests.
1045 PHP_METHOD(HttpRequest
, addPostData
)
1047 zval
*post
, *post_data
;
1048 getObject(http_request_object
, obj
);
1050 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1054 post
= GET_PROP(obj
, postData
);
1055 array_merge(post_data
, post
);
1061 /* {{{ proto bool HttpRequest::setPostData(array post_data)
1063 * Set the POST data entries.
1064 * Overwrites previously set POST data.
1065 * Affects only POST requests.
1067 PHP_METHOD(HttpRequest
, setPostData
)
1069 zval
*post
, *post_data
;
1070 getObject(http_request_object
, obj
);
1072 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1076 post
= GET_PROP(obj
, postData
);
1077 zend_hash_clean(Z_ARRVAL_P(post
));
1078 array_copy(post_data
, post
);
1084 /* {{{ proto array HttpRequest::getPostData()
1086 * Get previously set POST data.
1088 PHP_METHOD(HttpRequest
, getPostData
)
1091 getObject(http_request_object
, obj
);
1095 post_data
= GET_PROP(obj
, postData
);
1096 array_init(return_value
);
1097 array_copy(post_data
, return_value
);
1101 /* {{{ proto void HttpRequest::unsetPostData()
1103 * Clean POST data entires.
1104 * Affects only POST requests.
1106 PHP_METHOD(HttpRequest
, unsetPostData
)
1109 getObject(http_request_object
, obj
);
1113 post_data
= GET_PROP(obj
, postData
);
1114 zend_hash_clean(Z_ARRVAL_P(post_data
));
1118 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1120 * Add a file to the POST request.
1121 * Affects only POST requests.
1123 PHP_METHOD(HttpRequest
, addPostFile
)
1125 zval
*files
, *entry
;
1126 char *name
, *file
, *type
= NULL
;
1127 int name_len
, file_len
, type_len
= 0;
1128 getObject(http_request_object
, obj
);
1130 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1135 if (!strchr(type
, '/')) {
1136 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
1140 type
= "application/x-octetstream";
1141 type_len
= sizeof("application/x-octetstream") - 1;
1144 MAKE_STD_ZVAL(entry
);
1147 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1148 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1149 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1151 files
= GET_PROP(obj
, postFiles
);
1152 add_next_index_zval(files
, entry
);
1158 /* {{{ proto array HttpRequest::getPostFiles()
1160 * Get all previously added POST files.
1162 PHP_METHOD(HttpRequest
, getPostFiles
)
1165 getObject(http_request_object
, obj
);
1169 files
= GET_PROP(obj
, postFiles
);
1171 array_init(return_value
);
1172 array_copy(files
, return_value
);
1176 /* {{{ proto void HttpRequest::unsetPostFiles()
1178 * Unset the POST files list.
1179 * Affects only POST requests.
1181 PHP_METHOD(HttpRequest
, unsetPostFiles
)
1184 getObject(http_request_object
, obj
);
1188 files
= GET_PROP(obj
, postFiles
);
1189 zend_hash_clean(Z_ARRVAL_P(files
));
1193 /* {{{ proto array HttpRequest::getResponseData()
1195 * Get all response data after the request has been sent.
1197 PHP_METHOD(HttpRequest
, getResponseData
)
1200 getObject(http_request_object
, obj
);
1204 data
= GET_PROP(obj
, responseData
);
1205 array_init(return_value
);
1206 array_copy(data
, return_value
);
1210 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1212 * Get response header(s) after the request has been sent.
1214 PHP_METHOD(HttpRequest
, getResponseHeader
)
1216 zval
*data
, **headers
, **header
;
1217 char *header_name
= NULL
;
1219 getObject(http_response_object
, obj
);
1221 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1225 data
= GET_PROP(obj
, responseData
);
1226 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1230 if (!header_len
|| !header_name
) {
1231 array_init(return_value
);
1232 array_copy(*headers
, return_value
);
1233 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1234 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1241 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1243 * Get response cookie(s) after the request has been sent.
1245 PHP_METHOD(HttpRequest
, getResponseCookie
)
1247 zval
*data
, **headers
;
1248 char *cookie_name
= NULL
;
1250 getObject(http_request_object
, obj
);
1252 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1256 array_init(return_value
);
1258 data
= GET_PROP(obj
, responseData
);
1259 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1262 zval
**header
= NULL
;
1264 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1265 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1266 /* several cookies? */
1267 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1270 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1272 MAKE_STD_ZVAL(cookie_hash
);
1273 array_init(cookie_hash
);
1275 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1277 add_next_index_zval(return_value
, cookie_hash
);
1281 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1282 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1283 add_next_index_zval(return_value
, cookie_hash
);
1284 return; /* <<< FOUND >>> */
1286 zval_dtor(cookie_hash
);
1291 zval_dtor(cookie_hash
);
1297 MAKE_STD_ZVAL(cookie_hash
);
1298 array_init(cookie_hash
);
1300 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1302 add_next_index_zval(return_value
, cookie_hash
);
1306 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1307 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1308 add_next_index_zval(return_value
, cookie_hash
);
1310 zval_dtor(cookie_hash
);
1315 zval_dtor(cookie_hash
);
1328 /* {{{ proto string HttpRequest::getResponseBody()
1330 * Get the response body after the request has been sent.
1332 PHP_METHOD(HttpRequest
, getResponseBody
)
1335 getObject(http_request_object
, obj
);
1339 data
= GET_PROP(obj
, responseData
);
1340 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1341 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
1348 /* {{{ proto int HttpRequest::getResponseCode()
1350 * Get the response code after the request has been sent.
1352 PHP_METHOD(HttpRequest
, getResponseCode
)
1354 zval
**code
, **hdrs
, *data
;
1355 getObject(http_request_object
, obj
);
1359 data
= GET_PROP(obj
, responseData
);
1360 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &hdrs
)) &&
1361 (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(hdrs
), "Status", sizeof("Status"), (void **) &code
))) {
1362 RETVAL_STRINGL(Z_STRVAL_PP(code
), Z_STRLEN_PP(code
), 1);
1363 convert_to_long(return_value
);
1370 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1372 * Get response info after the request has been sent.
1373 * See http_get() for a full list of returned info.
1375 PHP_METHOD(HttpRequest
, getResponseInfo
)
1377 zval
*info
, **infop
;
1378 char *info_name
= NULL
;
1380 getObject(http_request_object
, obj
);
1382 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1386 info
= GET_PROP(obj
, responseInfo
);
1388 if (info_len
&& info_name
) {
1389 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1390 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
1392 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Could not find response info named %s", info_name
);
1396 array_init(return_value
);
1397 array_copy(info
, return_value
);
1402 /* {{{ proto bool HttpRequest::send()
1404 * Send the HTTP request.
1409 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1410 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1411 * $r->addQueryData(array('category' => 3));
1412 * if ($r->send() && $r->getResponseCode() == 200) {
1413 * file_put_contents('local.rss', $r->getResponseBody());
1421 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1422 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1423 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
1424 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1426 * echo $r->getResponseBody();
1431 PHP_METHOD(HttpRequest
, send
)
1433 STATUS status
= FAILURE
;
1434 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
1435 char *response_data
, *request_uri
;
1436 size_t response_len
;
1437 getObject(http_request_object
, obj
);
1441 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
1442 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initilaize cURL");
1446 meth
= GET_PROP(obj
, method
);
1447 URL
= GET_PROP(obj
, url
);
1448 qdata
= GET_PROP(obj
, queryData
);
1449 opts
= GET_PROP(obj
, options
);
1450 info
= GET_PROP(obj
, responseInfo
);
1451 resp
= GET_PROP(obj
, responseData
);
1453 // HTTP_URI_MAXLEN+1 long char *
1454 request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0);
1456 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
1457 if (!strchr(request_uri
, '?')) {
1458 strcat(request_uri
, "?");
1460 strcat(request_uri
, "&");
1462 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
1465 switch (Z_LVAL_P(meth
))
1468 status
= http_get_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1472 status
= http_head_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1477 zval
*post_files
, *post_data
;
1479 post_files
= GET_PROP(obj
, postFiles
);
1480 post_data
= GET_PROP(obj
, postData
);
1482 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files
))) {
1484 /* urlencoded post */
1485 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
);
1495 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
1498 FOREACH_KEYVAL(post_data
, key
, idx
, data
) {
1500 convert_to_string_ex(data
);
1501 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1502 CURLFORM_COPYNAME
, key
,
1503 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
1504 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
1514 FOREACH_VAL(post_files
, data
) {
1515 zval
**file
, **type
, **name
;
1517 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
1518 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
1519 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
1521 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1522 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
1523 CURLFORM_FILE
, Z_STRVAL_PP(file
),
1524 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
1530 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
);
1531 curl_formfree(http_post_data
[0]);
1542 /* final data handling */
1543 if (status
!= SUCCESS
) {
1547 size_t body_len
= 0;
1550 MAKE_STD_ZVAL(zheaders
)
1551 array_init(zheaders
);
1553 if (SUCCESS
!= http_split_response_ex(response_data
, response_len
, Z_ARRVAL_P(zheaders
), &body
, &body_len
)) {
1554 zval_dtor(zheaders
);
1556 efree(response_data
);
1560 add_assoc_zval(resp
, "headers", zheaders
);
1561 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
1563 efree(response_data
);
1571 #endif /* HTTP_HAVE_CURL */
1573 #endif /* ZEND_ENGINE_2 */
1580 * vim600: noet sw=4 ts=4 fdm=marker
1581 * vim<600: noet sw=4 ts=4