Skip to content

Commit 03b143a

Browse files
committed
Add CurlHandle::exec() CurlHandle::setOpt() and CurlHandle::getInfo()
1 parent 65472c7 commit 03b143a

10 files changed

+294
-56
lines changed

ext/curl/curl.stub.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,39 @@
88
*/
99
final class CurlHandle
1010
{
11+
public function __construct(?string $url = null) {}
12+
13+
/** @alias curl_setopt */
14+
public function setOpt(int $option, mixed $value): bool {}
15+
16+
/** @alias curl_getinfo */
17+
public function getInfo(?int $option = null): mixed {}
18+
19+
/** @alias curl_exec */
20+
public function exec(): string|bool {}
21+
22+
/** @alias curl_escape */
23+
public function escape(string $string): string|false {}
24+
25+
/** @alias curl_unescape */
26+
public function unescape(string $string): string|false {}
27+
28+
/** @alias curl_pause */
29+
public function pause(int $flags): int {}
30+
31+
/** @alias curl_errno */
32+
public function getErrno(): int {}
33+
34+
/** @alias curl_reset */
35+
public function reset(): void;
36+
37+
/** @alias curl_setopt_array */
38+
public function setOptArray(array $options): bool {}
39+
40+
#if LIBCURL_VERSION_NUM >= 0x073E00 /* Available since 7.62.0 */
41+
/** @alias curl_upkeep */
42+
public function upkeep(): bool {}
43+
#endif
1144
}
1245

1346
/**

ext/curl/curl_arginfo.h

Lines changed: 56 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/curl/interface.c

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ static zend_object_handlers curl_object_handlers;
223223
static zend_object *curl_create_object(zend_class_entry *class_type);
224224
static void curl_free_obj(zend_object *object);
225225
static HashTable *curl_get_gc(zend_object *object, zval **table, int *n);
226-
static zend_function *curl_get_constructor(zend_object *object);
227226
static zend_object *curl_clone_obj(zend_object *object);
228227
php_curl *init_curl_handle_into_zval(zval *curl);
229228
static inline zend_result build_mime_structure_from_hash(php_curl *ch, zval *zpostfields);
@@ -1334,7 +1333,6 @@ PHP_MINIT_FUNCTION(curl)
13341333
curl_object_handlers.offset = XtOffsetOf(php_curl, std);
13351334
curl_object_handlers.free_obj = curl_free_obj;
13361335
curl_object_handlers.get_gc = curl_get_gc;
1337-
curl_object_handlers.get_constructor = curl_get_constructor;
13381336
curl_object_handlers.clone_obj = curl_clone_obj;
13391337
curl_object_handlers.cast_object = curl_cast_object;
13401338
curl_object_handlers.compare = zend_objects_not_comparable;
@@ -1362,11 +1360,6 @@ static zend_object *curl_create_object(zend_class_entry *class_type) {
13621360
return &intern->std;
13631361
}
13641362

1365-
static zend_function *curl_get_constructor(zend_object *object) {
1366-
zend_throw_error(NULL, "Cannot directly construct CurlHandle, use curl_init() instead");
1367-
return NULL;
1368-
}
1369-
13701363
static zend_object *curl_clone_obj(zend_object *object) {
13711364
php_curl *ch;
13721365
CURL *cp;
@@ -2036,8 +2029,7 @@ static void _php_curl_set_default_options(php_curl *ch)
20362029
}
20372030
/* }}} */
20382031

2039-
/* {{{ Initialize a cURL session */
2040-
PHP_FUNCTION(curl_init)
2032+
static void _php_curl_init(INTERNAL_FUNCTION_PARAMETERS)
20412033
{
20422034
php_curl *ch;
20432035
CURL *cp;
@@ -2048,14 +2040,16 @@ PHP_FUNCTION(curl_init)
20482040
Z_PARAM_STR_OR_NULL(url)
20492041
ZEND_PARSE_PARAMETERS_END();
20502042

2043+
ch = Z_CURL_P(return_value);
2044+
init_curl_handle(ch);
2045+
20512046
cp = curl_easy_init();
20522047
if (!cp) {
20532048
php_error_docref(NULL, E_WARNING, "Could not initialize a new cURL handle");
2049+
zval_ptr_dtor(return_value);
20542050
RETURN_FALSE;
20552051
}
20562052

2057-
ch = init_curl_handle_into_zval(return_value);
2058-
20592053
ch->cp = cp;
20602054

20612055
ch->handlers.write->method = PHP_CURL_STDOUT;
@@ -2071,6 +2065,19 @@ PHP_FUNCTION(curl_init)
20712065
}
20722066
}
20732067
}
2068+
2069+
/* {{{ Initialize a cURL session */
2070+
PHP_METHOD(CurlHandle, __construct)
2071+
{
2072+
return_value = ZEND_THIS;
2073+
_php_curl_init(INTERNAL_FUNCTION_PARAM_PASSTHRU);
2074+
}
2075+
2076+
PHP_FUNCTION(curl_init)
2077+
{
2078+
object_init_ex(return_value, curl_ce);
2079+
_php_curl_init(INTERNAL_FUNCTION_PARAM_PASSTHRU);
2080+
}
20742081
/* }}} */
20752082

20762083
void _php_setup_easy_copy_handlers(php_curl *ch, php_curl *source)
@@ -3291,11 +3298,9 @@ PHP_FUNCTION(curl_setopt)
32913298
zend_long options;
32923299
php_curl *ch;
32933300

3294-
ZEND_PARSE_PARAMETERS_START(3, 3)
3295-
Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3296-
Z_PARAM_LONG(options)
3297-
Z_PARAM_ZVAL(zvalue)
3298-
ZEND_PARSE_PARAMETERS_END();
3301+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Olz", &zid, curl_ce, &options, &zvalue) == FAILURE) {
3302+
RETURN_THROWS();
3303+
}
32993304

33003305
ch = Z_CURL_P(zid);
33013306

@@ -3315,10 +3320,9 @@ PHP_FUNCTION(curl_setopt_array)
33153320
zend_ulong option;
33163321
zend_string *string_key;
33173322

3318-
ZEND_PARSE_PARAMETERS_START(2, 2)
3319-
Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3320-
Z_PARAM_ARRAY(arr)
3321-
ZEND_PARSE_PARAMETERS_END();
3323+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oa", &zid, curl_ce, &arr) == FAILURE) {
3324+
RETURN_THROWS();
3325+
}
33223326

33233327
ch = Z_CURL_P(zid);
33243328

@@ -3360,9 +3364,9 @@ PHP_FUNCTION(curl_exec)
33603364
zval *zid;
33613365
php_curl *ch;
33623366

3363-
ZEND_PARSE_PARAMETERS_START(1, 1)
3364-
Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3365-
ZEND_PARSE_PARAMETERS_END();
3367+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zid, curl_ce) == FAILURE) {
3368+
RETURN_THROWS();
3369+
}
33663370

33673371
ch = Z_CURL_P(zid);
33683372

@@ -3415,11 +3419,9 @@ PHP_FUNCTION(curl_getinfo)
34153419
zend_long option;
34163420
bool option_is_null = 1;
34173421

3418-
ZEND_PARSE_PARAMETERS_START(1, 2)
3419-
Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3420-
Z_PARAM_OPTIONAL
3421-
Z_PARAM_LONG_OR_NULL(option, option_is_null)
3422-
ZEND_PARSE_PARAMETERS_END();
3422+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l!", &zid, curl_ce, &option, &option_is_null) == FAILURE) {
3423+
RETURN_THROWS();
3424+
}
34233425

34243426
ch = Z_CURL_P(zid);
34253427

@@ -3699,9 +3701,9 @@ PHP_FUNCTION(curl_errno)
36993701
zval *zid;
37003702
php_curl *ch;
37013703

3702-
ZEND_PARSE_PARAMETERS_START(1,1)
3703-
Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3704-
ZEND_PARSE_PARAMETERS_END();
3704+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zid, curl_ce) == FAILURE) {
3705+
RETURN_THROWS();
3706+
}
37053707

37063708
ch = Z_CURL_P(zid);
37073709

@@ -3898,9 +3900,9 @@ PHP_FUNCTION(curl_reset)
38983900
zval *zid;
38993901
php_curl *ch;
39003902

3901-
ZEND_PARSE_PARAMETERS_START(1, 1)
3902-
Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3903-
ZEND_PARSE_PARAMETERS_END();
3903+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zid, curl_ce) == FAILURE) {
3904+
RETURN_THROWS();
3905+
}
39043906

39053907
ch = Z_CURL_P(zid);
39063908

@@ -3923,10 +3925,9 @@ PHP_FUNCTION(curl_escape)
39233925
zval *zid;
39243926
php_curl *ch;
39253927

3926-
ZEND_PARSE_PARAMETERS_START(2,2)
3927-
Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3928-
Z_PARAM_STR(str)
3929-
ZEND_PARSE_PARAMETERS_END();
3928+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OS", &zid, curl_ce, &str) == FAILURE) {
3929+
RETURN_THROWS();
3930+
}
39303931

39313932
ch = Z_CURL_P(zid);
39323933

@@ -3952,10 +3953,9 @@ PHP_FUNCTION(curl_unescape)
39523953
zend_string *str;
39533954
php_curl *ch;
39543955

3955-
ZEND_PARSE_PARAMETERS_START(2,2)
3956-
Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3957-
Z_PARAM_STR(str)
3958-
ZEND_PARSE_PARAMETERS_END();
3956+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OS", &zid, curl_ce, &str) == FAILURE) {
3957+
RETURN_THROWS();
3958+
}
39593959

39603960
ch = Z_CURL_P(zid);
39613961

@@ -3979,10 +3979,9 @@ PHP_FUNCTION(curl_pause)
39793979
zval *zid;
39803980
php_curl *ch;
39813981

3982-
ZEND_PARSE_PARAMETERS_START(2,2)
3983-
Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
3984-
Z_PARAM_LONG(bitmask)
3985-
ZEND_PARSE_PARAMETERS_END();
3982+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zid, curl_ce, &bitmask) == FAILURE) {
3983+
RETURN_THROWS();
3984+
}
39863985

39873986
ch = Z_CURL_P(zid);
39883987

@@ -3998,9 +3997,9 @@ PHP_FUNCTION(curl_upkeep)
39983997
zval *zid;
39993998
php_curl *ch;
40003999

4001-
ZEND_PARSE_PARAMETERS_START(1, 1)
4002-
Z_PARAM_OBJECT_OF_CLASS(zid, curl_ce)
4003-
ZEND_PARSE_PARAMETERS_END();
4000+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zid, curl_ce) == FAILURE) {
4001+
RETURN_THROWS();
4002+
}
40044003

40054004
ch = Z_CURL_P(zid);
40064005

ext/curl/tests/bug80121.phpt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ curl
55
--FILE--
66
<?php
77

8-
try {
9-
new CurlHandle;
10-
} catch (Error $e) {
11-
echo $e->getMessage(), "\n";
12-
}
138
try {
149
new CurlMultiHandle;
1510
} catch (Error $e) {
@@ -23,6 +18,5 @@ try {
2318

2419
?>
2520
--EXPECT--
26-
Cannot directly construct CurlHandle, use curl_init() instead
2721
Cannot directly construct CurlMultiHandle, use curl_multi_init() instead
2822
Cannot directly construct CurlShareHandle, use curl_share_init() instead

ext/curl/tests/curl_object_007.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test curl_error() & curl_errno() function without url
3+
--EXTENSIONS--
4+
curl
5+
--FILE--
6+
<?php
7+
8+
$ch = new CurlHandle();
9+
var_dump($ch->getErrno());
10+
$ch->exec();
11+
var_dump($ch->getErrno());
12+
unset($ch);
13+
14+
15+
?>
16+
--EXPECTF--
17+
int(0)
18+
int(3)

ext/curl/tests/curl_object_020.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Test curl_getinfo() function with CURLINFO_HTTP_CODE parameter
3+
--CREDITS--
4+
Jean-Marc Fontaine <jmf@durcommefaire.net>
5+
--EXTENSIONS--
6+
curl
7+
--FILE--
8+
<?php
9+
include 'server.inc';
10+
$host = curl_cli_server_start();
11+
12+
$url = "{$host}/get.inc?test=";
13+
$ch = new CurlHandle($url);
14+
$ch->setOpt(CURLOPT_URL, $url);
15+
$ch->exec();
16+
var_dump($ch->getInfo(CURLINFO_HTTP_CODE));
17+
?>
18+
--EXPECT--
19+
Hello World!
20+
Hello World!int(200)

0 commit comments

Comments
 (0)