AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Posting to a PHP script (https://forums.alliedmods.net/showthread.php?t=130583)

Fishcake 06-25-2010 08:46

Posting to a PHP script
 
Does anyone know if it is possible to POST to a PHP script via SM?

If so, what would be the best way of going about it?

Monkeys 06-25-2010 10:59

Re: Posting to a PHP script
 
If there is no direct method implemented, you could use an intermediary page.
You load the page which will then post to the actual php page you need.

Fishcake 06-25-2010 11:07

Re: Posting to a PHP script
 
What I'm looking to do is give people the ability to send data to a global database, but I'd like to have some control on what comes back.

So rather than give people mySQL details, where they could insert anything they wanted, I would build a PHP API that people can send data to, like a mySQL query.

This is where I am a bit stuck, as I'm not sure how I would POST the data from SM.

Kilandor 06-25-2010 14:37

Re: Posting to a PHP script
 
GET can be done via Causing a Hidden MOTD window to open a url (invisible). However your limited to 255 characters.

But that wont' work for POST.

Your going to have to use Sockets You can use my Plugin as a guideline

https://forums.alliedmods.net/showthread.php?t=117577

Fishcake 06-25-2010 15:12

Re: Posting to a PHP script
 
Is there any chance you could dumb it down a bit so its a tad easier to understand?

This would be my first time I've used sockets and I'm not 100% sure where I would start.

Kilandor 06-25-2010 16:21

Re: Posting to a PHP script
 
Code:

#include <sourcemod>
#include <socket>
#include <base64>

#define PLUGIN_VERSION "1.0"
public Plugin:myinfo =
{
    name = "Sockets POST Example",
    author = "Kilandor",
    description = "Simple example to show how to post data to a URL",
    version = PLUGIN_VERSION,
    url = "http://www.kilandor.com/"
};

new String:host[256], String:baseURL[256], String:secKey[256];

Format(host, sizeof(host), "www.example.com"); //Domain of the site to POST to
Format(baseURL, sizeof(baseURL), "index.php"); //File and or path to POST to
Format(secKey, sizeof(secKey), "Some Security Key(recommend CVAR USAGE instead)"); //Security Key, or something of the sort to use on backend to identify, or verify authorized access

public Action:Command_PostExample()
{
    new String:headers[1024];
    new String:base64[1024];
    new Handle:socket;

    //Encodes the message for handling in the URL on the Backend
    EncodeBase64(base64, sizeof(base64), "data to send encoded to prevent having to URL encode");

    //Combines the data, and secKey to prevent unauthorized use
    //This data can be retreived in php with (example: $_POST['data'])
    //The format here is basicly the same as GET, without the ?
    Format(headers, sizeof(headers), "data=%s&seckey=%s", base64, secKey);

    //Used to pass the POST data to the socket
    new Handle:headers_pack = CreateDataPack();
    WritePackString(headers_pack, headers);
    //Creates a new Socket handle
    socket = SocketCreate(SOCKET_TCP, OnSocketError);
    //Sets extra argument data to passback
    SocketSetArg(socket, headers_pack);
    //Establishes connection to the host, and sets up callbacks
    SocketConnect(socket, OnSocketConnected, OnSocketReceive, OnSocketDisconnected, host, 80)
}

public OnSocketConnected(Handle:socket, any:headers_pack)
{
    // socket is connected, send the http request
   
    //Resets the datapack to begining
    ResetPack(headers_pack)
    new String:headers[1024];
    //Gets POST data we created before from the datapack
    ReadPackString(headers_pack, headers, sizeof(headers));
   
    decl String:requestStr[1024];
    //This Formats the headers needed to make a HTTP/1.1 POST request.
    Format(requestStr, sizeof(requestStr), "POST /%s HTTP/1.1\nHost: %s\nConnection: close\nContent-type: application/x-www-form-urlencoded\nContent-length: %d\n\n%s", baseURL, host, strlen(headers), headers);
    //Sends the Request
    SocketSend(socket, requestStr);
}

public OnSocketReceive(Handle:socket, String:receiveData[], const dataSize, any:headers_pack) {
    //Used for data received back (I think)
}

public OnSocketDisconnected(Handle:socket, any:headers_pack) {
    // Connection: close advises the webserver to close the connection when the transfer is finished
    // we're done here
    CloseHandle(headers_pack);
    CloseHandle(socket);
}

public OnSocketError(Handle:socket, const errorType, const errorNum, any:headers_pack) {
    // a socket error occured
    LogError("[CallAdmin] socket error %d (errno %d)", errorType, errorNum);
    CloseHandle(headers_pack);
    CloseHandle(socket);
}

You will need
Sockets 3.0.0 Alpha
base64.inc

I use base64 to easily encode the data which can the easily decoded by PHP

I stripped out everything bare minimum that is needed. In this case you would simply call Command_PostExample to execute the connection.

And you can setup your PHP script to save some data to a file to see what all happens and what not.

Fishcake 06-25-2010 16:58

Re: Posting to a PHP script
 
Genius! I've managed to get a reply from my PHP script and PHP picks up the variables I'm sending.

Only problem I'm having now is on the Receive I get back a lot of junk such as the headers. I also get a 'c' before my value and a '0' after the value. Is there a way to just get back just the page data? (http://zmstat.co.uk/capture.php is what I'm testing with)

Also, thanks for your awesome post :)

Kilandor 06-26-2010 01:24

Re: Posting to a PHP script
 
I haven't tried the receive myself but here is a decent looking post

http://forums.alliedmods.net/showpos...&postcount=274

Fishcake 06-26-2010 12:23

Re: Posting to a PHP script
 
Well, I've already managed to print the data, It's just that the response data includes all of the headers too.

Anyone know if I can get rid of those?

Thrawn2 06-26-2010 12:31

Re: Posting to a PHP script
 
as far as i remember the header ends with \n\n


All times are GMT -4. The time now is 14:03.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.