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.
__________________