4

i'm searching the inet for around 3 days now and i'm stuck at this.

I got a MySQL Database and a php Script, as well as a Game made in UE4. UE4 uses c++.

So now i want to send requests from the c++ game to the php script and that shall interact with the database. For example create an account or login. I also want to pass the mysql query result of the php script to my c++ class. I tried using HttpRequest, but i can't get data from php to c++ with that.

Maybe you can, but i don't understand it at all. What i accomplished by now is that you can send a POST request from the game to the php script and pass variables so that the script uses them to perform the mysql query. But how can i pass data from the php file to c++ now? The response i get is always the whole site (head and body) and i don't know where i could save the query result to pass it to the c++ code.

I'm a full beginner here, so go easy on me. I read so many different posts and blogs that my brain hurts like hell ): I hope someone can tell me how to do this easily or at least give me a hint on what i have to google and what i could use. I don't need a full tutorial, just a name of a library better than the Http.h (if simple HttpRequest cant manage this) would be enough. ): I'm really frustrated...

eXi

eXifreXi
  • 43
  • 1
  • 4
  • httprequest requires a webserver. no webserver, not http. your c++ app can always exec() an external `php.exe yourscript.php` and read its output/return values. but the question would be why would you need PHP for this? c++ can connect to mysql perfectly well already anyways. – Marc B Aug 22 '14 at 15:50
  • You could take a short cut and access the database from your C++ game. Search the web for "mysql connector c++". – Thomas Matthews Aug 22 '14 at 16:06
  • @eXifreXi Please post the exact string that you receive as HTTP response. There seems to be an ambiguity as to where you are at at the moment. – RandomSeed Aug 22 '14 at 16:10
  • Oh sorry guys, i didnt see the comments. Please read my comments on the answer of RandomSeed. That should cover your questions. The thing with the direct connection is, that everyone tells me to not do this at all, because a direct connection of my client with the database gives possibilities to cheat or at least get into the database :/ – eXifreXi Aug 22 '14 at 16:28
  • Recommended reading: [Why should a developer use web services instead of direct connections to a db?](http://stackoverflow.com/q/2142070/1446005) – RandomSeed Aug 25 '14 at 00:25

2 Answers2

3

The PHP script should retun a HTTP response reduced to a bare minimum. It doesn't even need to be a HTML document:

<?php

    // file: api.php

    $param = $_POST['myparam'];

    $foo = bar($param); // $foo contains e.g. "1,ab,C"
    echo $foo;          // if you opened http://myhost.com/api.php in a browser
                        // all you would see is "1,ab,C"
                        // (which is not a valid HTML document, but who cares)

?>

Then parse this HTTP response (a plain string, that is) from your game. You can use your own data format, or use a well-known format of your choice (XML or JSON are good candidates).

RandomSeed
  • 29,301
  • 6
  • 52
  • 87
  • Just to add that the first `\r\n\r\n` in the response will indicate the boundary between headers and content. See [rfc2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2) – Basic Aug 22 '14 at 15:54
  • Ok, so far i understood this. It was mentioned often enough on other sides. The problem is, i don't know how to set the data that the HTTP Response provides. I can get the ResponseContent as a string inside the game, but this is always the php script code. If i echo $foo like you said, how can i get the value of foo inside the c++ file? Do i have to change the header files of the request and php file? ): Sorry if these are dumb questions, but i can't get it into my head. – eXifreXi Aug 22 '14 at 15:58
  • @Basic This is true if OP were reading raw data from the network socket. Hopefully, she uses a higher-level API that parses the HTTP response and separates header from contents. – RandomSeed Aug 22 '14 at 15:59
  • From OP... `The response i get is always the whole site (head and body)` but I agree something higher-level would be nicer. [Merged Comments] If you're getting the _contents_ of the script file, it sounds like PHP isn't being invoked on the server. It should _never_ return the PHP source code if it's configured correctly. Try browsing to the page yourself. Does it give the response you want? – Basic Aug 22 '14 at 16:00
  • Allright, then I am afraid I must refer you to [Marc B's comment](http://stackoverflow.com/questions/25450821/i-want-to-send-requests-from-a-ue4-c-game-to-my-php-script-so-that-it-interac/25450986?noredirect=1#comment39711406_25450821). You need a web server or at least a working PHP interpreter. As Basic says, don't look further, your PHP script is not being interpreted by your server (there *is* a working PHP-enabled web server in your setup, isn't there?) – RandomSeed Aug 22 '14 at 16:02
  • @Basic My understanding of "the whole site (head and body)" was OP is receiving a full HTML document, with its `` and ``, now I am not sure. But OP should post the exact response that she currently receives. – RandomSeed Aug 22 '14 at 16:06
  • Ah ok, good point. I read that differently // For the OP, some things to check: http://stackoverflow.com/a/4189637/156755 – Basic Aug 22 '14 at 16:08
  • I have Xampp installed. There lays my .php file and the mysql database. As i said, i'm a big noob on this topic. Just to show you what my codes are: C++:http://pastebin.com/bsXfGdEV php:http://pastebin.com/mci7dxT4 And the Content of the ResponstContentString: http://puu.sh/b3jwt/7dfca02eeb.jpg I guess i'm doing something totaly stupid :X – eXifreXi Aug 22 '14 at 16:17
  • 2
    You are receiving extra contents because... you explicitely put it there! Your PHP code produces a **HTML document**, which is much more than what you need. **Just `echo 1` on success, and `echo 0` on failure**. All you need is a boolean value! – RandomSeed Aug 22 '14 at 16:20
  • Ok, so i just delete head and body and strip the echos down to 0, 1 (false, true). Can i easily echo a variable that contains a mysql query rueslt like this too? If yes, i guess that solves my problem at the moment. :X – eXifreXi Aug 22 '14 at 16:26
  • The HTTP response will always be a plain flat string. To return complex data, you must use some kind of protocol to make sense out of this string. Well-known formats exist to transfer complex, structured data such as tabular data from a database result set. I mentionned XML or JSON, since most languages, including PHP, are natively able to generate and parse such formats. You can also devise your own protocol if you want. – RandomSeed Aug 24 '14 at 23:56
  • Side notes regarding your usage of `http_response_code()` in your PHP snippet: code 501 means "Not implemented", the appropriate (read: standard) code should be 403 "Forbidden". This also got me thinking: for authenticating a username/password, you could handle the case with HTTP status codes only (403 "Forbidden" or 204 "OK - no content". This is perhaps a bit too hackish, though. – RandomSeed Aug 25 '14 at 00:14
1

The json object in unreal is pretty good, so I would recommend outputting json from your php script. Json in php is a pretty natural workflow.

<?php
    $obj['userid'] = 5476;
    $obj['foo'] = 'bar';
    echo json_encode($obj);
php?>

That will echo out

{"userid":5476,"foo":"bar"}

If that's all you output in your script then it's pretty straightforward to treat that as a string and populate an unreal json object with it.

FString TheStuffIGotFromTheServer;
TSharedPtr<FJsonObject> ParsedJson;
TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(TheStuffIGotFromTheServer);
if (FJsonSerializer::Deserialize(JsonReader, ParsedJson))
{
    FString foo = ParsedJson.GetStringField("foo");
    double UserId = ParsedJson.GetNumberField("userid");
}

Check out the unreal json docs to get a feel for what you can do with it.

pdylanross
  • 265
  • 3
  • 7