C++ outside UE4 help

Hello unreal engineers,

I tried to make a simple console program which sends a HTTP POST request to a php page. I’m sending a username to the php page, and the php should save the username to a database. But somehow whenever start the program, it connects with the php page, the php saves something to the database, but it’s empty, a null string.

Here is the code of C++:



#include <iostream>
#include <stdio.h>
#include <iostream>
#include <string>
#include "curl\include\curl\curl.h"

using namespace std;

void SendData(string &postData)
{
	CURL *pCurl;
	CURLcode res;

	pCurl = curl_easy_init();

	if (pCurl)
	{
		//Set target URL
		curl_easy_setopt(pCurl, CURLOPT_URL, "http://127.0.0.1/master.php");

		curl_easy_setopt(pCurl, CURLOPT_POST, 1);

		//POST data
		curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, postData);

		//Perform request
		res = curl_easy_perform(pCurl);

		//Check for errors
		if (res != CURLE_OK)
		{
			fprintf(stderr, "curl_easy_perform() failed: %s
",
				curl_easy_strerror(res));
		}

		//Always cleanup
		curl_easy_cleanup(pCurl);
	}
	curl_global_cleanup();


}

int main(void)
{
	cout << "MySQL communicator v1.0
";
	string post = "user=kolio";

	SendData(post);

	cout << "Executed 
";

	system("PAUSE");
	return 0;
}


And this is the php script:


	<?php
		$user = null;
		$id = null;

		$user = $_POST'user'];

		$conn = mysql_connect("localhost", "tts_user", "123");
		if ($conn) {
			$selDB = mysql_select_db("tts_db");

			if ($selDB) {
				//$user = "petko";
				//$sql = "SELECT id FROM test WHERE user = \"" . $user . "\"";
				$sql = "INSERT INTO test (user) VALUES('{$user}')";
				$result = mysql_query($sql);

				echo mysql_error($conn);

				/*if (mysql_num_rows($result) > 0) {
					while($row = mysql_fetch_array($result))
					{
						$id = $row'id'];
					}
				}*/
					
			}
		}
	?>

It seems like c++ doesn’t send anything, or I’m getting the POST vars wrong. Whenever I start the program, it returns me “undefined index user at line 24”. But the php executed again and saved a null string to the database. What I’m doing wrong?

PS. I’m aware of the safeties to check if POST is set and stuff like that.

Are you sure you are sending the string in the format that your server expects?

I read this about post data

http://curl.haxx.se/libcurl/c/CURLOPT_POSTFIELDS.html

“Pass a char * as parameter, pointing to the full data to send in a HTTP POST operation. You must make sure that the data is formatted the way you want the server to receive it. libcurl will not convert or encode it for you in any way. For example, the web server may assume that this data is url-encoded.”

Hey Rama, thanks for the reply

So if I create a



char* pPostData = "user=ozone";


should do the work right? Well I tried it and it doesn’t work again… it still says undefined index… And what do you mean by formatting the string the way the server accepts?

PS. It’s not server problem, I tried using curl.exe to send a POST request, and it worked. It doesn’t want to work from my program tho

I fixed it. I was missing curl_global_init(CURL_GLOBAL_ALL);