• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code http_post_string

Neptune

Member
I think http_post_string might be bugged - In that, a web server does not actually recognize the request method to be 'POST' and thus using $_POST to gather the data in PHP returns empty.

I've found only one example of http_post_string, and it is being used incorrectly:
GML:
http_post_string(URL,"");
The empty string defeats the purpose of the function, and further makes me think that there is a problem there.

If someone has an example where http_post_string is being used correctly & successfully, or can prove me wrong, I'd greatly appreciate.
 

FrostyCat

Redemption Seeker
PHP:
PHP:
<?php
if (isset($_POST['name']) && isset($_POST['score'])) {
    $name = $_POST['name'];
    $score = $_POST['score'];
    echo "Hello $name! Your score is $score.";
} else {
    echo "INCOMPLETE REQUEST";
}
GML: Create Event:
GML:
xhr_hello = http_post_string("http://localhost/hello.php", "name=Alice&score=1234");
GML: Async HTTP Event:
GML:
if (async_load[? "id"] == xhr_hello) {
    switch (async_load[? "status"]) {
        case 1: break;
        case 0:
            show_message("Response: " + async_load[? "result"]);
        break;
        default:
            show_message("Request failed!");
    }
}
Tested on IDE 2.2.5.481 / Runtime 2.2.5.378 Windows VM / PHP 7.4.4 on XAMPP. Output:
Response: Hello Alice! Your score is 1234.
 

Neptune

Member
:eek: maybe the isset portion in PHP is where I'm going wrong, if using isset is required.

If it still doesn't work, I'll also try my code on XAMPP just to be sure.
Thankyou for testing.

EDIT:

Hmm, idk. Still nothing.
Doesn't make much sense to me, because http_get works fine with web server and to switch to http_post_string is a just a few very simple changes.
 
Last edited:

Neptune

Member
Ok I ran this on PHP script:
PHP:
if (isset($_POST['name']) && isset($_POST['score'])) {
    $name = $_POST['name'];
    $score = $_POST['score'];
    die("Hello $name! Your score is $score.");
} else{
    die("INCOMPLETE REQUEST");
}
And this in GM:
GML:
http_attempt = http_post_string("https://domain.net/script.php", "name=Alice&score=1234");
Few things to note, I changed your ECHO to DIE, and the only other difference I can think of is that my web domain is using https and not http.


The show_message on the async_load[? "result"] was "INCOMPLETE REQUEST"
 

Neptune

Member
@chamaeleon Yeeeah kinda. I've went down a bunch of stackoverflow rabbit trails, but honestly the majority of what I see is strictly web based, and I feel like doing this with GM is the obstacle, and not so much using POST being difficult.
Few years back I had some simple web forms use $_POST no problems... And I knew even less about what I was doing then 😅
 

chamaeleon

Member
@chamaeleon Yeeeah kinda. I've went down a bunch of stackoverflow rabbit trails, but honestly the majority of what I see is strictly web based, and I feel like doing this with GM is the unknown, and not so much using POST.
Few years back I had some simple web forms use $_POST no problems... And I knew even less about what I was doing then 😅
You should consider learning the fundamentals of using tools like curl to hand-craft queries that you can test your PHP API with, without having to rely on GMS. When those queries does the right thing, then you can figure out if your GMS programs works as expected.
 

Neptune

Member
Currently the problem really isn't anything with my PHP query on the server side. I'm just getting an empty result with this:
PHP:
$name = $_POST['name'];
Before my database query even runs...


That being said, I've done more complex database things in the past, like an instant messenger built into a website (it was a disaster) but it executed way more complex database operations than what I'm attempting to do here.
 

chamaeleon

Member
Currently the problem really isn't anything with my PHP query on the server side. I'm just getting an empty result with this:
PHP:
$name = $_POST['name'];
Before my database query even runs...


That being said, I've done more complex database things in the past, like an instant messenger built into a website (it was a disaster) but it executed way more complex database operations than what I'm attempting to do here.
Skip anything database related, just have a single PHP file that contains @FrostyCat's code, and use curl until you get the expected response. Then work with the database, then work with GMS.
 

Neptune

Member
Bah, I can't justify throwing in another layer of unknowns right now, while http_get works perfectly fine.

If http_get works, and im not typing $_POST incorrectly in script, then everything outside of GM is done.
 
Top