Windows How to get an echo value back from website?

Raoul

Member
Hi,
I have a php script which I connect to succesfully from within my game.
the script run all right and interacts with a mysql server.

I am now struggling to understand how to get a value back from the call to the script.

No need to get complex data, just a char returned by a echo "x" command at the end of the script.
If I run the script via the browser I see the echo string returned in the html page.

I tried to figure out how to do so but I got extremely confused after a dozen attemtps with the following example:

https://docs.yoyogames.com/source/dadiospice/002_reference/asynchronous functions/http_post_string.html

I understand that when I assign a variable to the http_post_command like:

myvar= http_post_string("http://www.newgame.com/game.php", str)

then I will need to check for id, status and result in a ds_map but tthis is where I got lost :(

Sorry if this has been answred already, I am still confusing after reading all the thread that I could find (I am totally new to html/php and how they return values to callers.

Thanks!
 
Last edited:

FrostyCat

Redemption Seeker
What's so confusing about it? It's just the same 3-step procedure time and time again.
  1. Fire off the request with http_post_string() or any other HTTP function as appropriate, and remember its request ID.
  2. In the Asynchronous HTTP event (Asynchronous > Async - HTTP in GMS 2, Asynchronous > HTTP in GMS 1.4), check async_load[? "id"] to make sure the event belongs to the right request, and async_load[? "status"] to make sure the request is done and error-free.
  3. If both checks hold, then get the body of the request in async_load[? "result"].
Start your request and store its request ID (step 1):
Code:
xhr_game = http_post_string("http://www.newgame.com/game.php", str);
Then in the HTTP event, check async_load[? "id"] against the stored request ID and async_load[? "status"] against 0 (step 2), and use async_load[? "result"] when both checks hold (step 3):
Code:
if (async_load[? "id"] == xhr_game && async_load[? "status"] == 0) {
  // Example: Show the response
  show_message(async_load[? "result"]);
}
In live practice, you probably also need to handle cases where async_load[? "status"] is less than 0 (when the request fails) and/or equal to 1 (when the request is in progress but not all of the content has arrived back yet). I generally use this expanded template to account for these:
Code:
/* Replace xhr_id with the actual request ID */
if (async_load[? "id"] == xhr_id) {
  switch (async_load[? "status"]) {
    // Request in progress
    case 1:
      /*
      Optionally use async_load[? "contentLength"] and async_load[? "sizeDownloaded"] here, but DON'T use async_load[? "result"]
      Leave it blank if nothing needs to be shown, but the case and the break must be here
      */
    break;
    // Request done
    case 0:
      /*
      Use async_load[? "result"] here
      */
    break;
    // Request failed
    default:
      /*
      Handle errors here
      if applicable, use async_load[? "http_status"] and async_load[? "result"] to determine the kind of error
      */
    break;
  }
}
More cases to handle, but still the same 3-step procedure as before. Nothing more to it, and definitely nothing to be scared of.
 

Raoul

Member
Hi FrostyCat,
I did not have time to test it but as soon as I tried (today) it worked as expected!

Once again I want to tank you for the extraordinary help that you provide to the others!
 
Top