2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #define HTTP_WANT_CURL
24 #include "php_http_api.h"
25 #include "php_http_url_api.h"
26 #include "php_http_request_body_api.h"
28 ZEND_EXTERN_MODULE_GLOBALS(http
);
30 /* {{{ http_request_body *http_request_body_new() */
31 PHP_HTTP_API http_request_body
*_http_request_body_new(TSRMLS_D
)
33 return (http_request_body
*) ecalloc(1, sizeof(http_request_body
));
37 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
38 PHP_HTTP_API STATUS
_http_request_body_fill(http_request_body
*body
, HashTable
*fields
, HashTable
*files TSRMLS_DC
)
40 if (files
&& (zend_hash_num_elements(files
) > 0)) {
45 struct curl_httppost
*http_post_data
[2] = {NULL
, NULL
};
48 FOREACH_HASH_KEYVAL(pos
, fields
, key
, idx
, data
) {
53 convert_to_string_ex(data
);
54 err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
55 CURLFORM_COPYNAME
, key
,
56 CURLFORM_COPYCONTENTS
, Z_STRVAL_PP(data
),
57 CURLFORM_CONTENTSLENGTH
, (long) Z_STRLEN_PP(data
),
65 if (CURLE_OK
!= err
) {
66 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post fields: %s", curl_easy_strerror(err
));
67 curl_formfree(http_post_data
[0]);
77 FOREACH_HASH_VAL(pos
, files
, data
) {
78 zval
**file
, **type
, **name
;
80 if (Z_TYPE_PP(data
) != IS_ARRAY
) {
81 http_error(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Unrecognized type of post file array entry");
82 } else if ( SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "name", sizeof("name"), (void **) &name
) ||
83 SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "type", sizeof("type"), (void **) &type
) ||
84 SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(data
), "file", sizeof("file"), (void **) &file
)) {
85 http_error(HE_NOTICE
, HTTP_E_INVALID_PARAM
, "Post file array entry misses either 'name', 'type' or 'file' entry");
87 CURLcode err
= curl_formadd(&http_post_data
[0], &http_post_data
[1],
88 CURLFORM_COPYNAME
, Z_STRVAL_PP(name
),
89 CURLFORM_FILE
, Z_STRVAL_PP(file
),
90 CURLFORM_CONTENTTYPE
, Z_STRVAL_PP(type
),
93 if (CURLE_OK
!= err
) {
94 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post files: %s", curl_easy_strerror(err
));
95 curl_formfree(http_post_data
[0]);
101 body
->type
= HTTP_REQUEST_BODY_CURLPOST
;
102 body
->data
= http_post_data
[0];
109 if (SUCCESS
!= http_urlencode_hash_ex(fields
, 1, NULL
, 0, &encoded
, &encoded_len
)) {
110 http_error(HE_WARNING
, HTTP_E_ENCODING
, "Could not encode post data");
114 body
->type
= HTTP_REQUEST_BODY_CSTRING
;
115 body
->data
= encoded
;
116 body
->size
= encoded_len
;
123 /* {{{ void http_request_body_dtor(http_request_body *) */
124 PHP_HTTP_API
void _http_request_body_dtor(http_request_body
*body TSRMLS_DC
)
129 case HTTP_REQUEST_BODY_CSTRING
:
135 case HTTP_REQUEST_BODY_CURLPOST
:
136 curl_formfree(body
->data
);
139 case HTTP_REQUEST_BODY_UPLOADFILE
:
140 php_stream_close(body
->data
);
147 /* {{{ void http_request_body_free(http_request_body *) */
148 PHP_HTTP_API
void _http_request_body_free(http_request_body
**body TSRMLS_DC
)
151 http_request_body_dtor(*body
);
158 #endif /* HTTP_HAVE_CURL */
165 * vim600: noet sw=4 ts=4 fdm=marker
166 * vim<600: noet sw=4 ts=4