* reset the curl handle if curl_easy_reset() is available
[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 # define _WINSOCKAPI_
24 # define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
25 # include <winsock2.h>
26 # include <sys/types.h>
27 #endif
28
29 #include <curl/curl.h>
30 #include <curl/easy.h>
31
32 #include "php.h"
33 #include "php_http.h"
34 #include "php_http_api.h"
35 #include "php_http_curl_api.h"
36 #include "php_http_std_defs.h"
37
38 #ifdef ZEND_ENGINE_2
39 # include "ext/standard/php_http.h"
40 #endif
41
42 #include "ext/standard/php_smart_str.h"
43
44 ZEND_DECLARE_MODULE_GLOBALS(http)
45
46 #if LIBCURL_VERSION_NUM >= 0x070c01
47 # define http_curl_reset(ch) curl_easy_reset(ch)
48 #else
49 # define http_curl_reset(ch)
50 #endif
51
52 #define http_curl_startup(ch, clean_curl, URL, options) \
53 if (!ch) { \
54 if (!(ch = curl_easy_init())) { \
55 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl"); \
56 return FAILURE; \
57 } \
58 clean_curl = 1; \
59 } else { \
60 http_curl_reset(ch); \
61 } \
62 http_curl_initbuf(); \
63 http_curl_setopts(ch, URL, options);
64
65
66 #define http_curl_cleanup(ch, clean_curl) \
67 http_curl_freestr(); \
68 http_curl_freebuf(); \
69 if (clean_curl) { \
70 curl_easy_cleanup(ch); \
71 ch = NULL; \
72 }
73
74 #define http_curl_freestr() \
75 zend_llist_clean(&HTTP_G(to_free))
76
77 #define http_curl_initbuf() http_curl_initbuf_ex(0)
78
79 #define http_curl_initbuf_ex(chunk_size) \
80 { \
81 size_t size = (chunk_size > 0) ? chunk_size : HTTP_CURLBUF_SIZE; \
82 http_curl_freebuf(); \
83 HTTP_G(curlbuf).data = emalloc(size); \
84 HTTP_G(curlbuf).free = size; \
85 HTTP_G(curlbuf).size = size; \
86 }
87
88 #define http_curl_freebuf() \
89 if (HTTP_G(curlbuf).data) { \
90 efree(HTTP_G(curlbuf).data); \
91 HTTP_G(curlbuf).data = NULL; \
92 } \
93 HTTP_G(curlbuf).used = 0; \
94 HTTP_G(curlbuf).free = 0; \
95 HTTP_G(curlbuf).size = 0;
96
97 #define http_curl_copybuf(data, size) \
98 * size = HTTP_G(curlbuf).used; \
99 * data = ecalloc(1, HTTP_G(curlbuf).used + 1); \
100 memcpy(* data, HTTP_G(curlbuf).data, * size);
101
102 #define http_curl_sizebuf(for_size) \
103 { \
104 size_t size = (for_size); \
105 if (size > HTTP_G(curlbuf).free) { \
106 size_t bsize = HTTP_G(curlbuf).size; \
107 while (size > bsize) { \
108 bsize *= 2; \
109 } \
110 HTTP_G(curlbuf).data = erealloc(HTTP_G(curlbuf).data, HTTP_G(curlbuf).used + bsize); \
111 HTTP_G(curlbuf).free += bsize; \
112 } \
113 }
114
115
116 #define http_curl_copystr(s) _http_curl_copystr((s) TSRMLS_CC)
117 static inline char *_http_curl_copystr(const char *str TSRMLS_DC);
118
119 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
120 static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC);
121
122 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
123 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
124 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
125 static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...);
126
127 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
128 static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *);
129
130 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
131 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC);
132
133 /* {{{ static inline char *http_curl_copystr(char *) */
134 static inline char *_http_curl_copystr(const char *str TSRMLS_DC)
135 {
136 char *new_str = estrdup(str);
137 zend_llist_add_element(&HTTP_G(to_free), &new_str);
138 return new_str;
139 }
140 /* }}} */
141
142 /* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
143 static size_t http_curl_body_callback(char *buf, size_t len, size_t n, void *s)
144 {
145 TSRMLS_FETCH();
146
147 http_curl_sizebuf(len *= n);
148
149 memcpy(HTTP_G(curlbuf).data + HTTP_G(curlbuf).used, buf, len);
150 HTTP_G(curlbuf).free -= len;
151 HTTP_G(curlbuf).used += len;
152 return len;
153 }
154 /* }}} */
155
156 /* {{{ static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *) */
157 static size_t http_curl_hdrs_callback(char *buf, size_t len, size_t n, void *s)
158 {
159 TSRMLS_FETCH();
160
161 /* discard previous headers */
162 if ((HTTP_G(curlbuf).used) && (!strncmp(buf, "HTTP/1.", sizeof("HTTP/1.") - 1))) {
163 http_curl_initbuf();
164 }
165 http_curl_sizebuf(len *= n);
166
167 memcpy(HTTP_G(curlbuf).data + HTTP_G(curlbuf).used, buf, len);
168 HTTP_G(curlbuf).free -= len;
169 HTTP_G(curlbuf).used += len;
170 return len;
171 }
172 /* }}} */
173
174 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
175 static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...)
176 {
177 zval **zoption;
178 va_list types;
179 int i;
180
181 if (SUCCESS != zend_hash_find(options, key, strlen(key) + 1, (void **) &zoption)) {
182 return NULL;
183 }
184 if (checks < 1) {
185 return *zoption;
186 }
187
188 va_start(types, checks);
189 for (i = 0; i < checks; ++i) {
190 if ((va_arg(types, int)) == (Z_TYPE_PP(zoption))) {
191 va_end(types);
192 return *zoption;
193 }
194 }
195 va_end(types);
196 return NULL;
197 }
198 /* }}} */
199
200 /* {{{ static inline void http_curl_setopts(CURL *, char *, HashTable *) */
201 static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC)
202 {
203 zval *zoption;
204 zend_bool range_req = 0;
205
206 /* standard options */
207 curl_easy_setopt(ch, CURLOPT_URL, url);
208 curl_easy_setopt(ch, CURLOPT_HEADER, 0);
209 curl_easy_setopt(ch, CURLOPT_FILETIME, 1);
210 curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1);
211 curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1);
212 curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, http_curl_body_callback);
213 curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, http_curl_hdrs_callback);
214 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
215 curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1);
216 #endif
217
218 if ((!options) || (1 > zend_hash_num_elements(options))) {
219 return;
220 }
221
222 /* proxy */
223 if (zoption = http_curl_getopt1(options, "proxyhost", IS_STRING)) {
224 curl_easy_setopt(ch, CURLOPT_PROXY, http_curl_copystr(Z_STRVAL_P(zoption)));
225 /* port */
226 if (zoption = http_curl_getopt1(options, "proxyport", IS_LONG)) {
227 curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
228 }
229 /* user:pass */
230 if (zoption = http_curl_getopt1(options, "proxyauth", IS_STRING)) {
231 curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
232 }
233 #if LIBCURL_VERSION_NUM >= 0x070a07
234 /* auth method */
235 if (zoption = http_curl_getopt1(options, "proxyauthtype", IS_LONG)) {
236 curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
237 }
238 #endif
239 }
240
241 /* outgoing interface */
242 if (zoption = http_curl_getopt1(options, "interface", IS_STRING)) {
243 curl_easy_setopt(ch, CURLOPT_INTERFACE, http_curl_copystr(Z_STRVAL_P(zoption)));
244 }
245
246 /* another port */
247 if (zoption = http_curl_getopt1(options, "port", IS_LONG)) {
248 curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption));
249 }
250
251 /* auth */
252 if (zoption = http_curl_getopt1(options, "httpauth", IS_STRING)) {
253 curl_easy_setopt(ch, CURLOPT_USERPWD, http_curl_copystr(Z_STRVAL_P(zoption)));
254 }
255 #if LIBCURL_VERSION_NUM >= 0x070a06
256 if (zoption = http_curl_getopt1(options, "httpauthtype", IS_LONG)) {
257 curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
258 }
259 #endif
260
261 /* compress, empty string enables deflate and gzip */
262 if (zoption = http_curl_getopt2(options, "compress", IS_LONG, IS_BOOL)) {
263 if (Z_LVAL_P(zoption)) {
264 curl_easy_setopt(ch, CURLOPT_ENCODING, "");
265 }
266 }
267
268 /* redirects, defaults to 0 */
269 if (zoption = http_curl_getopt1(options, "redirect", IS_LONG)) {
270 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
271 curl_easy_setopt(ch, CURLOPT_MAXREDIRS, Z_LVAL_P(zoption));
272 if (zoption = http_curl_getopt2(options, "unrestrictedauth", IS_LONG, IS_BOOL)) {
273 curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
274 }
275 } else {
276 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0);
277 }
278
279 /* referer */
280 if (zoption = http_curl_getopt1(options, "referer", IS_STRING)) {
281 curl_easy_setopt(ch, CURLOPT_REFERER, http_curl_copystr(Z_STRVAL_P(zoption)));
282 }
283
284 /* useragent, default "PECL::HTTP/version (PHP/version)" */
285 if (zoption = http_curl_getopt1(options, "useragent", IS_STRING)) {
286 curl_easy_setopt(ch, CURLOPT_USERAGENT, http_curl_copystr(Z_STRVAL_P(zoption)));
287 } else {
288 curl_easy_setopt(ch, CURLOPT_USERAGENT,
289 "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
290 }
291
292 /* additional headers, array('name' => 'value') */
293 if (zoption = http_curl_getopt1(options, "headers", IS_ARRAY)) {
294 char *header_key;
295 long header_idx;
296 struct curl_slist *headers = NULL;
297
298 FOREACH_KEY(zoption, header_key, header_idx) {
299 if (header_key) {
300 zval **header_val;
301 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val)) {
302 char header[1024] = {0};
303 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
304 headers = curl_slist_append(headers, http_curl_copystr(header));
305 }
306
307 /* reset */
308 header_key = NULL;
309 }
310 }
311
312 if (headers) {
313 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
314 }
315 }
316
317 /* cookies, array('name' => 'value') */
318 if (zoption = http_curl_getopt1(options, "cookies", IS_ARRAY)) {
319 char *cookie_key = NULL;
320 long cookie_idx = 0;
321 smart_str qstr = {0};
322
323 FOREACH_KEY(zoption, cookie_key, cookie_idx) {
324 if (cookie_key) {
325 zval **cookie_val;
326 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val)) {
327 smart_str_appends(&qstr, cookie_key);
328 smart_str_appendl(&qstr, "=", 1);
329 smart_str_appendl(&qstr, Z_STRVAL_PP(cookie_val), Z_STRLEN_PP(cookie_val));
330 smart_str_appendl(&qstr, "; ", 2);
331 }
332
333 /* reset */
334 cookie_key = NULL;
335 }
336 }
337 smart_str_0(&qstr);
338
339 if (qstr.c) {
340 curl_easy_setopt(ch, CURLOPT_COOKIE, http_curl_copystr(qstr.c));
341 efree(qstr.c);
342 }
343 }
344
345 /* cookiestore */
346 if (zoption = http_curl_getopt1(options, "cookiestore", IS_STRING)) {
347 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, http_curl_copystr(Z_STRVAL_P(zoption)));
348 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, http_curl_copystr(Z_STRVAL_P(zoption)));
349 }
350
351 /* resume */
352 if (zoption = http_curl_getopt1(options, "resume", IS_LONG)) {
353 range_req = 1;
354 curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(zoption));
355 }
356
357 /* maxfilesize */
358 if (zoption = http_curl_getopt1(options, "maxfilesize", IS_LONG)) {
359 curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption));
360 }
361
362 /* lastmodified */
363 if (zoption = http_curl_getopt1(options, "lastmodified", IS_LONG)) {
364 curl_easy_setopt(ch, CURLOPT_TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
365 curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(zoption));
366 }
367
368 /* timeout */
369 if (zoption = http_curl_getopt1(options, "timeout", IS_LONG)) {
370 curl_easy_setopt(ch, CURLOPT_TIMEOUT, Z_LVAL_P(zoption));
371 }
372
373 /* connecttimeout */
374 if (zoption = http_curl_getopt1(options, "connecttimeout", IS_LONG)) {
375 curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT, Z_LVAL_P(zoption));
376 }
377 }
378 /* }}} */
379
380 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
381 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC)
382 {
383 zval array;
384 Z_ARRVAL(array) = info;
385
386 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
387 #define HTTP_CURL_INFO_EX(I, X) \
388 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
389 { \
390 case CURLINFO_STRING: \
391 { \
392 char *c; \
393 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
394 add_assoc_string(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
395 } \
396 } \
397 break; \
398 \
399 case CURLINFO_DOUBLE: \
400 { \
401 double d; \
402 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
403 add_assoc_double(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), d); \
404 } \
405 } \
406 break; \
407 \
408 case CURLINFO_LONG: \
409 { \
410 long l; \
411 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
412 add_assoc_long(&array, pretty_key(http_curl_copystr(#X), sizeof(#X)-1, 0, 0), l); \
413 } \
414 } \
415 break; \
416 }
417
418 HTTP_CURL_INFO(EFFECTIVE_URL);
419
420 #if LIBCURL_VERSION_NUM >= 0x070a07
421 HTTP_CURL_INFO(RESPONSE_CODE);
422 #else
423 HTTP_CURL_INFO_EX(HTTP_CODE, RESPONSE_CODE);
424 #endif
425 HTTP_CURL_INFO(HTTP_CONNECTCODE);
426
427 #if LIBCURL_VERSION_NUM >= 0x070500
428 HTTP_CURL_INFO(FILETIME);
429 #endif
430 HTTP_CURL_INFO(TOTAL_TIME);
431 HTTP_CURL_INFO(NAMELOOKUP_TIME);
432 HTTP_CURL_INFO(CONNECT_TIME);
433 HTTP_CURL_INFO(PRETRANSFER_TIME);
434 HTTP_CURL_INFO(STARTTRANSFER_TIME);
435 #if LIBCURL_VERSION_NUM >= 0x070907
436 HTTP_CURL_INFO(REDIRECT_TIME);
437 HTTP_CURL_INFO(REDIRECT_COUNT);
438 #endif
439
440 HTTP_CURL_INFO(SIZE_UPLOAD);
441 HTTP_CURL_INFO(SIZE_DOWNLOAD);
442 HTTP_CURL_INFO(SPEED_DOWNLOAD);
443 HTTP_CURL_INFO(SPEED_UPLOAD);
444
445 HTTP_CURL_INFO(HEADER_SIZE);
446 HTTP_CURL_INFO(REQUEST_SIZE);
447
448 HTTP_CURL_INFO(SSL_VERIFYRESULT);
449 #if LIBCURL_VERSION_NUM >= 0x070c03
450 /*HTTP_CURL_INFO(SSL_ENGINES);
451 todo: CURLINFO_SLIST */
452 #endif
453
454 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
455 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
456 HTTP_CURL_INFO(CONTENT_TYPE);
457
458 #if LIBCURL_VERSION_NUM >= 0x070a03
459 /*HTTP_CURL_INFO(PRIVATE);*/
460 #endif
461
462 #if LIBCURL_VERSION_NUM >= 0x070a08
463 HTTP_CURL_INFO(HTTPAUTH_AVAIL);
464 HTTP_CURL_INFO(PROXYAUTH_AVAIL);
465 #endif
466
467 #if LIBCURL_VERSION_NUM >= 0x070c02
468 /*HTTP_CURL_INFO(OS_ERRNO);*/
469 #endif
470
471 #if LIBCURL_VERSION_NUM >= 0x070c03
472 HTTP_CURL_INFO(NUM_CONNECTS);
473 #endif
474 }
475 /* }}} */
476
477 /* {{{ STATUS http_get_ex(CURL *, char *, HashTable *, HashTable *, char **, size_t *) */
478 PHP_HTTP_API STATUS _http_get_ex(CURL *ch, const char *URL, HashTable *options,
479 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
480 {
481 zend_bool clean_curl = 0;
482
483 http_curl_startup(ch, clean_curl, URL, options);
484 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1);
485
486 if (CURLE_OK != curl_easy_perform(ch)) {
487 http_curl_cleanup(ch, clean_curl);
488 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
489 return FAILURE;
490 }
491
492 if (info) {
493 http_curl_getinfo(ch, info);
494 }
495
496 http_curl_copybuf(data, data_len);
497 http_curl_cleanup(ch, clean_curl);
498
499 return SUCCESS;
500 }
501
502 /* {{{ STATUS http_head_ex(CURL *, char *, HashTable *, HashTable *, char **data, size_t *) */
503 PHP_HTTP_API STATUS _http_head_ex(CURL *ch, const char *URL, HashTable *options,
504 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
505 {
506 zend_bool clean_curl = 0;
507
508 http_curl_startup(ch, clean_curl, URL, options);
509 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
510
511 if (CURLE_OK != curl_easy_perform(ch)) {
512 http_curl_cleanup(ch, clean_curl);
513 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
514 return FAILURE;
515 }
516
517 if (info) {
518 http_curl_getinfo(ch, info);
519 }
520
521 http_curl_copybuf(data, data_len);
522 http_curl_cleanup(ch, clean_curl);
523
524 return SUCCESS;
525 }
526
527 /* {{{ STATUS http_post_data_ex(CURL *, char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
528 PHP_HTTP_API STATUS _http_post_data_ex(CURL *ch, const char *URL, char *postdata,
529 size_t postdata_len, HashTable *options, HashTable *info, char **data,
530 size_t *data_len TSRMLS_DC)
531 {
532 zend_bool clean_curl = 0;
533
534 http_curl_startup(ch, clean_curl, URL, options);
535 curl_easy_setopt(ch, CURLOPT_POST, 1);
536 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, postdata);
537 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, postdata_len);
538
539 if (CURLE_OK != curl_easy_perform(ch)) {
540 http_curl_cleanup(ch, clean_curl);
541 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
542 return FAILURE;
543 }
544
545 if (info) {
546 http_curl_getinfo(ch, info);
547 }
548
549 http_curl_copybuf(data, data_len);
550 http_curl_cleanup(ch, clean_curl);
551
552 return SUCCESS;
553 }
554 /* }}} */
555
556 /* {{{ STATUS http_post_array_ex(CURL *, char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
557 PHP_HTTP_API STATUS _http_post_array_ex(CURL *ch, const char *URL, HashTable *postarray,
558 HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC)
559 {
560 smart_str qstr = {0};
561 STATUS status;
562
563 HTTP_URL_ARGSEP_OVERRIDE;
564 if (php_url_encode_hash_ex(postarray, &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
565 if (qstr.c) {
566 efree(qstr.c);
567 }
568 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode post data");
569 HTTP_URL_ARGSEP_RESTORE;
570 return FAILURE;
571 }
572 smart_str_0(&qstr);
573 HTTP_URL_ARGSEP_RESTORE;
574
575 status = http_post_data_ex(ch, URL, qstr.c, qstr.len, options, info, data, data_len);
576
577 if (qstr.c) {
578 efree(qstr.c);
579 }
580 return status;
581 }
582 /* }}} */
583
584 /* {{{ STATUS http_post_curldata_ex(CURL *, char *, curl_httppost *, HashTable *, HashTable *, char **, size_t *) */
585 PHP_HTTP_API STATUS _http_post_curldata_ex(CURL *ch, const char *URL,
586 struct curl_httppost *curldata, HashTable *options, HashTable *info,
587 char **data, size_t *data_len TSRMLS_DC)
588 {
589 zend_bool clean_curl = 0;
590
591 http_curl_startup(ch, clean_curl, URL, options);
592 curl_easy_setopt(ch, CURLOPT_POST, 1);
593 curl_easy_setopt(ch, CURLOPT_HTTPPOST, curldata);
594
595 if (CURLE_OK != curl_easy_perform(ch)) {
596 http_curl_cleanup(ch, clean_curl);
597 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
598 return FAILURE;
599 }
600
601 if (info) {
602 http_curl_getinfo(ch, info);
603 }
604
605 http_curl_copybuf(data, data_len);
606 http_curl_cleanup(ch, clean_curl);
607
608 return SUCCESS;
609 }
610 /* }}} */
611
612 /*
613 * Local variables:
614 * tab-width: 4
615 * c-basic-offset: 4
616 * End:
617 * vim600: noet sw=4 ts=4 fdm=marker
618 * vim<600: noet sw=4 ts=4
619 */
620