[tutorial] how to use HttpManager for php + maniascript

You can talk about ManiaScript for ManiaPlanet here

Moderator: English Moderator

Post Reply
reaby
Posts: 956
Joined: 29 Dec 2010, 23:26
Location: Eastern Finland
Contact:

[tutorial] how to use HttpManager for php + maniascript

Post by reaby »

Hi, i write this for very simplified example for input/output in gamemode maniascript,
you can use this techniques forexample an external mysql connection <=> maniascript :)


ManiaScript

declare this as script global variable for your script

Code: Select all

#Include "Libs/Nadeo/Json.Script.txt" as JSON
declare CHttpRequest[] myRequests = CHttpRequest[]; 
do this at beginning or end of the active playloop..
(very simple httprequest manager)

Code: Select all

declare Integer[] toRemove = Integer[];
declare x = 0;

foreach (req in Http.Requests) {
    if (req.IsCompleted) {
        log(req.Result);    
        toRemove.add(x);
    }
    x += 1;
}

foreach (index in toRemove) {    
    Http.Destroy(Http.Requests[index]);    
}
 
do to trigger http request, do anywhere inside the playloop:

Code: Select all

myRequests.add(Http.CreatePost("http://localhost/tm2/test.php", "{" ^JSON::Stringify("player",Event.Player.User, "") ^"}"));
I used this code inside Event foreach loop at Event::onWaypoint
Also
it's very important remeber to add the object tags "{ }" around the json library output...


php

Code: Select all

<?php
file_put_contents("log.txt", $HTTP_RAW_POST_DATA, FILE_APPEND);
$object = (object) json_decode($HTTP_RAW_POST_DATA);

// all you echo, will be sent back to the maniascript...so generate the xml document here
if ($object->player->Login == "reaby") {
    echo "yay!!";
}

print_r($object);  // this will output the recieved object values to your maniaplanet debug window
 ?>
reaby
Posts: 956
Joined: 29 Dec 2010, 23:26
Location: Eastern Finland
Contact:

Re: [tutorial] how to use HttpManager for php + maniascript

Post by reaby »

and... little bit more complicated example, using xml for return transport...

ManiaScript

Code: Select all

declare Integer[] toRemove = Integer[];
declare x = 0;
declare CXmlDocument xmlDoc;

foreach (req in Http.Requests) {
    if (req.IsCompleted) {
        xmlDoc = Xml.Create(req.Result);
        log(req.Result);
        declare CXmlNode datas = xmlDoc.GetFirstChild("datas");
        declare CXmlNode medals = datas.GetFirstChild("medals");
        declare CXmlNode record = datas.GetFirstChild("record");
        log(medals.TextContents);
        
        log(record.TextContents);
        
        
        toRemove.add(x);
    }
    x += 1;
}


foreach (index in toRemove) {
    Http.Destroy(Http.Requests[index]);    
}

php

Code: Select all

<?php
$object = (object) json_decode($HTTP_RAW_POST_DATA);
// print_r($object);

$array = array();
$array['login'] = $object->player->Login;
$array['datas'] = array("medals" => 1, "record" => 1337);

echo assocArrayToXML("document", $array);


// ----------------------------------------------------------------------
// this code snipplet is from 
// http://php.net/manual/en/book.simplexml.php#101949
// ----------------------------------------------------------------------
function assocArrayToXML($root_element_name,$ar)
{
    $xml = new SimpleXMLElement("<{$root_element_name}></{$root_element_name}>");
    $f = create_function('$f,$c,$a','
            foreach($a as $k=>$v) {
                if(is_array($v)) {
                    $ch=$c->addChild($k);
                    $f($f,$ch,$v);
                } else {
                    $c->addChild($k,$v);
                }
            }');
    $f($f,$xml,$ar);
    return $xml->asXML();
} 
?>
correction:
Removed Xml-header from simpleXmlElement generation, thanks for noraynea for the correction!
Last edited by reaby on 02 Dec 2014, 12:54, edited 1 time in total.
User avatar
magnetik
Posts: 1673
Joined: 01 Feb 2012, 19:13
Location: Bordeaux
Contact:

Re: [tutorial] how to use HttpManager for php + maniascript

Post by magnetik »

Just a little remark : you can't really rely on the data sent since your script is more or less open source and there is no auth mecanism at the moment.

We are working on some prototypes that may allow you to do so :thumbsup:
ManiaPlanet technical documentation portal (Dedicated, ManiaLink, ManiaScript, Titles...) -- contribute!
User avatar
spaii
Posts: 1075
Joined: 19 Jun 2010, 00:04
Location: Rémy - France
Contact:

Re: [tutorial] how to use HttpManager for php + maniascript

Post by spaii »

magnetik wrote:We are working on some prototypes that may allow you to do so :thumbsup:
Nice to know that :thumbsup:
Post Reply

Return to “ManiaScript”

Who is online

Users browsing this forum: No registered users and 1 guest