Skip to content

Commit 5f25d80

Browse files
author
Ilia Alshanetsky
committed
cURL extension news.
1 parent d0e1a4f commit 5f25d80

File tree

3 files changed

+116
-16
lines changed

3 files changed

+116
-16
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2006, PHP 5.1.3
4+
- Improved cURL extension: (Ilia)
5+
. Added curl_setopt_array() function that allows setting of multiple options
6+
via an associated array.
7+
. Added the ability to retrieve the request message sent to the server.
48
- Changed get_headers() to retrieve headers also from non-200 responses. (Ilia)
59
- Changed get_headers() to use the default context. (Ilia)
610
- Added a check for special characters in the session name. (Ilia)

ext/curl/interface.c

+105-16
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ zend_function_entry curl_functions[] = {
135135
PHP_FE(curl_copy_handle, NULL)
136136
PHP_FE(curl_version, NULL)
137137
PHP_FE(curl_setopt, NULL)
138+
PHP_FE(curl_setopt_array, NULL)
138139
PHP_FE(curl_exec, NULL)
139140
PHP_FE(curl_getinfo, NULL)
140141
PHP_FE(curl_error, NULL)
@@ -337,6 +338,7 @@ PHP_MINIT_FUNCTION(curl)
337338
REGISTER_CURL_CONSTANT(CURLINFO_CONTENT_TYPE);
338339
REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_TIME);
339340
REGISTER_CURL_CONSTANT(CURLINFO_REDIRECT_COUNT);
341+
REGISTER_CURL_CONSTANT(CURLINFO_HEADER_OUT);
340342

341343
/* cURL protocol constants (curl_version) */
342344
REGISTER_CURL_CONSTANT(CURL_VERSION_IPV6);
@@ -719,6 +721,23 @@ static size_t curl_write_header(char *data, size_t size, size_t nmemb, void *ctx
719721
}
720722
/* }}} */
721723

724+
static int curl_debug(CURL *cp, curl_infotype type, char *buf, size_t buf_len, void *ctx)
725+
{
726+
php_curl *ch = (php_curl *) ctx;
727+
728+
if (type == CURLINFO_HEADER_OUT) {
729+
if (ch->header.str_len) {
730+
efree(ch->header.str);
731+
}
732+
if (buf_len > 0) {
733+
ch->header.str = estrndup(buf, buf_len);
734+
ch->header.str_len = buf_len;
735+
}
736+
}
737+
738+
return 0;
739+
}
740+
722741
#if CURLOPT_PASSWDFUNCTION != 0
723742
/* {{{ curl_passwd
724743
*/
@@ -841,6 +860,7 @@ static void alloc_curl_handle(php_curl **ch)
841860
(*ch)->handlers->read = ecalloc(1, sizeof(php_curl_read));
842861

843862
(*ch)->in_callback = 0;
863+
(*ch)->header.str_len = 0;
844864

845865
memset(&(*ch)->err, 0, sizeof((*ch)->err));
846866

@@ -949,24 +969,10 @@ PHP_FUNCTION(curl_copy_handle)
949969
}
950970
/* }}} */
951971

952-
/* {{{ proto bool curl_setopt(resource ch, int option, mixed value)
953-
Set an option for a CURL transfer */
954-
PHP_FUNCTION(curl_setopt)
972+
static int _php_curl_setopt(php_curl *ch, long option, zval **zvalue, zval *return_value TSRMLS_DC)
955973
{
956-
zval **zid, **zoption, **zvalue;
957-
php_curl *ch;
958974
CURLcode error=CURLE_OK;
959-
int option;
960975

961-
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &zid, &zoption, &zvalue) == FAILURE) {
962-
WRONG_PARAM_COUNT;
963-
}
964-
965-
ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl);
966-
967-
convert_to_long_ex(zoption);
968-
969-
option = Z_LVAL_PP(zoption);
970976
switch (option) {
971977
case CURLOPT_INFILESIZE:
972978
case CURLOPT_VERBOSE:
@@ -1294,24 +1300,94 @@ PHP_FUNCTION(curl_setopt)
12941300

12951301
break;
12961302
}
1303+
case CURLINFO_HEADER_OUT:
1304+
convert_to_long_ex(zvalue);
1305+
if (Z_LVAL_PP(zvalue) == 1) {
1306+
curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, curl_debug);
1307+
curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *)ch);
1308+
curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 1);
1309+
} else {
1310+
curl_easy_setopt(ch->cp, CURLOPT_DEBUGFUNCTION, NULL);
1311+
curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, NULL);
1312+
curl_easy_setopt(ch->cp, CURLOPT_VERBOSE, 0);
1313+
}
1314+
break;
12971315
}
12981316

12991317
SAVE_CURL_ERROR(ch, error);
13001318
if (error != CURLE_OK) {
1301-
RETURN_FALSE;
1319+
return 1;
13021320
} else {
1321+
return 0;
1322+
}
1323+
}
1324+
1325+
/* {{{ proto bool curl_setopt(resource ch, int option, mixed value)
1326+
Set an option for a CURL transfer */
1327+
PHP_FUNCTION(curl_setopt)
1328+
{
1329+
zval **zid, **zoption, **zvalue;
1330+
php_curl *ch;
1331+
1332+
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &zid, &zoption, &zvalue) == FAILURE) {
1333+
WRONG_PARAM_COUNT;
1334+
}
1335+
1336+
ZEND_FETCH_RESOURCE(ch, php_curl *, zid, -1, le_curl_name, le_curl);
1337+
1338+
convert_to_long_ex(zoption);
1339+
1340+
if (!_php_curl_setopt(ch, Z_LVAL_PP(zoption), zvalue, return_value TSRMLS_CC)) {
13031341
RETURN_TRUE;
1342+
} else {
1343+
RETURN_FALSE;
13041344
}
13051345
}
13061346
/* }}} */
13071347

1348+
/* {{{ proto bool curl_setopt_array(resource ch, array options)
1349+
Set an array of option for a CURL transfer */
1350+
PHP_FUNCTION(curl_setopt_array)
1351+
{
1352+
zval *zid, *arr, **entry;
1353+
php_curl *ch;
1354+
long option;
1355+
HashPosition pos;
1356+
char *string_key;
1357+
int str_key_len;
1358+
1359+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za", &zid, &arr) == FAILURE) {
1360+
RETURN_FALSE;
1361+
}
1362+
1363+
ZEND_FETCH_RESOURCE(ch, php_curl *, &zid, -1, le_curl_name, le_curl);
1364+
1365+
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
1366+
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &pos) == SUCCESS) {
1367+
if (zend_hash_get_current_key_ex(Z_ARRVAL_P(arr), &string_key, &str_key_len, &option, 0, &pos) == HASH_KEY_IS_STRING) {
1368+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array keys must be CURLOPT constants or equivalent interger values.");
1369+
RETURN_FALSE;
1370+
}
1371+
if (_php_curl_setopt(ch, option, entry, return_value TSRMLS_CC)) {
1372+
RETURN_FALSE;
1373+
}
1374+
zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
1375+
}
1376+
RETURN_TRUE;
1377+
}
1378+
/* }}} */
1379+
13081380
/* {{{ _php_curl_cleanup_handle(ch)
13091381
Cleanup an execution phase */
13101382
void _php_curl_cleanup_handle(php_curl *ch)
13111383
{
13121384
if (ch->handlers->write->buf.len > 0) {
13131385
memset(&ch->handlers->write->buf, 0, sizeof(smart_str));
13141386
}
1387+
if (ch->header.str_len) {
1388+
efree(ch->header.str);
1389+
ch->header.str_len = 0;
1390+
}
13151391

13161392
memset(ch->err.str, 0, CURL_ERROR_SIZE + 1);
13171393
ch->err.no = 0;
@@ -1443,6 +1519,9 @@ PHP_FUNCTION(curl_getinfo)
14431519
if (curl_easy_getinfo(ch->cp, CURLINFO_REDIRECT_TIME, &d_code) == CURLE_OK) {
14441520
CAAD("redirect_time", d_code);
14451521
}
1522+
if (ch->header.str_len > 0) {
1523+
CAAS("request_header", ch->header.str);
1524+
}
14461525
} else {
14471526
option = Z_LVAL_PP(zoption);
14481527
switch (option) {
@@ -1493,6 +1572,12 @@ PHP_FUNCTION(curl_getinfo)
14931572
}
14941573
break;
14951574
}
1575+
case CURLINFO_HEADER_OUT:
1576+
if (ch->header.str_len > 0) {
1577+
RETURN_STRINGL(ch->header.str, ch->header.str_len, 1);
1578+
} else {
1579+
RETURN_FALSE;
1580+
}
14961581
}
14971582
}
14981583
}
@@ -1586,6 +1671,10 @@ static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC)
15861671
if (ch->handlers->passwd) {
15871672
zval_ptr_dtor(&ch->handlers->passwd);
15881673
}
1674+
if (ch->header.str_len > 0) {
1675+
efree(ch->header.str);
1676+
}
1677+
15891678
efree(ch->handlers->write);
15901679
efree(ch->handlers->write_header);
15911680
efree(ch->handlers->read);

ext/curl/php_curl.h

+7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ PHP_FUNCTION(curl_version);
6363
PHP_FUNCTION(curl_init);
6464
PHP_FUNCTION(curl_copy_handle);
6565
PHP_FUNCTION(curl_setopt);
66+
PHP_FUNCTION(curl_setopt_array);
6667
PHP_FUNCTION(curl_exec);
6768
PHP_FUNCTION(curl_getinfo);
6869
PHP_FUNCTION(curl_error);
@@ -107,6 +108,11 @@ struct _php_curl_error {
107108
int no;
108109
};
109110

111+
struct _php_curl_send_headers {
112+
char *str;
113+
size_t str_len;
114+
};
115+
110116
struct _php_curl_free {
111117
zend_llist str;
112118
zend_llist post;
@@ -116,6 +122,7 @@ struct _php_curl_free {
116122
typedef struct {
117123
struct _php_curl_error err;
118124
struct _php_curl_free to_free;
125+
struct _php_curl_send_headers header;
119126
void ***thread_ctx;
120127
CURL *cp;
121128
php_curl_handlers *handlers;

0 commit comments

Comments
 (0)