@@ -135,6 +135,7 @@ zend_function_entry curl_functions[] = {
135
135
PHP_FE (curl_copy_handle , NULL )
136
136
PHP_FE (curl_version , NULL )
137
137
PHP_FE (curl_setopt , NULL )
138
+ PHP_FE (curl_setopt_array , NULL )
138
139
PHP_FE (curl_exec , NULL )
139
140
PHP_FE (curl_getinfo , NULL )
140
141
PHP_FE (curl_error , NULL )
@@ -337,6 +338,7 @@ PHP_MINIT_FUNCTION(curl)
337
338
REGISTER_CURL_CONSTANT (CURLINFO_CONTENT_TYPE );
338
339
REGISTER_CURL_CONSTANT (CURLINFO_REDIRECT_TIME );
339
340
REGISTER_CURL_CONSTANT (CURLINFO_REDIRECT_COUNT );
341
+ REGISTER_CURL_CONSTANT (CURLINFO_HEADER_OUT );
340
342
341
343
/* cURL protocol constants (curl_version) */
342
344
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
719
721
}
720
722
/* }}} */
721
723
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
+
722
741
#if CURLOPT_PASSWDFUNCTION != 0
723
742
/* {{{ curl_passwd
724
743
*/
@@ -841,6 +860,7 @@ static void alloc_curl_handle(php_curl **ch)
841
860
(* ch )-> handlers -> read = ecalloc (1 , sizeof (php_curl_read ));
842
861
843
862
(* ch )-> in_callback = 0 ;
863
+ (* ch )-> header .str_len = 0 ;
844
864
845
865
memset (& (* ch )-> err , 0 , sizeof ((* ch )-> err ));
846
866
@@ -949,24 +969,10 @@ PHP_FUNCTION(curl_copy_handle)
949
969
}
950
970
/* }}} */
951
971
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 )
955
973
{
956
- zval * * zid , * * zoption , * * zvalue ;
957
- php_curl * ch ;
958
974
CURLcode error = CURLE_OK ;
959
- int option ;
960
975
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 );
970
976
switch (option ) {
971
977
case CURLOPT_INFILESIZE :
972
978
case CURLOPT_VERBOSE :
@@ -1294,24 +1300,94 @@ PHP_FUNCTION(curl_setopt)
1294
1300
1295
1301
break ;
1296
1302
}
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 ;
1297
1315
}
1298
1316
1299
1317
SAVE_CURL_ERROR (ch , error );
1300
1318
if (error != CURLE_OK ) {
1301
- RETURN_FALSE ;
1319
+ return 1 ;
1302
1320
} 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 )) {
1303
1341
RETURN_TRUE ;
1342
+ } else {
1343
+ RETURN_FALSE ;
1304
1344
}
1305
1345
}
1306
1346
/* }}} */
1307
1347
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
+
1308
1380
/* {{{ _php_curl_cleanup_handle(ch)
1309
1381
Cleanup an execution phase */
1310
1382
void _php_curl_cleanup_handle (php_curl * ch )
1311
1383
{
1312
1384
if (ch -> handlers -> write -> buf .len > 0 ) {
1313
1385
memset (& ch -> handlers -> write -> buf , 0 , sizeof (smart_str ));
1314
1386
}
1387
+ if (ch -> header .str_len ) {
1388
+ efree (ch -> header .str );
1389
+ ch -> header .str_len = 0 ;
1390
+ }
1315
1391
1316
1392
memset (ch -> err .str , 0 , CURL_ERROR_SIZE + 1 );
1317
1393
ch -> err .no = 0 ;
@@ -1443,6 +1519,9 @@ PHP_FUNCTION(curl_getinfo)
1443
1519
if (curl_easy_getinfo (ch -> cp , CURLINFO_REDIRECT_TIME , & d_code ) == CURLE_OK ) {
1444
1520
CAAD ("redirect_time" , d_code );
1445
1521
}
1522
+ if (ch -> header .str_len > 0 ) {
1523
+ CAAS ("request_header" , ch -> header .str );
1524
+ }
1446
1525
} else {
1447
1526
option = Z_LVAL_PP (zoption );
1448
1527
switch (option ) {
@@ -1493,6 +1572,12 @@ PHP_FUNCTION(curl_getinfo)
1493
1572
}
1494
1573
break ;
1495
1574
}
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
+ }
1496
1581
}
1497
1582
}
1498
1583
}
@@ -1586,6 +1671,10 @@ static void _php_curl_close(zend_rsrc_list_entry *rsrc TSRMLS_DC)
1586
1671
if (ch -> handlers -> passwd ) {
1587
1672
zval_ptr_dtor (& ch -> handlers -> passwd );
1588
1673
}
1674
+ if (ch -> header .str_len > 0 ) {
1675
+ efree (ch -> header .str );
1676
+ }
1677
+
1589
1678
efree (ch -> handlers -> write );
1590
1679
efree (ch -> handlers -> write_header );
1591
1680
efree (ch -> handlers -> read );
0 commit comments