Android Question Simple API Request with key and certificate

hi i have this code in php that i wrote some month ago, my question is, can i write this in b4a?
the actual difficoult that i encounter is given by the file .crt and .key for the ssl. Someone can help me in some way?


My php code that i wanna convert:
function post_listaDispositivi() {
    $body = trim(file_get_contents('php://input'));
    
    $crt_path = "./Chiavi/" . $body . "/" . $body . ".crt";
    $key_path = "./Chiavi/" . $body . "/" . $body . ".key";
    
    //$crt_path = "./Chiavi/" . $body . "/" . $body . ".crt";
    //$key_path = "./Chiavi/" . $body . "/" . $body . ".key";
    
    if( !file_exists($crt_path) || !file_exists($key_path) ){
        echo "-1";
        return "-1";
    }
    
    $url = "https://apig-ivaservizi.agenziaentrate.gov.it/apig/v1/gestori/me/dispositivi/?perPage=500000";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_PORT , 443);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    curl_setopt($ch, CURLOPT_SSLCERT, $crt_path);
    curl_setopt(curl, CURLOPT_SSLCERTTYPE, "CRT");
    curl_setopt($ch, CURLOPT_SSLKEY, $key_path);
    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                
    if( strtoupper($method) == "POST" )
    {
       curl_setopt($ch, CURLOPT_POST, 1);
       curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    }

    $response = curl_exec($ch);
    $info =curl_errno($ch)>0 ? array("curl_error_".curl_errno($ch)=>curl_error($ch)) : curl_getinfo($ch);
    print_r($response);
    curl_close($ch);
    return $response;
}
 

aeric

Expert
Licensed User
Longtime User
If I understand correctly, there are 2 parts.
1. You need to receive request (http POST) from a client. If you want this run on Android device, you need to check for httpserver using B4A.
2. It seems the app need to make a http request to another server. On this part, it could be done using OkHttpUtils2.
 
Upvote 0
Maybe you don't need a http server on Android if you can specify the value of the body i.e the name of the certificate which is just a string in B4A.
okay so, Actually the php code that i post stand on a site and with my b4a app i make an api request to my siteand the php run the other api request.

so i have
App -> Site -> Destination

but i wanna only use
App -> Destination

Actually in the code

Code B4A that run the PHP that i posted:
Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString("https://www.sisoftsrl.it/wp-admin/admin-post.php?action=listaDispositivi", Comodo.UserCode)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        response = j.GetString
        valorizzaUid(idDispositivo)

i use this "solution" with site just because in b4a i dont know how to pass .crt and .key file for openssl to give for api call
because the Destination wanna this certificate for give me the access to data
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
pass your certs (in pem format) as headers
 
Upvote 0
Top