1c3b99c599528e2d7d10eb9add79a62680657c3a
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::setSslOptions(array options)
646 * Set additional SSL options.
648 PHP_METHOD(HttpRequest
, setSslOptions
)
650 zval
*opts
, *old_opts
, **ssl_options
;
651 getObject(http_request_object
, obj
);
653 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
657 old_opts
= GET_PROP(obj
, options
);
659 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
660 array_merge(opts
, *ssl_options
);
663 add_assoc_zval(old_opts
, "ssl", opts
);
670 /* {{{ proto array HttpRequest::getSslOtpions()
672 * Get previously set SSL options.
674 PHP_METHOD(HttpRequest
, getSslOptions
)
676 zval
*opts
, **ssl_options
;
677 getObject(http_request_object
, obj
);
681 opts
= GET_PROP(obj
, options
);
683 array_init(return_value
);
685 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
686 array_copy(*ssl_options
, return_value
);
691 /* {{{ proto void HttpRequest::unsetSslOptions()
693 * Unset previously set SSL options.
695 PHP_METHOD(HttpRequest
, unsetSslOptions
)
698 getObject(http_request_object
, obj
);
702 opts
= GET_PROP(obj
, options
);
703 zend_hash_del(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"));
707 /* {{{ proto bool HttpRequest::addHeaders(array headers)
709 * Add request header name/value pairs.
711 PHP_METHOD(HttpRequest
, addHeaders
)
713 zval
*opts
, **headers
, *new_headers
;
714 getObject(http_request_object
, obj
);
716 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
720 opts
= GET_PROP(obj
, options
);
722 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
723 array_merge(new_headers
, *headers
);
725 zval_add_ref(&new_headers
);
726 add_assoc_zval(opts
, "headers", new_headers
);
733 /* {{{ proto array HttpRequest::getHeaders()
735 * Get previously set request headers.
737 PHP_METHOD(HttpRequest
, getHeaders
)
739 zval
*opts
, **headers
;
740 getObject(http_request_object
, obj
);
744 opts
= GET_PROP(obj
, options
);
746 array_init(return_value
);
748 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
749 array_copy(*headers
, return_value
);
754 /* {{{ proto void HttpRequest::unsetHeaders()
756 * Unset previously set request headers.
758 PHP_METHOD(HttpRequest
, unsetHeaders
)
761 getObject(http_request_object
, obj
);
765 opts
= GET_PROP(obj
, options
);
766 zend_hash_del(Z_ARRVAL_P(opts
), "headers", sizeof("headers"));
770 /* {{{ proto bool HttpRequest::addCookies(array cookies)
774 PHP_METHOD(HttpRequest
, addCookies
)
776 zval
*opts
, **cookies
, *new_cookies
;
777 getObject(http_request_object
, obj
);
779 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
783 opts
= GET_PROP(obj
, options
);
785 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
786 array_merge(new_cookies
, *cookies
);
788 zval_add_ref(&new_cookies
);
789 add_assoc_zval(opts
, "cookies", new_cookies
);
796 /* {{{ proto array HttpRequest::getCookies()
798 * Get previously set cookies.
800 PHP_METHOD(HttpRequest
, getCookies
)
802 zval
*opts
, **cookies
;
803 getObject(http_request_object
, obj
);
807 opts
= GET_PROP(obj
, options
);
809 array_init(return_value
);
811 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
812 array_copy(*cookies
, return_value
);
817 /* {{{ proto void HttpRequest::unsetCookies()
820 PHP_METHOD(HttpRequest
, unsetCookies
)
823 getObject(http_request_object
, obj
);
827 opts
= GET_PROP(obj
, options
);
828 zend_hash_del(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"));
832 /* {{{ proto bool HttpRequest::setURL(string url)
834 * Set the request URL.
836 PHP_METHOD(HttpRequest
, setURL
)
840 getObject(http_request_object
, obj
);
842 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
846 UPD_PROP(obj
, string
, url
, URL
);
851 /* {{{ proto string HttpRequest::getUrl()
853 * Get the previously set request URL.
855 PHP_METHOD(HttpRequest
, getURL
)
858 getObject(http_request_object
, obj
);
862 URL
= GET_PROP(obj
, url
);
863 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
867 /* {{{ proto bool HttpRequest::setMethod(long request_method)
869 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
870 * <tt>HTTP_POST</tt> constants.
872 PHP_METHOD(HttpRequest
, setMethod
)
875 getObject(http_request_object
, obj
);
877 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
881 UPD_PROP(obj
, long, method
, meth
);
886 /* {{{ proto long HttpRequest::getMethod()
888 * Get the previously set request method.
890 PHP_METHOD(HttpRequest
, getMethod
)
893 getObject(http_request_object
, obj
);
897 meth
= GET_PROP(obj
, method
);
898 RETURN_LONG(Z_LVAL_P(meth
));
902 /* {{{ proto bool HttpRequest::setContentType(string content_type)
904 * Set the content type the post request should have.
905 * Use this only if you know what you're doing.
907 PHP_METHOD(HttpRequest
, setContentType
)
911 getObject(http_request_object
, obj
);
913 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
917 if (!strchr(ctype
, '/')) {
918 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
919 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
924 UPD_PROP(obj
, string
, contentType
, ctype
);
929 /* {{{ proto string HttpRequest::getContentType()
931 * Get the previously content type.
933 PHP_METHOD(HttpRequest
, getContentType
)
936 getObject(http_request_object
, obj
);
940 ctype
= GET_PROP(obj
, contentType
);
941 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
945 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
947 * Set the URL query parameters to use.
948 * Overwrites previously set query parameters.
949 * Affects any request types.
951 PHP_METHOD(HttpRequest
, setQueryData
)
954 char *query_data
= NULL
;
955 getObject(http_request_object
, obj
);
957 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
961 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
962 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
965 UPD_PROP(obj
, string
, queryData
, query_data
);
970 convert_to_string(qdata
);
971 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
976 /* {{{ proto string HttpRequest::getQueryData()
978 * Get the current query data in form of an urlencoded query string.
980 PHP_METHOD(HttpRequest
, getQueryData
)
983 getObject(http_request_object
, obj
);
987 qdata
= GET_PROP(obj
, queryData
);
988 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
992 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
994 * Add parameters to the query parameter list.
995 * Affects any request type.
997 PHP_METHOD(HttpRequest
, addQueryData
)
999 zval
*qdata
, *old_qdata
;
1000 char *query_data
= NULL
;
1001 getObject(http_request_object
, obj
);
1003 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1007 old_qdata
= GET_PROP(obj
, queryData
);
1009 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
1013 UPD_PROP(obj
, string
, queryData
, query_data
);
1020 /* {{{ proto void HttpRequest::unsetQueryData()
1022 * Clean the query parameters.
1023 * Affects any request type.
1025 PHP_METHOD(HttpRequest
, unsetQueryData
)
1027 getObject(http_request_object
, obj
);
1031 UPD_PROP(obj
, string
, queryData
, "");
1035 /* {{{ proto bool HttpRequest::addPostData(array post_data)
1037 * Adds POST data entries.
1038 * Affects only POST requests.
1040 PHP_METHOD(HttpRequest
, addPostData
)
1042 zval
*post
, *post_data
;
1043 getObject(http_request_object
, obj
);
1045 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1049 post
= GET_PROP(obj
, postData
);
1050 array_merge(post_data
, post
);
1056 /* {{{ proto bool HttpRequest::setPostData(array post_data)
1058 * Set the POST data entries.
1059 * Overwrites previously set POST data.
1060 * Affects only POST requests.
1062 PHP_METHOD(HttpRequest
, setPostData
)
1064 zval
*post
, *post_data
;
1065 getObject(http_request_object
, obj
);
1067 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1071 post
= GET_PROP(obj
, postData
);
1072 zend_hash_clean(Z_ARRVAL_P(post
));
1073 array_copy(post_data
, post
);
1079 /* {{{ proto array HttpRequest::getPostData()
1081 * Get previously set POST data.
1083 PHP_METHOD(HttpRequest
, getPostData
)
1086 getObject(http_request_object
, obj
);
1090 post_data
= GET_PROP(obj
, postData
);
1091 array_init(return_value
);
1092 array_copy(post_data
, return_value
);
1096 /* {{{ proto void HttpRequest::unsetPostData()
1098 * Clean POST data entires.
1099 * Affects only POST requests.
1101 PHP_METHOD(HttpRequest
, unsetPostData
)
1104 getObject(http_request_object
, obj
);
1108 post_data
= GET_PROP(obj
, postData
);
1109 zend_hash_clean(Z_ARRVAL_P(post_data
));
1113 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1115 * Add a file to the POST request.
1116 * Affects only POST requests.
1118 PHP_METHOD(HttpRequest
, addPostFile
)
1120 zval
*files
, *entry
;
1121 char *name
, *file
, *type
= NULL
;
1122 int name_len
, file_len
, type_len
= 0;
1123 getObject(http_request_object
, obj
);
1125 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1130 if (!strchr(type
, '/')) {
1131 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
1135 type
= "application/x-octetstream";
1136 type_len
= sizeof("application/x-octetstream") - 1;
1139 MAKE_STD_ZVAL(entry
);
1142 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1143 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1144 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1146 files
= GET_PROP(obj
, postFiles
);
1147 add_next_index_zval(files
, entry
);
1153 /* {{{ proto array HttpRequest::getPostFiles()
1155 * Get all previously added POST files.
1157 PHP_METHOD(HttpRequest
, getPostFiles
)
1160 getObject(http_request_object
, obj
);
1164 files
= GET_PROP(obj
, postFiles
);
1166 array_init(return_value
);
1167 array_copy(files
, return_value
);
1171 /* {{{ proto void HttpRequest::unsetPostFiles()
1173 * Unset the POST files list.
1174 * Affects only POST requests.
1176 PHP_METHOD(HttpRequest
, unsetPostFiles
)
1179 getObject(http_request_object
, obj
);
1183 files
= GET_PROP(obj
, postFiles
);
1184 zend_hash_clean(Z_ARRVAL_P(files
));
1188 /* {{{ proto array HttpRequest::getResponseData()
1190 * Get all response data after the request has been sent.
1192 PHP_METHOD(HttpRequest
, getResponseData
)
1195 getObject(http_request_object
, obj
);
1199 data
= GET_PROP(obj
, responseData
);
1200 array_init(return_value
);
1201 array_copy(data
, return_value
);
1205 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1207 * Get response header(s) after the request has been sent.
1209 PHP_METHOD(HttpRequest
, getResponseHeader
)
1211 zval
*data
, **headers
, **header
;
1212 char *header_name
= NULL
;
1214 getObject(http_response_object
, obj
);
1216 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1220 data
= GET_PROP(obj
, responseData
);
1221 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1225 if (!header_len
|| !header_name
) {
1226 array_init(return_value
);
1227 array_copy(*headers
, return_value
);
1228 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1229 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1236 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1238 * Get response cookie(s) after the request has been sent.
1240 PHP_METHOD(HttpRequest
, getResponseCookie
)
1242 zval
*data
, **headers
;
1243 char *cookie_name
= NULL
;
1245 getObject(http_request_object
, obj
);
1247 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1251 array_init(return_value
);
1253 data
= GET_PROP(obj
, responseData
);
1254 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1257 zval
**header
= NULL
;
1259 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1260 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1261 /* several cookies? */
1262 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1265 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1267 MAKE_STD_ZVAL(cookie_hash
);
1268 array_init(cookie_hash
);
1270 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1272 add_next_index_zval(return_value
, cookie_hash
);
1276 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1277 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1278 add_next_index_zval(return_value
, cookie_hash
);
1279 return; /* <<< FOUND >>> */
1281 zval_dtor(cookie_hash
);
1286 zval_dtor(cookie_hash
);
1292 MAKE_STD_ZVAL(cookie_hash
);
1293 array_init(cookie_hash
);
1295 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1297 add_next_index_zval(return_value
, cookie_hash
);
1301 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1302 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1303 add_next_index_zval(return_value
, cookie_hash
);
1305 zval_dtor(cookie_hash
);
1310 zval_dtor(cookie_hash
);
1323 /* {{{ proto string HttpRequest::getResponseBody()
1325 * Get the response body after the request has been sent.
1327 PHP_METHOD(HttpRequest
, getResponseBody
)
1330 getObject(http_request_object
, obj
);
1334 data
= GET_PROP(obj
, responseData
);
1335 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1336 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
1343 /* {{{ proto int HttpRequest::getResponseCode()
1345 * Get the response code after the request has been sent.
1347 PHP_METHOD(HttpRequest
, getResponseCode
)
1349 zval
**code
, **hdrs
, *data
;
1350 getObject(http_request_object
, obj
);
1354 data
= GET_PROP(obj
, responseData
);
1355 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &hdrs
)) &&
1356 (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(hdrs
), "Status", sizeof("Status"), (void **) &code
))) {
1357 RETVAL_STRINGL(Z_STRVAL_PP(code
), Z_STRLEN_PP(code
), 1);
1358 convert_to_long(return_value
);
1365 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1367 * Get response info after the request has been sent.
1368 * See http_get() for a full list of returned info.
1370 PHP_METHOD(HttpRequest
, getResponseInfo
)
1372 zval
*info
, **infop
;
1373 char *info_name
= NULL
;
1375 getObject(http_request_object
, obj
);
1377 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1381 info
= GET_PROP(obj
, responseInfo
);
1383 if (info_len
&& info_name
) {
1384 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1385 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
1387 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Could not find response info named %s", info_name
);
1391 array_init(return_value
);
1392 array_copy(info
, return_value
);
1397 /* {{{ proto bool HttpRequest::send()
1399 * Send the HTTP request.
1404 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1405 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1406 * $r->addQueryData(array('category' => 3));
1407 * if ($r->send() && $r->getResponseCode() == 200) {
1408 * file_put_contents('local.rss', $r->getResponseBody());
1416 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1417 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1418 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
1419 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1421 * echo $r->getResponseBody();
1426 PHP_METHOD(HttpRequest
, send
)
1428 STATUS status
= FAILURE
;
1429 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
1430 char *response_data
, *request_uri
;
1431 size_t response_len
;
1432 getObject(http_request_object
, obj
);
1436 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
1437 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initilaize cURL");
1441 meth
= GET_PROP(obj
, method
);
1442 URL
= GET_PROP(obj
, url
);
1443 qdata
= GET_PROP(obj
, queryData
);
1444 opts
= GET_PROP(obj
, options
);
1445 info
= GET_PROP(obj
, responseInfo
);
1446 resp
= GET_PROP(obj
, responseData
);
1448 // HTTP_URI_MAXLEN+1 long char *
1449 request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0);
1451 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
1452 if (!strchr(request_uri
, '?')) {
1453 strcat(request_uri
, "?");
1455 strcat(request_uri
, "&");
1457 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
1460 switch (Z_LVAL_P(meth
))
1463 status
= http_get_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1467 status
= http_head_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1472 zval
*post_files
, *post_data
;
1474 post_files
= GET_PROP(obj
, postFiles
);
1475 post_data
= GET_PROP(obj
, postData
);
1477 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files
))) {
1479 /* urlencoded post */
1480 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
);
1490 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
1493 FOREACH_KEYVAL(post_data
, key
, idx
, data
) {
1495 convert_to_string_ex(data
);
1496 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1497 CURLFORM_COPYNAME
, key
,
1498 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
1499 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
1509 FOREACH_VAL(post_files
, data
) {
1510 zval
**file
, **type
, **name
;
1512 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
1513 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
1514 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
1516 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1517 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
1518 CURLFORM_FILE
, Z_STRVAL_PP(file
),
1519 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
1525 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
);
1526 curl_formfree(http_post_data
[0]);
1537 /* final data handling */
1538 if (status
!= SUCCESS
) {
1542 size_t body_len
= 0;
1545 MAKE_STD_ZVAL(zheaders
)
1546 array_init(zheaders
);
1548 if (SUCCESS
!= http_split_response_ex(response_data
, response_len
, Z_ARRVAL_P(zheaders
), &body
, &body_len
)) {
1549 zval_dtor(zheaders
);
1551 efree(response_data
);
1555 add_assoc_zval(resp
, "headers", zheaders
);
1556 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
1558 efree(response_data
);
1566 #endif /* HTTP_HAVE_CURL */
1568 #endif /* ZEND_ENGINE_2 */
1575 * vim600: noet sw=4 ts=4 fdm=marker
1576 * vim<600: noet sw=4 ts=4