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"
33 #include "php_http_message_object.h"
34 #include "php_http_response_object.h"
35 #include "php_http_request_object.h"
39 /* {{{ HttpResponse */
41 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
43 * Instantiates a new HttpResponse object, which can be used to send
44 * any data/resource/file to an HTTP client with caching and multiple
45 * ranges/resuming support.
47 * NOTE: GZIPping is not implemented yet.
49 PHP_METHOD(HttpResponse
, __construct
)
51 zend_bool do_cache
= 0, do_gzip
= 0;
52 getObject(http_response_object
, obj
);
54 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bb", &do_cache
, &do_gzip
)) {
59 UPD_PROP(obj
, long, cache
, do_cache
);
60 UPD_PROP(obj
, long, gzip
, do_gzip
);
64 /* {{{ proto bool HttpResponse::setCache(bool cache)
66 * Whether it sould be attempted to cache the entitity.
67 * This will result in necessary caching headers and checks of clients
68 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
69 * matches a "304 Not Modified" status code will be issued.
71 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
72 * to something more appropriate than "no-cache"!
74 PHP_METHOD(HttpResponse
, setCache
)
76 zend_bool do_cache
= 0;
77 getObject(http_response_object
, obj
);
79 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_cache
)) {
83 UPD_PROP(obj
, long, cache
, do_cache
);
88 /* {{{ proto bool HttpResponse::getCache()
90 * Get current caching setting.
92 PHP_METHOD(HttpResponse
, getCache
)
94 zval
*do_cache
= NULL
;
95 getObject(http_response_object
, obj
);
99 do_cache
= GET_PROP(obj
, cache
);
100 RETURN_BOOL(Z_LVAL_P(do_cache
));
104 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
106 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
108 PHP_METHOD(HttpResponse
, setGzip
)
110 zend_bool do_gzip
= 0;
111 getObject(http_response_object
, obj
);
113 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "b", &do_gzip
)) {
117 UPD_PROP(obj
, long, gzip
, do_gzip
);
122 /* {{{ proto bool HttpResponse::getGzip()
124 * Get current gzipping setting.
126 PHP_METHOD(HttpResponse
, getGzip
)
128 zval
*do_gzip
= NULL
;
129 getObject(http_response_object
, obj
);
133 do_gzip
= GET_PROP(obj
, gzip
);
134 RETURN_BOOL(Z_LVAL_P(do_gzip
));
138 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
140 * Set a custom cache-control header, usually being "private" or "public"; if
141 * $raw is set to true the header will be sent as-is.
143 PHP_METHOD(HttpResponse
, setCacheControl
)
148 getObject(http_response_object
, obj
);
150 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &ccontrol
, &cc_len
, &raw
)) {
154 if ((!raw
) && (strcmp(ccontrol
, "public") && strcmp(ccontrol
, "private") && strcmp(ccontrol
, "no-cache"))) {
155 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol
);
159 UPD_PROP(obj
, long, raw_cache_header
, raw
);
160 UPD_PROP(obj
, string
, cacheControl
, ccontrol
);
165 /* {{{ proto string HttpResponse::getCacheControl()
167 * Get current Cache-Control header setting.
169 PHP_METHOD(HttpResponse
, getCacheControl
)
172 getObject(http_response_object
, obj
);
176 ccontrol
= GET_PROP(obj
, cacheControl
);
177 RETURN_STRINGL(Z_STRVAL_P(ccontrol
), Z_STRLEN_P(ccontrol
), 1);
181 /* {{{ proto bool HttpResponse::setContentType(string content_type)
183 * Set the content-type of the sent entity.
185 PHP_METHOD(HttpResponse
, setContentType
)
189 getObject(http_response_object
, obj
);
191 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ctype_len
)) {
195 if (!strchr(ctype
, '/')) {
196 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
197 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype
);
201 UPD_PROP(obj
, string
, contentType
, ctype
);
207 /* {{{ proto string HttpResponse::getContentType()
209 * Get current Content-Type header setting.
211 PHP_METHOD(HttpResponse
, getContentType
)
214 getObject(http_response_object
, obj
);
218 ctype
= GET_PROP(obj
, contentType
);
219 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
223 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
225 * Set the Content-Disposition of the sent entity. This setting aims to suggest
226 * the receiveing user agent how to handle the sent entity; usually the client
227 * will show the user a "Save As..." popup.
229 PHP_METHOD(HttpResponse
, setContentDisposition
)
233 zend_bool is_inline
= 0;
234 getObject(http_response_object
, obj
);
236 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &file
, &file_len
, &is_inline
)) {
240 UPD_PROP(obj
, string
, dispoFile
, file
);
241 UPD_PROP(obj
, long, dispoInline
, is_inline
);
246 /* {{{ proto array HttpResponse::getContentDisposition()
248 * Get current Content-Disposition setting.
249 * Will return an associative array like:
252 * 'filename' => 'foo.bar',
257 PHP_METHOD(HttpResponse
, getContentDisposition
)
261 getObject(http_response_object
, obj
);
263 if (ZEND_NUM_ARGS()) {
267 file
= GET_PROP(obj
, dispoFile
);
268 is_inline
= GET_PROP(obj
, dispoInline
);
270 array_init(return_value
);
271 add_assoc_stringl(return_value
, "filename", Z_STRVAL_P(file
), Z_STRLEN_P(file
), 1);
272 add_assoc_bool(return_value
, "inline", Z_LVAL_P(is_inline
));
276 /* {{{ proto bool HttpResponse::setETag(string etag)
278 * Set a custom ETag. Use this only if you know what you're doing.
280 PHP_METHOD(HttpResponse
, setETag
)
284 getObject(http_response_object
, obj
);
286 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
)) {
290 UPD_PROP(obj
, string
, eTag
, etag
);
295 /* {{{ proto string HttpResponse::getETag()
297 * Get the previously set custom ETag.
299 PHP_METHOD(HttpResponse
, getETag
)
302 getObject(http_response_object
, obj
);
306 etag
= GET_PROP(obj
, eTag
);
307 RETURN_STRINGL(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), 1);
311 /* {{{ proto bool HttpResponse::setData(string data)
313 * Set the data to be sent.
315 PHP_METHOD(HttpResponse
, setData
)
318 getObject(http_response_object
, obj
);
320 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_data
)) {
324 convert_to_string_ex(&the_data
);
325 SET_PROP(obj
, data
, the_data
);
326 UPD_PROP(obj
, long, lastModified
, http_lmod(the_data
, SEND_DATA
));
327 UPD_PROP(obj
, long, send_mode
, SEND_DATA
);
332 /* {{{ proto string HttpResponse::getData()
334 * Get the previously set data to be sent.
336 PHP_METHOD(HttpResponse
, getData
)
339 getObject(http_response_object
, obj
);
343 the_data
= GET_PROP(obj
, data
);
344 RETURN_STRINGL(Z_STRVAL_P(the_data
), Z_STRLEN_P(the_data
), 1);
348 /* {{{ proto bool HttpResponse::setStream(resource stream)
350 * Set the resource to be sent.
352 PHP_METHOD(HttpResponse
, setStream
)
355 php_stream
*the_real_stream
;
356 getObject(http_response_object
, obj
);
358 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &the_stream
)) {
362 php_stream_from_zval(the_real_stream
, &the_stream
);
364 SET_PROP(obj
, stream
, the_stream
);
365 UPD_PROP(obj
, long, lastModified
, http_lmod(the_real_stream
, SEND_RSRC
));
366 UPD_PROP(obj
, long, send_mode
, SEND_RSRC
);
371 /* {{{ proto resource HttpResponse::getStream()
373 * Get the previously set resource to be sent.
375 PHP_METHOD(HttpResponse
, getStream
)
378 getObject(http_response_object
, obj
);
382 the_stream
= GET_PROP(obj
, stream
);
383 RETURN_RESOURCE(Z_LVAL_P(the_stream
));
387 /* {{{ proto bool HttpResponse::setFile(string file)
389 * Set the file to be sent.
391 PHP_METHOD(HttpResponse
, setFile
)
394 getObject(http_response_object
, obj
);
396 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &the_file
)) {
400 convert_to_string_ex(&the_file
);
402 UPD_PROP(obj
, string
, file
, Z_STRVAL_P(the_file
));
403 UPD_PROP(obj
, long, lastModified
, http_lmod(the_file
, -1));
404 UPD_PROP(obj
, long, send_mode
, -1);
409 /* {{{ proto string HttpResponse::getFile()
411 * Get the previously set file to be sent.
413 PHP_METHOD(HttpResponse
, getFile
)
416 getObject(http_response_object
, obj
);
420 the_file
= GET_PROP(obj
, file
);
421 RETURN_STRINGL(Z_STRVAL_P(the_file
), Z_STRLEN_P(the_file
), 1);
425 /* {{{ proto bool HttpResponse::send()
427 * Finally send the entity.
432 * $r = new HttpResponse(true);
433 * $r->setFile('../hidden/contract.pdf');
434 * $r->setContentType('application/pdf');
440 PHP_METHOD(HttpResponse
, send
)
442 zval
*do_cache
, *do_gzip
;
443 getObject(http_response_object
, obj
);
447 do_cache
= GET_PROP(obj
, cache
);
448 do_gzip
= GET_PROP(obj
, gzip
);
451 if (Z_LVAL_P(do_gzip
)) {
452 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC
);
456 if (Z_LVAL_P(do_cache
)) {
457 zval
*cctrl
, *etag
, *lmod
, *ccraw
;
459 etag
= GET_PROP(obj
, eTag
);
460 lmod
= GET_PROP(obj
, lastModified
);
461 cctrl
= GET_PROP(obj
, cacheControl
);
462 ccraw
= GET_PROP(obj
, raw_cache_header
);
464 if (Z_LVAL_P(ccraw
)) {
465 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), Z_STRVAL_P(cctrl
), Z_STRLEN_P(cctrl
));
466 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
));
468 char cc_header
[42] = {0};
469 sprintf(cc_header
, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl
));
470 http_cache_etag(Z_STRVAL_P(etag
), Z_STRLEN_P(etag
), cc_header
, strlen(cc_header
));
471 http_cache_last_modified(Z_LVAL_P(lmod
), Z_LVAL_P(lmod
) ? Z_LVAL_P(lmod
) : time(NULL
), cc_header
, strlen(cc_header
));
477 zval
*ctype
= GET_PROP(obj
, contentType
);
478 if (Z_STRLEN_P(ctype
)) {
479 http_send_content_type(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
));
481 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
485 /* content disposition */
487 zval
*dispo_file
= GET_PROP(obj
, dispoFile
);
488 if (Z_STRLEN_P(dispo_file
)) {
489 zval
*dispo_inline
= GET_PROP(obj
, dispoInline
);
490 http_send_content_disposition(Z_STRVAL_P(dispo_file
), Z_STRLEN_P(dispo_file
), (zend_bool
) Z_LVAL_P(dispo_inline
));
496 zval
*send_mode
= GET_PROP(obj
, send_mode
);
497 switch (Z_LVAL_P(send_mode
))
501 zval
*zdata
= GET_PROP(obj
, data
);
502 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
507 php_stream
*the_real_stream
;
508 zval
*the_stream
= GET_PROP(obj
, stream
);
509 php_stream_from_zval(the_real_stream
, &the_stream
);
510 RETURN_SUCCESS(http_send_stream(the_real_stream
));
515 zval
*zfile
= GET_PROP(obj
, file
);
516 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile
)));
524 /* {{{ HttpMessage */
526 /* {{{ void HttpMessage::__construct([string raw_message]) */
527 PHP_METHOD(HttpMessage
, __construct
)
529 zval
*message
= NULL
;
531 getObject(http_message_object
, obj
);
532 http_message
*msg
= obj
->message
;
534 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|z/", &message
)) {
539 SET_PROP(obj
, raw
, message
);
544 /* {{{ void HttpMessage::__destruct() */
545 PHP_METHOD(HttpMessage
, __destruct
)
547 getObject(http_message_object
, obj
);
556 #ifdef HTTP_HAVE_CURL
557 /* {{{ HttpRequest */
559 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
561 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
562 * and POST (including posting files) HTTP requests.
564 PHP_METHOD(HttpRequest
, __construct
)
569 getObject(http_request_object
, obj
);
571 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &URL
, &URL_len
, &meth
)) {
575 INIT_PARR(obj
, options
);
576 INIT_PARR(obj
, responseInfo
);
577 INIT_PARR(obj
, responseData
);
578 INIT_PARR(obj
, postData
);
579 INIT_PARR(obj
, postFiles
);
582 UPD_PROP(obj
, string
, url
, URL
);
585 UPD_PROP(obj
, long, method
, meth
);
590 /* {{{ proto void HttpRequest::__destruct()
592 * Destroys the HttpRequest object.
594 PHP_METHOD(HttpRequest
, __destruct
)
596 getObject(http_request_object
, obj
);
600 FREE_PARR(obj
, options
);
601 FREE_PARR(obj
, responseInfo
);
602 FREE_PARR(obj
, responseData
);
603 FREE_PARR(obj
, postData
);
604 FREE_PARR(obj
, postFiles
);
608 /* {{{ proto bool HttpRequest::setOptions(array options)
610 * Set the request options to use. See http_get() for a full list of available options.
612 PHP_METHOD(HttpRequest
, setOptions
)
616 zval
*opts
, *old_opts
, **opt
;
617 getObject(http_request_object
, obj
);
619 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
623 old_opts
= GET_PROP(obj
, options
);
625 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
626 FOREACH_KEYVAL(opts
, key
, idx
, opt
) {
628 if (!strcmp(key
, "headers")) {
630 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "headers", sizeof("headers"), (void **) &headers
)) {
631 array_merge(*opt
, *headers
);
634 } else if (!strcmp(key
, "cookies")) {
636 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
637 array_merge(*opt
, *cookies
);
642 add_assoc_zval(old_opts
, key
, *opt
);
653 /* {{{ proto array HttpRequest::getOptions()
655 * Get current set options.
657 PHP_METHOD(HttpRequest
, getOptions
)
660 getObject(http_request_object
, obj
);
664 opts
= GET_PROP(obj
, options
);
665 array_init(return_value
);
666 array_copy(opts
, return_value
);
670 /* {{{ proto void HttpRequest::unsetOptions()
672 * Unset all options/headers/cookies.
674 PHP_METHOD(HttpRequest
, unsetOptions
)
676 getObject(http_request_object
, obj
);
680 FREE_PARR(obj
, options
);
681 INIT_PARR(obj
, options
);
685 /* {{{ proto bool HttpRequest::setSslOptions(array options)
687 * Set additional SSL options.
689 PHP_METHOD(HttpRequest
, setSslOptions
)
691 zval
*opts
, *old_opts
, **ssl_options
;
692 getObject(http_request_object
, obj
);
694 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &opts
)) {
698 old_opts
= GET_PROP(obj
, options
);
700 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(old_opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
701 array_merge(opts
, *ssl_options
);
704 add_assoc_zval(old_opts
, "ssl", opts
);
711 /* {{{ proto array HttpRequest::getSslOtpions()
713 * Get previously set SSL options.
715 PHP_METHOD(HttpRequest
, getSslOptions
)
717 zval
*opts
, **ssl_options
;
718 getObject(http_request_object
, obj
);
722 opts
= GET_PROP(obj
, options
);
724 array_init(return_value
);
726 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"), (void **) &ssl_options
)) {
727 array_copy(*ssl_options
, return_value
);
732 /* {{{ proto void HttpRequest::unsetSslOptions()
734 * Unset previously set SSL options.
736 PHP_METHOD(HttpRequest
, unsetSslOptions
)
739 getObject(http_request_object
, obj
);
743 opts
= GET_PROP(obj
, options
);
744 zend_hash_del(Z_ARRVAL_P(opts
), "ssl", sizeof("ssl"));
748 /* {{{ proto bool HttpRequest::addHeaders(array headers)
750 * Add request header name/value pairs.
752 PHP_METHOD(HttpRequest
, addHeaders
)
754 zval
*opts
, **headers
, *new_headers
;
755 getObject(http_request_object
, obj
);
757 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_headers
)) {
761 opts
= GET_PROP(obj
, options
);
763 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
764 array_merge(new_headers
, *headers
);
766 zval_add_ref(&new_headers
);
767 add_assoc_zval(opts
, "headers", new_headers
);
774 /* {{{ proto array HttpRequest::getHeaders()
776 * Get previously set request headers.
778 PHP_METHOD(HttpRequest
, getHeaders
)
780 zval
*opts
, **headers
;
781 getObject(http_request_object
, obj
);
785 opts
= GET_PROP(obj
, options
);
787 array_init(return_value
);
789 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "headers", sizeof("headers"), (void **) &headers
)) {
790 array_copy(*headers
, return_value
);
795 /* {{{ proto void HttpRequest::unsetHeaders()
797 * Unset previously set request headers.
799 PHP_METHOD(HttpRequest
, unsetHeaders
)
802 getObject(http_request_object
, obj
);
806 opts
= GET_PROP(obj
, options
);
807 zend_hash_del(Z_ARRVAL_P(opts
), "headers", sizeof("headers"));
811 /* {{{ proto bool HttpRequest::addCookies(array cookies)
815 PHP_METHOD(HttpRequest
, addCookies
)
817 zval
*opts
, **cookies
, *new_cookies
;
818 getObject(http_request_object
, obj
);
820 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a/", &new_cookies
)) {
824 opts
= GET_PROP(obj
, options
);
826 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
827 array_merge(new_cookies
, *cookies
);
829 zval_add_ref(&new_cookies
);
830 add_assoc_zval(opts
, "cookies", new_cookies
);
837 /* {{{ proto array HttpRequest::getCookies()
839 * Get previously set cookies.
841 PHP_METHOD(HttpRequest
, getCookies
)
843 zval
*opts
, **cookies
;
844 getObject(http_request_object
, obj
);
848 opts
= GET_PROP(obj
, options
);
850 array_init(return_value
);
852 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"), (void **) &cookies
)) {
853 array_copy(*cookies
, return_value
);
858 /* {{{ proto void HttpRequest::unsetCookies()
861 PHP_METHOD(HttpRequest
, unsetCookies
)
864 getObject(http_request_object
, obj
);
868 opts
= GET_PROP(obj
, options
);
869 zend_hash_del(Z_ARRVAL_P(opts
), "cookies", sizeof("cookies"));
873 /* {{{ proto bool HttpRequest::setURL(string url)
875 * Set the request URL.
877 PHP_METHOD(HttpRequest
, setURL
)
881 getObject(http_request_object
, obj
);
883 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &URL
, &URL_len
)) {
887 UPD_PROP(obj
, string
, url
, URL
);
892 /* {{{ proto string HttpRequest::getUrl()
894 * Get the previously set request URL.
896 PHP_METHOD(HttpRequest
, getURL
)
899 getObject(http_request_object
, obj
);
903 URL
= GET_PROP(obj
, url
);
904 RETURN_STRINGL(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), 1);
908 /* {{{ proto bool HttpRequest::setMethod(long request_method)
910 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
911 * <tt>HTTP_POST</tt> constants.
913 PHP_METHOD(HttpRequest
, setMethod
)
916 getObject(http_request_object
, obj
);
918 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &meth
)) {
922 UPD_PROP(obj
, long, method
, meth
);
927 /* {{{ proto long HttpRequest::getMethod()
929 * Get the previously set request method.
931 PHP_METHOD(HttpRequest
, getMethod
)
934 getObject(http_request_object
, obj
);
938 meth
= GET_PROP(obj
, method
);
939 RETURN_LONG(Z_LVAL_P(meth
));
943 /* {{{ proto bool HttpRequest::setContentType(string content_type)
945 * Set the content type the post request should have.
946 * Use this only if you know what you're doing.
948 PHP_METHOD(HttpRequest
, setContentType
)
952 getObject(http_request_object
, obj
);
954 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &ctype
, &ct_len
)) {
958 if (!strchr(ctype
, '/')) {
959 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
960 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
965 UPD_PROP(obj
, string
, contentType
, ctype
);
970 /* {{{ proto string HttpRequest::getContentType()
972 * Get the previously content type.
974 PHP_METHOD(HttpRequest
, getContentType
)
977 getObject(http_request_object
, obj
);
981 ctype
= GET_PROP(obj
, contentType
);
982 RETURN_STRINGL(Z_STRVAL_P(ctype
), Z_STRLEN_P(ctype
), 1);
986 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
988 * Set the URL query parameters to use.
989 * Overwrites previously set query parameters.
990 * Affects any request types.
992 PHP_METHOD(HttpRequest
, setQueryData
)
995 char *query_data
= NULL
;
996 getObject(http_request_object
, obj
);
998 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &qdata
)) {
1002 if ((Z_TYPE_P(qdata
) == IS_ARRAY
) || (Z_TYPE_P(qdata
) == IS_OBJECT
)) {
1003 if (SUCCESS
!= http_urlencode_hash(HASH_OF(qdata
), &query_data
)) {
1006 UPD_PROP(obj
, string
, queryData
, query_data
);
1011 convert_to_string(qdata
);
1012 UPD_PROP(obj
, string
, queryData
, Z_STRVAL_P(qdata
));
1017 /* {{{ proto string HttpRequest::getQueryData()
1019 * Get the current query data in form of an urlencoded query string.
1021 PHP_METHOD(HttpRequest
, getQueryData
)
1024 getObject(http_request_object
, obj
);
1028 qdata
= GET_PROP(obj
, queryData
);
1029 RETURN_STRINGL(Z_STRVAL_P(qdata
), Z_STRLEN_P(qdata
), 1);
1033 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1035 * Add parameters to the query parameter list.
1036 * Affects any request type.
1038 PHP_METHOD(HttpRequest
, addQueryData
)
1040 zval
*qdata
, *old_qdata
;
1041 char *query_data
= NULL
;
1042 getObject(http_request_object
, obj
);
1044 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &qdata
)) {
1048 old_qdata
= GET_PROP(obj
, queryData
);
1050 if (SUCCESS
!= http_urlencode_hash_ex(HASH_OF(qdata
), 1, Z_STRVAL_P(old_qdata
), Z_STRLEN_P(old_qdata
), &query_data
, NULL
)) {
1054 UPD_PROP(obj
, string
, queryData
, query_data
);
1061 /* {{{ proto void HttpRequest::unsetQueryData()
1063 * Clean the query parameters.
1064 * Affects any request type.
1066 PHP_METHOD(HttpRequest
, unsetQueryData
)
1068 getObject(http_request_object
, obj
);
1072 UPD_PROP(obj
, string
, queryData
, "");
1076 /* {{{ proto bool HttpRequest::addPostData(array post_data)
1078 * Adds POST data entries.
1079 * Affects only POST requests.
1081 PHP_METHOD(HttpRequest
, addPostData
)
1083 zval
*post
, *post_data
;
1084 getObject(http_request_object
, obj
);
1086 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1090 post
= GET_PROP(obj
, postData
);
1091 array_merge(post_data
, post
);
1097 /* {{{ proto bool HttpRequest::setPostData(array post_data)
1099 * Set the POST data entries.
1100 * Overwrites previously set POST data.
1101 * Affects only POST requests.
1103 PHP_METHOD(HttpRequest
, setPostData
)
1105 zval
*post
, *post_data
;
1106 getObject(http_request_object
, obj
);
1108 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &post_data
)) {
1112 post
= GET_PROP(obj
, postData
);
1113 zend_hash_clean(Z_ARRVAL_P(post
));
1114 array_copy(post_data
, post
);
1120 /* {{{ proto array HttpRequest::getPostData()
1122 * Get previously set POST data.
1124 PHP_METHOD(HttpRequest
, getPostData
)
1127 getObject(http_request_object
, obj
);
1131 post_data
= GET_PROP(obj
, postData
);
1132 array_init(return_value
);
1133 array_copy(post_data
, return_value
);
1137 /* {{{ proto void HttpRequest::unsetPostData()
1139 * Clean POST data entires.
1140 * Affects only POST requests.
1142 PHP_METHOD(HttpRequest
, unsetPostData
)
1145 getObject(http_request_object
, obj
);
1149 post_data
= GET_PROP(obj
, postData
);
1150 zend_hash_clean(Z_ARRVAL_P(post_data
));
1154 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1156 * Add a file to the POST request.
1157 * Affects only POST requests.
1159 PHP_METHOD(HttpRequest
, addPostFile
)
1161 zval
*files
, *entry
;
1162 char *name
, *file
, *type
= NULL
;
1163 int name_len
, file_len
, type_len
= 0;
1164 getObject(http_request_object
, obj
);
1166 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &name
, &name_len
, &file
, &file_len
, &type
, &type_len
)) {
1171 if (!strchr(type
, '/')) {
1172 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type
);
1176 type
= "application/x-octetstream";
1177 type_len
= sizeof("application/x-octetstream") - 1;
1180 MAKE_STD_ZVAL(entry
);
1183 add_assoc_stringl(entry
, "name", name
, name_len
, 1);
1184 add_assoc_stringl(entry
, "type", type
, type_len
, 1);
1185 add_assoc_stringl(entry
, "file", file
, file_len
, 1);
1187 files
= GET_PROP(obj
, postFiles
);
1188 add_next_index_zval(files
, entry
);
1194 /* {{{ proto array HttpRequest::getPostFiles()
1196 * Get all previously added POST files.
1198 PHP_METHOD(HttpRequest
, getPostFiles
)
1201 getObject(http_request_object
, obj
);
1205 files
= GET_PROP(obj
, postFiles
);
1207 array_init(return_value
);
1208 array_copy(files
, return_value
);
1212 /* {{{ proto void HttpRequest::unsetPostFiles()
1214 * Unset the POST files list.
1215 * Affects only POST requests.
1217 PHP_METHOD(HttpRequest
, unsetPostFiles
)
1220 getObject(http_request_object
, obj
);
1224 files
= GET_PROP(obj
, postFiles
);
1225 zend_hash_clean(Z_ARRVAL_P(files
));
1229 /* {{{ proto array HttpRequest::getResponseData()
1231 * Get all response data after the request has been sent.
1233 PHP_METHOD(HttpRequest
, getResponseData
)
1236 getObject(http_request_object
, obj
);
1240 data
= GET_PROP(obj
, responseData
);
1241 array_init(return_value
);
1242 array_copy(data
, return_value
);
1246 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1248 * Get response header(s) after the request has been sent.
1250 PHP_METHOD(HttpRequest
, getResponseHeader
)
1252 zval
*data
, **headers
, **header
;
1253 char *header_name
= NULL
;
1255 getObject(http_response_object
, obj
);
1257 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &header_name
, &header_len
)) {
1261 data
= GET_PROP(obj
, responseData
);
1262 if (SUCCESS
!= zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1266 if (!header_len
|| !header_name
) {
1267 array_init(return_value
);
1268 array_copy(*headers
, return_value
);
1269 } else if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(headers
), pretty_key(header_name
, header_len
, 1, 1), header_len
+ 1, (void **) &header
)) {
1270 RETURN_STRINGL(Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1277 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1279 * Get response cookie(s) after the request has been sent.
1281 PHP_METHOD(HttpRequest
, getResponseCookie
)
1283 zval
*data
, **headers
;
1284 char *cookie_name
= NULL
;
1286 getObject(http_request_object
, obj
);
1288 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &cookie_name
, &cookie_len
)) {
1292 array_init(return_value
);
1294 data
= GET_PROP(obj
, responseData
);
1295 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &headers
)) {
1298 zval
**header
= NULL
;
1300 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers
), key
, idx
, header
) {
1301 if (key
&& !strcasecmp(key
, "Set-Cookie")) {
1302 /* several cookies? */
1303 if (Z_TYPE_PP(header
) == IS_ARRAY
) {
1306 FOREACH_HASH_VAL(Z_ARRVAL_PP(header
), cookie
) {
1308 MAKE_STD_ZVAL(cookie_hash
);
1309 array_init(cookie_hash
);
1311 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(cookie
), Z_ARRVAL_P(cookie_hash
))) {
1313 add_next_index_zval(return_value
, cookie_hash
);
1317 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1318 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1319 add_next_index_zval(return_value
, cookie_hash
);
1320 return; /* <<< FOUND >>> */
1322 zval_dtor(cookie_hash
);
1327 zval_dtor(cookie_hash
);
1333 MAKE_STD_ZVAL(cookie_hash
);
1334 array_init(cookie_hash
);
1336 if (SUCCESS
== http_parse_cookie(Z_STRVAL_PP(header
), Z_ARRVAL_P(cookie_hash
))) {
1338 add_next_index_zval(return_value
, cookie_hash
);
1342 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(cookie_hash
), "name", sizeof("name"), (void **) &name
)) &&
1343 (!strcmp(Z_STRVAL_PP(name
), cookie_name
))) {
1344 add_next_index_zval(return_value
, cookie_hash
);
1346 zval_dtor(cookie_hash
);
1351 zval_dtor(cookie_hash
);
1364 /* {{{ proto string HttpRequest::getResponseBody()
1366 * Get the response body after the request has been sent.
1368 PHP_METHOD(HttpRequest
, getResponseBody
)
1371 getObject(http_request_object
, obj
);
1375 data
= GET_PROP(obj
, responseData
);
1376 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "body", sizeof("body"), (void **) &body
)) {
1377 RETURN_STRINGL(Z_STRVAL_PP(body
), Z_STRLEN_PP(body
), 1);
1384 /* {{{ proto int HttpRequest::getResponseCode()
1386 * Get the response code after the request has been sent.
1388 PHP_METHOD(HttpRequest
, getResponseCode
)
1390 zval
**code
, **hdrs
, *data
;
1391 getObject(http_request_object
, obj
);
1395 data
= GET_PROP(obj
, responseData
);
1396 if ( (SUCCESS
== zend_hash_find(Z_ARRVAL_P(data
), "headers", sizeof("headers"), (void **) &hdrs
)) &&
1397 (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(hdrs
), "Response Status", sizeof("Response Status"), (void **) &code
))) {
1398 RETVAL_STRINGL(Z_STRVAL_PP(code
), Z_STRLEN_PP(code
), 1);
1399 convert_to_long(return_value
);
1406 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1408 * Get response info after the request has been sent.
1409 * See http_get() for a full list of returned info.
1411 PHP_METHOD(HttpRequest
, getResponseInfo
)
1413 zval
*info
, **infop
;
1414 char *info_name
= NULL
;
1416 getObject(http_request_object
, obj
);
1418 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &info_name
, &info_len
)) {
1422 info
= GET_PROP(obj
, responseInfo
);
1424 if (info_len
&& info_name
) {
1425 if (SUCCESS
== zend_hash_find(Z_ARRVAL_P(info
), pretty_key(info_name
, info_len
, 0, 0), info_len
+ 1, (void **) &infop
)) {
1426 RETURN_ZVAL(*infop
, 1, ZVAL_PTR_DTOR
);
1428 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Could not find response info named %s", info_name
);
1432 array_init(return_value
);
1433 array_copy(info
, return_value
);
1438 /* {{{ proto bool HttpRequest::send()
1440 * Send the HTTP request.
1445 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1446 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1447 * $r->addQueryData(array('category' => 3));
1448 * if ($r->send() && $r->getResponseCode() == 200) {
1449 * file_put_contents('local.rss', $r->getResponseBody());
1457 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1458 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1459 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
1460 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1462 * echo $r->getResponseBody();
1467 PHP_METHOD(HttpRequest
, send
)
1469 STATUS status
= FAILURE
;
1470 zval
*meth
, *URL
, *qdata
, *opts
, *info
, *resp
;
1471 char *response_data
, *request_uri
;
1472 size_t response_len
;
1473 getObject(http_request_object
, obj
);
1477 if ((!obj
->ch
) && (!(obj
->ch
= curl_easy_init()))) {
1478 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initilaize cURL");
1482 meth
= GET_PROP(obj
, method
);
1483 URL
= GET_PROP(obj
, url
);
1484 qdata
= GET_PROP(obj
, queryData
);
1485 opts
= GET_PROP(obj
, options
);
1486 info
= GET_PROP(obj
, responseInfo
);
1487 resp
= GET_PROP(obj
, responseData
);
1489 // HTTP_URI_MAXLEN+1 long char *
1490 request_uri
= http_absolute_uri_ex(Z_STRVAL_P(URL
), Z_STRLEN_P(URL
), NULL
, 0, NULL
, 0, 0);
1492 if (Z_STRLEN_P(qdata
) && (strlen(request_uri
) < HTTP_URI_MAXLEN
)) {
1493 if (!strchr(request_uri
, '?')) {
1494 strcat(request_uri
, "?");
1496 strcat(request_uri
, "&");
1498 strncat(request_uri
, Z_STRVAL_P(qdata
), HTTP_URI_MAXLEN
- strlen(request_uri
));
1501 switch (Z_LVAL_P(meth
))
1504 status
= http_get_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1508 status
= http_head_ex(obj
->ch
, request_uri
, Z_ARRVAL_P(opts
), Z_ARRVAL_P(info
), &response_data
, &response_len
);
1513 zval
*post_files
, *post_data
;
1515 post_files
= GET_PROP(obj
, postFiles
);
1516 post_data
= GET_PROP(obj
, postData
);
1518 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files
))) {
1520 /* urlencoded post */
1521 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
);
1531 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
1534 FOREACH_KEYVAL(post_data
, key
, idx
, data
) {
1536 convert_to_string_ex(data
);
1537 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1538 CURLFORM_COPYNAME
, key
,
1539 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
1540 CURLFORM_CONTENTSLENGTH
, Z_STRLEN_PP(data
),
1550 FOREACH_VAL(post_files
, data
) {
1551 zval
**file
, **type
, **name
;
1553 if ( SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) &&
1554 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) &&
1555 SUCCESS
== zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
1557 curl_formadd(&http_post_data
[0], &http_post_data
[1],
1558 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
1559 CURLFORM_FILE
, Z_STRVAL_PP(file
),
1560 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
1566 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
);
1567 curl_formfree(http_post_data
[0]);
1578 /* final data handling */
1579 if (status
!= SUCCESS
) {
1583 size_t body_len
= 0;
1586 MAKE_STD_ZVAL(zheaders
)
1587 array_init(zheaders
);
1589 if (SUCCESS
!= http_split_response_ex(response_data
, response_len
, Z_ARRVAL_P(zheaders
), &body
, &body_len
)) {
1590 zval_dtor(zheaders
);
1592 efree(response_data
);
1596 add_assoc_zval(resp
, "headers", zheaders
);
1597 add_assoc_stringl(resp
, "body", body
, body_len
, 0);
1599 efree(response_data
);
1607 #endif /* HTTP_HAVE_CURL */
1609 #endif /* ZEND_ENGINE_2 */
1616 * vim600: noet sw=4 ts=4 fdm=marker
1617 * vim<600: noet sw=4 ts=4