AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   sending data to php script (https://forums.alliedmods.net/showthread.php?t=58038)

Cheap_Suit 07-17-2007 13:31

sending data to php script
 
How do you send data to a script and get the results?

This is how it was done from a brower from another php script:
PHP Code:

$sendMsg = new sendMsg($_POST["message"]);
$sendMsg->send()
 
// returns the result
$sendMsg->result 


hackziner 07-18-2007 12:38

Re: sending data to php script
 
Hum, so you want send data from a amxmodx plugin to a php script or send data from a php script to a amxmodx plugin ? who open the connexion ? In fact I think you've no choice because the socket module doesn't allow to listen for tcp connection ( I'm not sure @ 100% ).

edit : I've read your pm too fast ... ...


There is to method to send datas to a php script, get and post ... but get is easier :)


First we've to know how do a post/get request ...

The best way to understand is to analyse a post request.
//Open wireshark, which is certainly the best packet sniffer
//Open a thread
//Write a reply
//Record packet & click on reply

We get in the first packet
PHP Code:

POST http://forums.alliedmods.net/newreply.php?do=postreply&t=58056 HTTP/1.1
Hostforums.alliedmods.net
User
-AgentMozilla/5.0 (WindowsUWindows NT 5.1frrv:1.8.1.4Gecko/20070515 Firefox/2.0.0.4
Accept
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*;q=0.5
Accept
-Languagefr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept
-Encodinggzip,deflate
Accept
-CharsetISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep
-Alive300
Proxy
-Connectionkeep-alive
Content
-Typeapplication/x-www-form-urlencoded
Referer
http://forums.alliedmods.net/showthread.php?t=58056
Content-Length215
Pragma
no-cache
Cache
-Controlno-cache 

ofc there is a \r\n at the end of each line and a double at the end of the first packet

in the second packet
PHP Code:

ajax=1&ajax_lastpost=1184769975&message=Just%20useless%2C%20but%20well%20coded%20%3A%29%3Cbr%3E&wysiwyg=1&styleid=0&signature=1&fromquickreply=1&s=&do=postreply&t=58056&p=who%20cares&parseurl=1&loggedinuser=21173&s

So, most of the thing here are useless for us.


so to do a get request you've just to send:
PHP Code:

hackziner@hackziner:/usr/lib/cgi-bin/server/pr_directurltelnet amxmodx.org 80
Trying 74.52.23.194
...
Connected to c2.17.344a.static.theplanet.com.
Escape character is '^]'.
get http://forums.alliedmods.net/showthread.php?t=58038\r\n 

t take the value 58038 ... to get this value in php "$t = _GET["t"];"

If you wanna try, just open telnet and do the same thing ...

after sending this request, the http server reply on the same connexion.
So you can see if your request works with telnet ...


The second one with the post ... I'm too lazy :) It's the same thing but datas are after the http header ( here in the second packet )


So, how do to this thing in pawn ... just copy an other code :)


Admin mail do the same thing.

#include <sockets>

new buff[1024]
new socket = socket_open( "http://www.amxmodx.org"", 80, SOCKET_TCP, error );

//http uses tcp and the default port is 80 ...
formatex( buff, 1023, "get http://forums.alliedmods.net/showthread.php?t=58038^r^n" );

socket_send( socket, buff, 1024);


set a task with
if (socket_change( socket) socket_recv( socket, buff, 1023)
to get the reply...

The reply is something like that :



PHP Code:

HTTP/1.0 200 OK
Date: Wed, 18 Jul 2007 16:11:07 GMT
Server: Apache/2.2.3 (Unix) mod_ssl/2.2.3 OpenSSL/0.9.7a DAV/2
X-Powered-By: PHP/5.2.2
Cache-Control: private
Pragma: private
Content-Type: text/xml; charset=windows-1252
X-Cache: MISS from proxy.free.fr
X-Cache-Lookup: MISS from proxy.free.fr:3128
Proxy-Connection: close

<?xml version="1.0" encoding="windows-1252"?>
<postbits>
<postbit postid="504914"><![CDATA[<!-- post #504914 -->

<!-- open content container -->

Some servers need a full and correct request, other only need something which looks like the normal request :)

Rolnaaba 07-18-2007 12:38

Re: sending data to php script
 
Quote:

Originally Posted by Cheap_Suit (Post 504446)
How do you send data to a script and get the results?

he wants both. Send a mesage and get what it sends back.

Xanimos 07-18-2007 13:00

Re: sending data to php script
 
You need to use sockets to do this.

Here's a tut by Darksnow

Cheap_Suit 07-18-2007 13:22

Re: sending data to php script
 
I've seen that and still have no idea what to put in socket_send.

Edit: The php script return's a number regarding if the message was sent successfully

hackziner 07-18-2007 13:24

Re: sending data to php script
 
Lol, I'll sum up my previous post :)

GET request is easier than POST

You have to open a TCP connection to the host

new socket = socket_open( "http://www.amxmodx.org"", 80, SOCKET_TCP, error );

You have to format a GET request

formatex( buff, 1023, "get http://forums.alliedmods.net/showthread.php?t=58038^r^n" );

t is a variable and 58038 the value of t

send the request

socket_send( socket, buff, 1024);

define a task to get the page

if (socket_change( socket) //check if there is something waiting in the socket
socket_recv( socket, buff, 1023) //put the socket in buff


If you wanna try if you php page works, just open telnet, type the request and see the result ...

Cheap_Suit 07-18-2007 13:32

Re: sending data to php script
 
Thanks, I'll see if I can get to work later.

Cheap_Suit 07-18-2007 21:16

Re: sending data to php script
 
Quote:

Originally Posted by hackziner (Post 504947)
Lol, I'll sum up my previous post :)

GET request is easier than POST

You have to open a TCP connection to the host

new socket = socket_open( "http://www.amxmodx.org"", 80, SOCKET_TCP, error );

You have to format a GET request

formatex( buff, 1023, "get http://forums.alliedmods.net/showthread.php?t=58038^r^n" );

t is a variable and 58038 the value of t

send the request

socket_send( socket, buff, 1024);

define a task to get the page

if (socket_change( socket) //check if there is something waiting in the socket
socket_recv( socket, buff, 1023) //put the socket in buff


If you wanna try if you php page works, just open telnet, type the request and see the result ...

I understand little to none about sockets and php and this helped alot. But I was thinking to send it
to the main php script. How would I send the message using sockets like the code in my first post.

This is where I want to send my message and get the result from $result.
PHP Code:

class sendMsg
{
 var 
$_msg;
 var 
$result;
 
 function 
sendMsg($message)
 {
  
$this->_msg $message;
 } 



All times are GMT -4. The time now is 21:26.

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