Comunicate PHP with server
#1

Hello, I have a question. Can I send, for example, a message using PHP to server? Is there any plugin to do that?
Explanation:
PHP код:
<?
//all other code
SendMessageToSrv(userid0xFFFFFF"bla bla bla bla");
?>
Need more info about this. Thanks.
Reply
#2

I'd suggest a socket plugin. Take a look at this thread:
https://sampforum.blast.hk/showthread.php?tid=333934

More information on how to use the socket plugin in PHP:

Quote:
Originally Posted by AndreT
Посмотреть сообщение
About the previous discussion... ports 0 - 1024, in most cases, require some sort of administrative rights on the system.

And BlueG, thanks a lot for this great plugin. I got the PHP-gameserver communications script that we spoke about earlier to work perfectly.

For anyone interested, here's something small I combined for use right now. The PHP code listens for connections incoming on port 7778.
pawn Код:
#include <a_samp>
#include <socket>

new Socket:Client;

public OnFilterScriptInit()
{
    // Create the client socket (feed data to webserver)
    Client = socket_create(TCP);
    if(is_socket_valid(Client))
    {
        socket_connect(Client, "127.0.0.1", 7778);
        socket_send(Client, "Hi, server!\n");
    }
    return 1;
}

public OnFilterScriptExit()
{
    // Destroy the socket if it is valid!
    if(is_socket_valid(Client))
        socket_destroy(Client);
}

// Any response from the PHP script (through PHP func socket_write) will fire this callback:
public onSocketAnswer(Socket:id, data[], data_len)
{
    printf("onSocketAnswer(%d, %s, %d)" _:id, data, data_len);
}
Note! When using socket_send, make sure to append \n when working according to this example, because the PHP code example below uses PHP_NORMAL_READ with socket_read (this means it will continue parsing, freezing rest of the script, until it finds a newline).

And a little PHP I've combined from various examples on the Internet. I haven't really gotten to finetuning it, nor have I mastered the background of socket operations. But this works fine for me.
PHP код:
// Time limit and flushing setting
set_time_limit(0);
ob_implicit_flush();

// Create the socket and make it listen for 127.0.0.1 on port 7778.
$socket socket_create(AF_INETSOCK_STREAMgetprotobyname('tcp')) or die("Error in socket_create: " socket_strerror(socket_last_error($socket)) . "\n");
socket_bind($socket"127.0.0.1"7778) or die("Error in socket_bind: " socket_strerror(socket_last_error($socket)) . "\n");
socket_listen($socket5) or die("Error in socket_listen: " socket_strerror(socket_last_error($socket)) . "\n");

while(
true)
{
    
// Accept an incoming connection!
    
$spawn socket_accept($socket);
    
    
// Send a welcoming notice
    
$welcome "I'm your humble host today, dear gameserver!";
    
socket_write($spawn$welcomestrlen($welcome));

    while(
true)
    {
        
// Try to read the input
        
$input socket_read($spawn1024PHP_NORMAL_READ);
        if(
$input === false)
        {
            
// Return to the first loop and look for a new connection to accept.
            
break;
        }

        if(
$input == "Hi, server!")
        {
            
$response "Hello to you as well, dear Sir!";
            
socket_write($spawn$responsestrlen($response));
        }
    }
}
socket_close($socket); 
I thought the second loop might be necessary so in case your gameserver disconnects from the PHP application, it will actually keep running the code and trying to establish a connection (socket_accept). Otherwise the PHP code will just reach its end.

I'm much of an experimenter so don't hit me if the code is buggy or something can be improved. I hope it gets the point of its abilities across. So far I've been using HTTP for sending all sorts of player data to the server.

Updated 15/04: Using PHP_NORMAL_READ in PHP's socket_read and ending plugins socket_send with a \n to correct issues that I missed when writing code for this forum :<
Updated 12/05: The PHP script now uses getprotobyname('tcp'), otherwise some Linux installations might get trouble.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)