Home > Software engineering >  Send data with libcurl to .php in c
Send data with libcurl to .php in c

Time:02-18

I use libcurl to send request to my site for fetching some info.

But I don't know how to get data from user (c ) and send that to .php file.

It's my c source which get name from user :

std::string userUname;
        std::cout << "[?] Enter Your Username: ";
        std::cin >> userUname;
        CURL* curl;
        CURLcode res;
        std::string readBuffer;
        curl = curl_easy_init();
        if (curl) {
            const char* testlevi = "http://example.com/a.php";
             curl_easy_setopt(curl, CURLOPT_URL, testlevi);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
            res = curl_easy_perform(curl);
            curl_easy_cleanup(curl);
            std::cout << readBuffer << std::endl;
        }

And thats my php file which gets username , and will echo:

<?php
$name = $_GET['name'];
if($name == "Jason"){
echo "Welcome Jason.";
}else{
echo "Username is wrong.";
}
?>

I just don't know how to send userUname to .php file($_GET['name'])

CodePudding user response:

You have to send the userUname in the body : curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=" userUname );

And in PHP change $_GET by $_POST OR $_REQUEST

  • Related