aa40ba6b94a2d112e8c86ce3053a5f11e560449b
[m6w6/ext-http] / http_curl_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
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 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #ifdef PHP_WIN32
23 # include <winsock2.h>
24 #endif
25
26 #include <curl/curl.h>
27
28 #include "phpstr/phpstr.h"
29
30 #include "php.h"
31 #include "php_http.h"
32 #include "php_http_std_defs.h"
33 #include "php_http_api.h"
34 #include "php_http_curl_api.h"
35 #include "php_http_url_api.h"
36
37 ZEND_EXTERN_MODULE_GLOBALS(http)
38
39 #if LIBCURL_VERSION_NUM >= 0x070c01
40 # define http_curl_reset(ch) curl_easy_reset(ch)
41 #else
42 # define http_curl_reset(ch)
43 #endif
44
45 #if LIBCURL_VERSION_NUM < 0x070c00
46 # define http_curl_error(dummy) HTTP_G(curlerr)
47 # define curl_easy_strerror(code) "unkown error"
48 #else
49 # define http_curl_error(code) curl_easy_strerror(code)
50 #endif
51
52 #define http_curl_startup(ch, clean_curl, URL, options, response) \
53 if (!ch) { \
54 if (!(ch = curl_easy_init())) { \
55 http_error(E_WARNING, HTTP_E_CURL, "Could not initialize curl"); \
56 return FAILURE; \
57 } \
58 clean_curl = 1; \
59 } else { \
60 http_curl_reset(ch); \
61 } \
62 http_curl_setopts(ch, URL, options, response);
63
64 #define http_curl_perform(ch, clean_curl, response) \
65 { \
66 CURLcode result; \
67 if (CURLE_OK != (result = curl_easy_perform(ch))) { \
68 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not perform request: %s", curl_easy_strerror(result)); \
69 http_curl_cleanup(ch, clean_curl, response); \
70 return FAILURE; \
71 } \
72 }
73
74 #define http_curl_cleanup(ch, clean_curl, response) \
75 zend_llist_clean(&HTTP_G(to_free)); \
76 if (clean_curl) { \
77 curl_easy_cleanup(ch); \
78 ch = NULL; \
79 } \
80 phpstr_fix(PHPSTR(response))
81
82 #define http_curl_copystr(s) _http_curl_copystr((s) TSRMLS_CC)
83 static inline char *_http_curl_copystr(const char *str TSRMLS_DC);
84
85 #define http_curl_setopts(c, u, o, r) _http_curl_setopts((c), (u), (o), (r) TSRMLS_CC)
86 static void _http_curl_setopts(CURL *ch, const char *url, HashTable *options, phpstr *response TSRMLS_DC);
87
88 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
89 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
90 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
91
92 static size_t http_curl_callback(char *, size_t, size_t, void *);
93 static int http_curl_progress(void *, double, double, double, double);
94
95 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
96 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC);
97
98 /* {{{ static inline char *http_curl_copystr(char *) */
99 static inline char *_http_curl_copystr(const char *str TSRMLS_DC)
100 {
101 char *new_str = estrdup(str);
102 zend_llist_add_element(&HTTP_G(to_free), &new_str);
103 return new_str;
104 }
105 /* }}} */
106
107 /* {{{ static size_t http_curl_callback(char *, size_t, size_t, void *) */
108 static size_t http_curl_callback(char *buf, size_t len, size_t n, void *s)
109 {
110 return s ? phpstr_append(PHPSTR(s), buf, len * n) : len * n;
111 }
112 /* }}} */
113
114 /* {{{ static int http_curl_progress(void *, double, double, double, double) */
115 static int http_curl_progress(void *data, double dltotal, double dlnow, double ultotal, double ulnow)
116 {
117 int i;
118 zval *params_pass[4], params_local[4], retval, *func = (zval *) data;
119 TSRMLS_FETCH();
120
121 for (i = 0; i < 5; ++i) {
122 params_pass[i] = &params_local[i];
123 }
124
125 ZVAL_DOUBLE(params_pass[0], dltotal);
126 ZVAL_DOUBLE(params_pass[1], dlnow);
127 ZVAL_DOUBLE(params_pass[2], ultotal);
128 ZVAL_DOUBLE(params_pass[3], ulnow);
129
130 return call_user_function(EG(function_table), NULL, func, &retval, 4, params_pass TSRMLS_CC);
131 }
132 /* }}} */
133
134 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
135 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
136 {
137 zval **zoption;
138
139 if (!options || (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))) {
140 return NULL;
141 }
142
143 if (Z_TYPE_PP(zoption) != type) {
144 switch (type)
145 {
146 case IS_BOOL: convert_to_boolean_ex(zoption); break;
147 case IS_LONG: convert_to_long_ex(zoption); break;
148 case IS_DOUBLE: convert_to_double_ex(zoption); break;
149 case IS_STRING: convert_to_string_ex(zoption); break;
150 case IS_ARRAY: convert_to_array_ex(zoption); break;
151 case IS_OBJECT: convert_to_object_ex(zoption); break;
152 default:
153 break;
154 }
155 }
156
157 return *zoption;
158 }
159 /* }}} */
160
161 /* {{{ static void http_curl_setopts(CURL *, char *, HashTable *, phpstr *) */
162 static void _http_curl_setopts(CURL *ch, const char *url, HashTable *options, phpstr *response TSRMLS_DC)
163 {
164 zval *zoption;
165 zend_bool range_req = 0;
166
167 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
168
169 /* standard options */
170 if (url) {
171 HTTP_CURL_OPT(URL, url);
172 }
173
174 HTTP_CURL_OPT(HEADER, 0);
175 HTTP_CURL_OPT(FILETIME, 1);
176 HTTP_CURL_OPT(NOPROGRESS, 1);
177 HTTP_CURL_OPT(AUTOREFERER, 1);
178 HTTP_CURL_OPT(WRITEFUNCTION, http_curl_callback);
179 HTTP_CURL_OPT(HEADERFUNCTION, http_curl_callback);
180
181 if (response) {
182 HTTP_CURL_OPT(WRITEDATA, response);
183 HTTP_CURL_OPT(WRITEHEADER, response);
184 }
185
186 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
187 HTTP_CURL_OPT(NOSIGNAL, 1);
188 #endif
189 #if LIBCURL_VERSION_NUM < 0x070c00
190 HTTP_CURL_OPT(ERRORBUFFER, HTTP_G(curlerr));
191 #endif
192
193 /* progress callback */
194 if (zoption = http_curl_getopt(options, "onprogress", 0)) {
195 HTTP_CURL_OPT(NOPROGRESS, 0);
196 HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress);
197 HTTP_CURL_OPT(PROGRESSDATA, zoption);
198 } else {
199 HTTP_CURL_OPT(NOPROGRESS, 1);
200 }
201
202 /* proxy */
203 if (zoption = http_curl_getopt(options, "proxyhost", IS_STRING)) {
204 HTTP_CURL_OPT(PROXY, http_curl_copystr(Z_STRVAL_P(zoption)));
205 /* port */
206 if (zoption = http_curl_getopt(options, "proxyport", IS_LONG)) {
207 HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
208 }
209 /* user:pass */
210 if (zoption = http_curl_getopt(options, "proxyauth", IS_STRING)) {
211 HTTP_CURL_OPT(PROXYUSERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
212 }
213 #if LIBCURL_VERSION_NUM >= 0x070a07
214 /* auth method */
215 if (zoption = http_curl_getopt(options, "proxyauthtype", IS_LONG)) {
216 HTTP_CURL_OPT(PROXYAUTH, Z_LVAL_P(zoption));
217 }
218 #endif
219 }
220
221 /* outgoing interface */
222 if (zoption = http_curl_getopt(options, "interface", IS_STRING)) {
223 HTTP_CURL_OPT(INTERFACE, http_curl_copystr(Z_STRVAL_P(zoption)));
224 }
225
226 /* another port */
227 if (zoption = http_curl_getopt(options, "port", IS_LONG)) {
228 HTTP_CURL_OPT(PORT, Z_LVAL_P(zoption));
229 }
230
231 /* auth */
232 if (zoption = http_curl_getopt(options, "httpauth", IS_STRING)) {
233 HTTP_CURL_OPT(USERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
234 }
235 #if LIBCURL_VERSION_NUM >= 0x070a06
236 if (zoption = http_curl_getopt(options, "httpauthtype", IS_LONG)) {
237 HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
238 }
239 #endif
240
241 /* compress, empty string enables deflate and gzip */
242 if (zoption = http_curl_getopt(options, "compress", IS_BOOL)) {
243 if (Z_LVAL_P(zoption)) {
244 HTTP_CURL_OPT(ENCODING, http_curl_copystr(""));
245 }
246 }
247
248 /* redirects, defaults to 0 */
249 if (zoption = http_curl_getopt(options, "redirect", IS_LONG)) {
250 HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
251 HTTP_CURL_OPT(MAXREDIRS, Z_LVAL_P(zoption));
252 if (zoption = http_curl_getopt(options, "unrestrictedauth", IS_BOOL)) {
253 HTTP_CURL_OPT(UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
254 }
255 } else {
256 HTTP_CURL_OPT(FOLLOWLOCATION, 0);
257 }
258
259 /* referer */
260 if (zoption = http_curl_getopt(options, "referer", IS_STRING)) {
261 HTTP_CURL_OPT(REFERER, http_curl_copystr(Z_STRVAL_P(zoption)));
262 }
263
264 /* useragent, default "PECL::HTTP/version (PHP/version)" */
265 if (zoption = http_curl_getopt(options, "useragent", IS_STRING)) {
266 HTTP_CURL_OPT(USERAGENT, http_curl_copystr(Z_STRVAL_P(zoption)));
267 } else {
268 HTTP_CURL_OPT(USERAGENT,
269 "PECL::HTTP/" HTTP_PEXT_VERSION " (PHP/" PHP_VERSION ")");
270 }
271
272 /* additional headers, array('name' => 'value') */
273 if (zoption = http_curl_getopt(options, "headers", IS_ARRAY)) {
274 char *header_key;
275 long header_idx;
276 struct curl_slist *headers = NULL;
277
278 FOREACH_KEY(zoption, header_key, header_idx) {
279 if (header_key) {
280 zval **header_val;
281 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val)) {
282 char header[1024] = {0};
283 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
284 headers = curl_slist_append(headers, http_curl_copystr(header));
285 }
286
287 /* reset */
288 header_key = NULL;
289 }
290 }
291
292 if (headers) {
293 HTTP_CURL_OPT(HTTPHEADER, headers);
294 }
295 }
296
297 /* cookies, array('name' => 'value') */
298 if (zoption = http_curl_getopt(options, "cookies", IS_ARRAY)) {
299 char *cookie_key = NULL;
300 long cookie_idx = 0;
301 phpstr *qstr = phpstr_new();
302
303 FOREACH_KEY(zoption, cookie_key, cookie_idx) {
304 if (cookie_key) {
305 zval **cookie_val;
306 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val)) {
307 phpstr_appendf(qstr, "%s=%s; ", cookie_key, Z_STRVAL_PP(cookie_val));
308 }
309
310 /* reset */
311 cookie_key = NULL;
312 }
313 }
314
315 if (qstr->used) {
316 phpstr_fix(qstr);
317 HTTP_CURL_OPT(COOKIE, http_curl_copystr(qstr->data));
318 }
319 phpstr_free(qstr);
320 }
321
322 /* cookiestore */
323 if (zoption = http_curl_getopt(options, "cookiestore", IS_STRING)) {
324 HTTP_CURL_OPT(COOKIEFILE, http_curl_copystr(Z_STRVAL_P(zoption)));
325 HTTP_CURL_OPT(COOKIEJAR, http_curl_copystr(Z_STRVAL_P(zoption)));
326 }
327
328 /* resume */
329 if (zoption = http_curl_getopt(options, "resume", IS_LONG)) {
330 range_req = 1;
331 HTTP_CURL_OPT(RESUME_FROM, Z_LVAL_P(zoption));
332 }
333
334 /* maxfilesize */
335 if (zoption = http_curl_getopt(options, "maxfilesize", IS_LONG)) {
336 HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
337 }
338
339 /* lastmodified */
340 if (zoption = http_curl_getopt(options, "lastmodified", IS_LONG)) {
341 HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
342 HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
343 }
344
345 /* timeout */
346 if (zoption = http_curl_getopt(options, "timeout", IS_LONG)) {
347 HTTP_CURL_OPT(TIMEOUT, Z_LVAL_P(zoption));
348 }
349
350 /* connecttimeout, defaults to 1 */
351 if (zoption = http_curl_getopt(options, "connecttimeout", IS_LONG)) {
352 HTTP_CURL_OPT(CONNECTTIMEOUT, Z_LVAL_P(zoption));
353 } else {
354 HTTP_CURL_OPT(CONNECTTIMEOUT, 1);
355 }
356
357 /* ssl */
358 if (zoption = http_curl_getopt(options, "ssl", IS_ARRAY)) {
359 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
360 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
361 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
362 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
363 if (!strcasecmp(key, #keyname)) { \
364 convert_to_string_ex(param); \
365 HTTP_CURL_OPT(optname, http_curl_copystr(Z_STRVAL_PP(param))); \
366 key = NULL; \
367 continue; \
368 }
369 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
370 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
371 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
372 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
373 if (!strcasecmp(key, #keyname)) { \
374 convert_to_long_ex(param); \
375 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
376 key = NULL; \
377 continue; \
378 }
379
380 long idx;
381 char *key = NULL;
382 zval **param;
383
384 FOREACH_KEYVAL(zoption, key, idx, param) {
385 if (key) {fprintf(stderr, "%s\n", key);
386 HTTP_CURL_OPT_SSL_STRING(CERT);
387 #if LIBCURL_VERSION_NUM >= 0x070903
388 HTTP_CURL_OPT_SSL_STRING(CERTTYPE);
389 #endif
390 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD);
391
392 HTTP_CURL_OPT_SSL_STRING(KEY);
393 HTTP_CURL_OPT_SSL_STRING(KEYTYPE);
394 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD);
395
396 HTTP_CURL_OPT_SSL_STRING(ENGINE);
397 HTTP_CURL_OPT_SSL_LONG(VERSION);
398
399 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER);
400 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST);
401 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST);
402
403
404 HTTP_CURL_OPT_STRING(CAINFO);
405 #if LIBCURL_VERSION_NUM >= 0x070908
406 HTTP_CURL_OPT_STRING(CAPATH);
407 #endif
408 HTTP_CURL_OPT_STRING(RANDOM_FILE);
409 HTTP_CURL_OPT_STRING(EGDSOCKET);
410
411 /* reset key */
412 key = NULL;
413 }
414 }
415 } else {
416 /* disable SSL verification by default */
417 HTTP_CURL_OPT(SSL_VERIFYPEER, 0);
418 HTTP_CURL_OPT(SSL_VERIFYHOST, 0);
419 }
420 }
421 /* }}} */
422
423 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
424 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC)
425 {
426 zval array;
427 Z_ARRVAL(array) = info;
428
429 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
430 #define HTTP_CURL_INFO_EX(I, X) \
431 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
432 { \
433 case CURLINFO_STRING: \
434 { \
435 char *c; \
436 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
437 add_assoc_string(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
438 } \
439 } \
440 break; \
441 \
442 case CURLINFO_DOUBLE: \
443 { \
444 double d; \
445 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
446 add_assoc_double(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), d); \
447 } \
448 } \
449 break; \
450 \
451 case CURLINFO_LONG: \
452 { \
453 long l; \
454 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
455 add_assoc_long(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), l); \
456 } \
457 } \
458 break; \
459 }
460
461 HTTP_CURL_INFO(EFFECTIVE_URL);
462
463 #if LIBCURL_VERSION_NUM >= 0x070a07
464 HTTP_CURL_INFO(RESPONSE_CODE);
465 #else
466 HTTP_CURL_INFO_EX(HTTP_CODE, RESPONSE_CODE);
467 #endif
468 HTTP_CURL_INFO(HTTP_CONNECTCODE);
469
470 #if LIBCURL_VERSION_NUM >= 0x070500
471 HTTP_CURL_INFO(FILETIME);
472 #endif
473 HTTP_CURL_INFO(TOTAL_TIME);
474 HTTP_CURL_INFO(NAMELOOKUP_TIME);
475 HTTP_CURL_INFO(CONNECT_TIME);
476 HTTP_CURL_INFO(PRETRANSFER_TIME);
477 HTTP_CURL_INFO(STARTTRANSFER_TIME);
478 #if LIBCURL_VERSION_NUM >= 0x070907
479 HTTP_CURL_INFO(REDIRECT_TIME);
480 HTTP_CURL_INFO(REDIRECT_COUNT);
481 #endif
482
483 HTTP_CURL_INFO(SIZE_UPLOAD);
484 HTTP_CURL_INFO(SIZE_DOWNLOAD);
485 HTTP_CURL_INFO(SPEED_DOWNLOAD);
486 HTTP_CURL_INFO(SPEED_UPLOAD);
487
488 HTTP_CURL_INFO(HEADER_SIZE);
489 HTTP_CURL_INFO(REQUEST_SIZE);
490
491 HTTP_CURL_INFO(SSL_VERIFYRESULT);
492 #if LIBCURL_VERSION_NUM >= 0x070c03
493 /*HTTP_CURL_INFO(SSL_ENGINES);
494 todo: CURLINFO_SLIST */
495 #endif
496
497 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
498 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
499 HTTP_CURL_INFO(CONTENT_TYPE);
500
501 #if LIBCURL_VERSION_NUM >= 0x070a03
502 /*HTTP_CURL_INFO(PRIVATE);*/
503 #endif
504
505 #if LIBCURL_VERSION_NUM >= 0x070a08
506 HTTP_CURL_INFO(HTTPAUTH_AVAIL);
507 HTTP_CURL_INFO(PROXYAUTH_AVAIL);
508 #endif
509
510 #if LIBCURL_VERSION_NUM >= 0x070c02
511 /*HTTP_CURL_INFO(OS_ERRNO);*/
512 #endif
513
514 #if LIBCURL_VERSION_NUM >= 0x070c03
515 HTTP_CURL_INFO(NUM_CONNECTS);
516 #endif
517 }
518 /* }}} */
519
520 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, phpstr *) */
521 PHP_HTTP_API STATUS _http_get_ex(CURL *ch, const char *URL, HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
522 {
523 zend_bool clean_curl = 0;
524
525 http_curl_startup(ch, clean_curl, URL, options, response);
526 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1);
527 http_curl_perform(ch, clean_curl, response);
528
529 if (info) {
530 http_curl_getinfo(ch, info);
531 }
532
533 http_curl_cleanup(ch, clean_curl, response);
534
535 return SUCCESS;
536 }
537
538 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, phpstr *) */
539 PHP_HTTP_API STATUS _http_head_ex(CURL *ch, const char *URL, HashTable *options,HashTable *info, phpstr *response TSRMLS_DC)
540 {
541 zend_bool clean_curl = 0;
542
543 http_curl_startup(ch, clean_curl, URL, options, response);
544 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
545 http_curl_perform(ch, clean_curl, response);
546
547 if (info) {
548 http_curl_getinfo(ch, info);
549 }
550
551 http_curl_cleanup(ch, clean_curl, response);
552
553 return SUCCESS;
554 }
555
556 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, phpstr *) */
557 PHP_HTTP_API STATUS _http_post_data_ex(CURL *ch, const char *URL, char *postdata,
558 size_t postdata_len, HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
559 {
560 zend_bool clean_curl = 0;
561
562 http_curl_startup(ch, clean_curl, URL, options, response);
563 curl_easy_setopt(ch, CURLOPT_POST, 1);
564 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, postdata);
565 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, postdata_len);
566 http_curl_perform(ch, clean_curl, response);
567
568 if (info) {
569 http_curl_getinfo(ch, info);
570 }
571
572 http_curl_cleanup(ch, clean_curl, response);
573
574 return SUCCESS;
575 }
576 /* }}} */
577
578 /* {{{ STATUS http_post_array_ex(CURL *, char *, HashTable *, HashTable *, HashTable *, phpstr *) */
579 PHP_HTTP_API STATUS _http_post_array_ex(CURL *ch, const char *URL, HashTable *postarray,
580 HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
581 {
582 STATUS status;
583 char *encoded;
584 size_t encoded_len;
585
586 if (SUCCESS != http_urlencode_hash_ex(postarray, 1, NULL, 0, &encoded, &encoded_len)) {
587 http_error(E_WARNING, HTTP_E_ENCODE, "Could not encode post data");
588 return FAILURE;
589 }
590
591 status = http_post_data_ex(ch, URL, encoded, encoded_len, options, info, response);
592 efree(encoded);
593
594 return status;
595 }
596 /* }}} */
597
598 /* {{{ STATUS http_post_curldata_ex(CURL *, char *, curl_httppost *, HashTable *, HashTable *, phpstr *) */
599 PHP_HTTP_API STATUS _http_post_curldata_ex(CURL *ch, const char *URL, struct curl_httppost *curldata,
600 HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
601 {
602 zend_bool clean_curl = 0;
603
604 http_curl_startup(ch, clean_curl, URL, options, response);
605 curl_easy_setopt(ch, CURLOPT_POST, 1);
606 curl_easy_setopt(ch, CURLOPT_HTTPPOST, curldata);
607 http_curl_perform(ch, clean_curl, response);
608
609 if (info) {
610 http_curl_getinfo(ch, info);
611 }
612
613 http_curl_cleanup(ch, clean_curl, response);
614
615 return SUCCESS;
616 }
617 /* }}} */
618
619 /*
620 * Local variables:
621 * tab-width: 4
622 * c-basic-offset: 4
623 * End:
624 * vim600: noet sw=4 ts=4 fdm=marker
625 * vim<600: noet sw=4 ts=4
626 */