11use curl:: easy:: Easy ;
22use phper:: {
3- c_str_ptr, php_fn, php_function, php_minfo, php_minfo_function, php_minit, php_minit_function,
3+ c_str_ptr,
4+ main:: php:: error_doc_ref,
5+ php_fn, php_function, php_minfo, php_minfo_function, php_minit, php_minit_function,
46 php_mshutdown, php_mshutdown_function, php_rinit, php_rinit_function, php_rshutdown,
57 php_rshutdown_function,
6- sys:: {
7- php_error_docref0, php_info_print_table_end, php_info_print_table_start, E_WARNING ,
8- PHP_INI_SYSTEM ,
9- } ,
8+ sys:: { php_info_print_table_end, php_info_print_table_start, PHP_INI_SYSTEM } ,
109 zend:: {
1110 api:: { FunctionEntries , FunctionEntryBuilder } ,
1211 compile:: { create_zend_arg_info, MultiInternalArgInfo , Visibility } ,
12+ errors:: Level ,
1313 ini:: { create_ini_entry, IniEntries } ,
1414 modules:: { create_zend_module_entry, ModuleArgs , ModuleEntry } ,
1515 types:: { ClassEntry , ExecuteData , ReturnValue , SetVal , Value } ,
@@ -60,7 +60,7 @@ static ARG_INFO_VOID: MultiInternalArgInfo<0> = MultiInternalArgInfo::new(0, fal
6060static ARG_INFO_MINI_CURL_CONSTRUCT : MultiInternalArgInfo < 1 > =
6161 MultiInternalArgInfo :: new ( 0 , false , [ create_zend_arg_info ( c_str_ptr ! ( "url" ) , false ) ] ) ;
6262
63- static MINI_CURL_METHODS : FunctionEntries < 2 > = FunctionEntries :: new ( [
63+ static MINI_CURL_METHODS : FunctionEntries < 3 > = FunctionEntries :: new ( [
6464 FunctionEntryBuilder :: new (
6565 c_str_ptr ! ( "__construct" ) ,
6666 Some ( php_fn ! ( mini_curl_construct) ) ,
@@ -70,6 +70,9 @@ static MINI_CURL_METHODS: FunctionEntries<2> = FunctionEntries::new([
7070 FunctionEntryBuilder :: new ( c_str_ptr ! ( "__destruct" ) , Some ( php_fn ! ( mini_curl_destruct) ) )
7171 . arg_info ( & ARG_INFO_VOID )
7272 . build ( ) ,
73+ FunctionEntryBuilder :: new ( c_str_ptr ! ( "exec" ) , Some ( php_fn ! ( mini_curl_exec) ) )
74+ . arg_info ( & ARG_INFO_VOID )
75+ . build ( ) ,
7376] ) ;
7477
7578#[ php_function]
@@ -85,13 +88,7 @@ pub fn mini_curl_construct(execute_data: &mut ExecuteData) -> impl SetVal {
8588
8689 if !url. is_empty ( ) {
8790 if let Err ( e) = easy. url ( url) {
88- unsafe {
89- php_error_docref0 (
90- null ( ) ,
91- E_WARNING as i32 ,
92- format ! ( "curl set failed: {}\0 " , e) . as_ptr ( ) . cast ( ) ,
93- ) ;
94- }
91+ error_doc_ref ( Level :: Warning , format ! ( "curl set failed: {}\0 " , e) ) ;
9592 return ReturnValue :: Bool ( false ) ;
9693 }
9794 }
@@ -101,6 +98,35 @@ pub fn mini_curl_construct(execute_data: &mut ExecuteData) -> impl SetVal {
10198 ReturnValue :: Null
10299}
103100
101+ #[ php_function]
102+ pub fn mini_curl_exec ( execute_data : & mut ExecuteData ) -> impl SetVal {
103+ if execute_data. parse_parameters :: < ( ) > ( ) . is_none ( ) {
104+ return ReturnValue :: Bool ( false ) ;
105+ }
106+
107+ let mut data = Vec :: new ( ) ;
108+
109+ let this = execute_data. get_this ( ) ;
110+ let ptr = MINI_CURL_CE . read_property ( this, "_rust_easy_ptr" ) ;
111+ let value = ptr. try_into_value ( ) . unwrap ( ) ;
112+ let ptr = value. into_long ( ) . unwrap ( ) ;
113+
114+ let mut handle = unsafe { Box :: from_raw ( ptr as * mut Easy ) } ;
115+ let mut transfer = handle. transfer ( ) ;
116+ transfer
117+ . write_function ( |new_data| {
118+ data. extend_from_slice ( new_data) ;
119+ Ok ( new_data. len ( ) )
120+ } )
121+ . unwrap ( ) ;
122+ transfer. perform ( ) . unwrap ( ) ;
123+ drop ( transfer) ;
124+
125+ Box :: into_raw ( handle) ;
126+
127+ ReturnValue :: String ( String :: from_utf8 ( data) . unwrap ( ) )
128+ }
129+
104130#[ php_function]
105131pub fn mini_curl_destruct ( execute_data : & mut ExecuteData ) -> impl SetVal {
106132 if execute_data. parse_parameters :: < ( ) > ( ) . is_none ( ) {
0 commit comments