Announcement

Collapse
No announcement yet.

HTTP data format issues

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    HTTP data format issues

    Hi. I am tring to setup a basic HTTP connection between my game and localhost. The connection is working fine. But, I am receiving null at the database.

    Here is the code chunk being used:



    TSharedRef<IHttpRequest> Request = FHttpModule::Get().CreateRequest();
    Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
    Request->SetURL(TEXT("http://127.0.0.1/insertGameStats.php"));
    Request->SetVerb(TEXT("POST"));
    Request->SetContentAsString(CurrentRequest.TheData);

    Request->OnProcessRequestComplete().BindUObject(this, &AHTTPHandlerActor::OnResponseReceived);


    Here is how I set data:

    CurrentRequest.AddURLPair(param, value);
    void AddURLPair(FString param, FString value)
    {
    TheData += (delim + param + "=" + value);
    delim = "&";
    }



    The php Script:



    $con = mysql_connect($db_hostname,$db_username);



    if(!$con)
    {
    die("Unable to Connect to MySQL: " . mysql_error());
    }


    $val = $_POST['val'];

    mysql_select_db($db_database, $con);

    $sql = "INSERT INTO testtable(value)
    VALUES
    ('$val')";

    if(!mysql_query($sql, $con))
    {
    die('Error: ' . mysql_error());
    }

    echo "1 record added to the game stats database";

    mysql_close($con);


    I know that the connection is being established, because a new entry is being made in the table. But, it is all NULL.

    Any help would be greatly appreciated

    #2
    I am facing similar issues setting up the php script to receive the data. I keep receiving NULL at the php side. Please help.

    Comment


      #3
      I have been trying to setup a way to get some gameplay stats to a server(for analysis), but I've been running into similar issues as well. The request does reach my localhost, but not the data. My database has a bunch of rows with just the default value, and not the value I want to send. Also, I have a similar code, but I'm accessing it through blueprints. I need help!

      Comment


        #4
        Since you are using Json, i guess your Data is formated to be a JsonString.

        Try this in your PhP Script:

        $json_incoming = json_decode(file_get_contents('php://input'));
        and to get the variable values do this:

        $username = mysql_real_escape_string($json_incoming->username);
        A Json String can look like this:

        { "username":"TEST" }
        In UE4 you eventually need to escape the " with \
        Open for contracted work | C++/BP (incl. Multiplayer) | Tutoring | VR

        My UE4 Blog/Page with Tutorials and more: Hit me for ALL the things!
        (Including 100+ Pages Multiplayer Network Compendium to get you started.)

        Comment


          #5
          Thanks. That gave me a starting point. I changed my header to Request->SetHeader("Content-Type", "application/x-www-form-urlencoded");
          Using this, I was able to receive using the same php script.
          Thanks a lot for the help.

          Comment

          Working...
          X