Hack Forums
[C] C++ Client to PHP Server /// TUTORIAL & SOURCE CODE - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] C++ Client to PHP Server /// TUTORIAL & SOURCE CODE (/showthread.php?tid=6265373)



C++ Client to PHP Server /// TUTORIAL & SOURCE CODE - Hackers - Mar 6, 2024

In this very short guide you will learn how to create a POST request from a C++ program to your website, and how to store that data to a .txt file on a server with PHP.

For the client side we will use WinHTTP which can handle both http and https requests to websites. This is a much more native solution than something like curl which would require openssl or similar alongside it to do an https:// web request.

Our client program is less than 20 lines of code, and all it does is just send 2 string values (something, with value of 'goes', and here with value of 'booya'). We can modify that to send string versions of any data format, or other information we need to save to the server.

C++ Client
Code
#include <windows.h>
#include <winhttp.h>
#include <iostream>
#pragma comment(lib, "winhttp.lib")
int main() {
    HINTERNET hSession = WinHttpOpen(L"Send Post Request/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
    HINTERNET hConnect = WinHttpConnect(hSession, L"your-website.com",INTERNET_DEFAULT_HTTPS_PORT, 0);
    HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"POST", L"/post/receive.php", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
    std::wstring headers = L"Content-Type: application/x-www-form-urlencoded";
    std::string postData = "something=goes&here=booya";
    BOOL bResults = WinHttpSendRequest(hRequest, headers.c_str(), headers.length(), (LPVOID)postData.c_str(), postData.length(), postData.length(), 0);
    if (bResults) bResults = WinHttpReceiveResponse(hRequest, NULL);
    if (hRequest) WinHttpCloseHandle(hRequest);
    if (hConnect) WinHttpCloseHandle(hConnect);
    if (hSession) WinHttpCloseHandle(hSession);
    return 0;
}

On the server side we make use of a tmp folder that isn't accessible to the public to store our data. This is a very basic script that will save a .txt file any time there is a new request. It is set up to save GET or POST requests, but our C++ client only sends POST. It also stores something called 'REMOTE_ADDR' which is the IP address of the requesting user (the device the C++ client is on).

PHP Server (your-website.com/post/receive.php)
PHP Code

<?php
$tempFolderPath 
= __DIR__ . '/tmp/';
$data = json_encode(["POST" => $_POST, "GET" => $_GET, "IP" => $_SERVER['REMOTE_ADDR']]);
$filePath = $tempFolderPath . '/receivedData-' . time() . '.txt';
$fileHandle = fopen($filePath, 'a');
if (
$fileHandle === false) {
    echo 
"Failed to open file for writing.";
    exit;
}
fwrite($fileHandle, $data);
fclose($fileHandle);
echo 
"Data received and stored successfully.";
?>

This is a very primitive example but shows how a C++ client can send data to a PHP server to save for review later.
I hope this helps you develop out your latest ideas! :)


RE: C++ Client to PHP Server /// TUTORIAL & SOURCE CODE - Klonopin - Mar 6, 2024

Pretty neat post Hackers , when I go about HTTP requests I usually stick to curl for a neater and cleaner experience.
both basically the same though, but curl is crossplat

POST request in curl:
Code
#include <iostream>
#include <string>
#include <curl/curl.h>

int main() {
    CURL *curl;
    CURLcode res;
    
    // initialize curl session
    curl = curl_easy_init();
    if (curl) {
        // Set the URL
        curl_easy_setopt(curl, CURLOPT_URL, "https://your-website.com/post/receive.php");
        
        // set the request type to POST
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        
        // set the POST data
        std::string postData = "something=goes&here=booya";
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
        
        // set the headers of the POST request
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        
        // perform the request to the webserver
        res = curl_easy_perform(curl);
        
        // check for errors
        if (res != CURLE_OK)
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
        
        // clean up curl request
        curl_easy_cleanup(curl);
        curl_slist_free_all(headers);
    }
    
    return 0;
}

GET request in curl:
Code
#include <iostream>
#include <curl/curl.h>

int main() {
    CURL *curl;
    CURLcode res;
    
    // initialize curl session ( were just gonna check for a 200 OK )
    curl = curl_easy_init();
    if (curl) {
        // set the URL
        curl_easy_setopt(curl, CURLOPT_URL, "https://your-website.com/resource");
        
        // perform the GET request
        res = curl_easy_perform(curl);
        
        // check for errors
        if (res != CURLE_OK)
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
        
        // clean up the GET request
        curl_easy_cleanup(curl);
    }
    
    return 0;
}
(very basic examples)


RE: C++ Client to PHP Server /// TUTORIAL & SOURCE CODE - Hackers - Mar 6, 2024

(Mar 6, 2024 08:45 AM)Klonopin Wrote:  Pretty neat post Hackers , when I go about HTTP requests I usually stick to curl for a neater and cleaner experience.
both basically the same though, but curl is crossplat

POST request in curl:
Code
#include <iostream>
#include <string>
#include <curl/curl.h>

int main() {
    CURL *curl;
    CURLcode res;
    
    // initialize curl session
    curl = curl_easy_init();
    if (curl) {
        // Set the URL
        curl_easy_setopt(curl, CURLOPT_URL, "https://your-website.com/post/receive.php");
        
        // set the request type to POST
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        
        // set the POST data
        std::string postData = "something=goes&here=booya";
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
        
        // set the headers of the POST request
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        
        // perform the request to the webserver
        res = curl_easy_perform(curl);
        
        // check for errors
        if (res != CURLE_OK)
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
        
        // clean up curl request
        curl_easy_cleanup(curl);
        curl_slist_free_all(headers);
    }
    
    return 0;
}

GET request in curl:
Code
#include <iostream>
#include <curl/curl.h>

int main() {
    CURL *curl;
    CURLcode res;
    
    // initialize curl session ( were just gonna check for a 200 OK )
    curl = curl_easy_init();
    if (curl) {
        // set the URL
        curl_easy_setopt(curl, CURLOPT_URL, "https://your-website.com/resource");
        
        // perform the GET request
        res = curl_easy_perform(curl);
        
        // check for errors
        if (res != CURLE_OK)
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
        
        // clean up the GET request
        curl_easy_cleanup(curl);
    }
    
    return 0;
}
(very basic examples)

Thanks for including this - I've had trouble with curl & ssl in the past so that's why I went a different route, but definitely would rather have something cross platform & also more modern than WinHTTP lol. I'm learning Visual Studio's options currently and that's going to make it a lot easier to start using packages. G++ for whatever reason, at least on my machine, refuses to play well with OpenSSL so I am hoping Visual Studio may make it easier to do that.


RE: C++ Client to PHP Server /// TUTORIAL & SOURCE CODE - Klonopin - Mar 6, 2024

(Mar 6, 2024 08:51 AM)nodes Wrote:  Thanks for including this - I've had trouble with curl & ssl in the past so that's why I went a different route, but definitely would rather have something cross platform & also more modern than WinHTTP lol. I'm learning Visual Studio's options currently and that's going to make it a lot easier to start using packages. G++ for whatever reason, at least on my machine, refuses to play well with OpenSSL so I am hoping Visual Studio may make it easier to do that.

If you ever have troubles including libraries and stuff in visual studio just dm me, I have a decent experience with it.

i usually end up using vcpkg most of the time tbh


RE: C++ Client to PHP Server /// TUTORIAL & SOURCE CODE - Hackers - Mar 6, 2024

(Mar 6, 2024 09:00 AM)Klonopin Wrote:  If you ever have troubles including libraries and stuff in visual studio just dm me, I have a decent experience with it.

i usually end up using vcpkg most of the time tbh

Yeah I need to get vcpkg or similar, and I want to think about using MSVC instead of g++
I had started using cmake files as well in past but then stopped using them and have been using VS Code tasks.json or similar for build steps.