• Print

Author Topic: http.Post  (Read 15825 times)

0 Members and 1 Guest are viewing this topic.

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
http.Post
« on: June 01, 2014, 11:30:39 pm »
Code: [Select]
http.Post('http://www.example.com', {
port = GetConVarString('hostport'),
hostname = GetHostName()
})

How would one make a page for this type of function? I've never delved into http, so I'd like to learn how.
Out of the Garry's Mod business.

Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: http.Post
« Reply #1 on: June 02, 2014, 10:34:59 am »
The easiest way to parse that would be to use PHP- You'd need a webserver running it (most do), or you can run XAMPP if you want to run it from your local windows machine.

If you're not familiar with PHP, you should probably follow some tutorials- but in essence, you could get your port and hostname values by accessing:
Code: PHP
  1. $_POST['port']
  2. $_POST['hostname']


Here's a very simple PHP script that would just display the two values in the webpage:
Code: PHP
  1. <?php
  2. echo $_POST['port'];
  3. echo '<br/>';
  4. echo $_POST['hostname'];
  5. ?>
« Last Edit: June 02, 2014, 10:37:13 am by Stickly Man! »
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: http.Post
« Reply #2 on: June 02, 2014, 11:05:01 am »
The easiest way to parse that would be to use PHP- You'd need a webserver running it (most do), or you can run XAMPP if you want to run it from your local windows machine.

If you're not familiar with PHP, you should probably follow some tutorials- but in essence, you could get your port and hostname values by accessing:
Code: PHP
  1. $_POST['port']
  2. $_POST['hostname']


Here's a very simple PHP script that would just display the two values in the webpage:
Code: PHP
  1. <?php
  2. echo $_POST['port'];
  3. echo '<br/>';
  4. echo $_POST['hostname'];
  5. ?>
Huh, never knew you knew PHP, Sticky. :P
/ot
Yeah, $_POST is the global variable stored by PHP for HTTP POST requests- it's an associative array which stores keys based on the variable names set in http.Post and values set to those variables' values.
If you wanted to, say, have a script that would connect the user when they click a button on your site, you could use:
Code: PHP
  1. <?php
  2. // Optional- uses less time and typing
  3. $host = $_POST['hostname'];
  4. $port = $_POST['port'];
  5. ?>
  6. <a href="steam://connect/<?php echo $host; ?>:<?php echo $port; ?>>Connect</a>
Of course, if your webserver is running > PHP 5.4 or has shorttags enabled,
Code: PHP
  1. <?php echo {var}; ?>
could be replaced by
Code: PHP
  1. <?={var};?>
but that's ot again, so... yeah.
bw81@ulysses-forums ~ % whoami
Homepage

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: http.Post
« Reply #3 on: June 02, 2014, 02:58:29 pm »
Is there a way to store that data? I'm trying to make a coderhire-like statistics page.
So when I access the page, I can see a list of these variables.
Out of the Garry's Mod business.

Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: http.Post
« Reply #4 on: June 02, 2014, 03:18:10 pm »
Depends on how you want to store the data. The easiest would be storing it in a flatfile- just append CSV values as a new line whenever someone visits the page. Then you could read back that data from that file on a separate page (or the same script, if no $_POST variables are set).

The likely preferred way is to use MySQL, especially if you already have a MySQL server already set up. You can read about how to interface it here (although there's more thorough documentation elsewhere): http://www.pontikis.net/blog/how-to-use-php-improved-mysqli-extension-and-why-you-should

Huh, never knew you knew PHP, Sticky. :P
I know everything! :D
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: http.Post
« Reply #5 on: June 02, 2014, 03:59:24 pm »
Code: PHP
  1. <?php
  2.  
  3. $host = $_POST['hostname'];
  4. $port = $_POST['port'];
  5.  
  6. if ($host == NULL && $port == NULL) {
  7.         $display = TRUE;
  8. }
  9.  
  10. if (!($display == TRUE)) {
  11.         $handle = fopen("statistics.csv", "a");
  12.         fputcsv($handle, $host);
  13.         fputcsv($handle, $port);
  14.         fclose($handle);
  15. } else {
  16.         $handle = fopen("statistics.csv", "r");
  17.         while (!feof($handle)) {
  18.                 $line = fgetcsv($handle, 1024);
  19.                 // What now?
  20. }
  21. ?>

Alright, so I got this far, how would I display the data on the page? Since I appended new lines onto statistics.csv, how would I output that to be displayed?
Out of the Garry's Mod business.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: http.Post
« Reply #6 on: June 02, 2014, 08:04:40 pm »
Code: PHP
  1. <?php
  2.  
  3. $host = $_POST['hostname'];
  4. $port = $_POST['port'];
  5.  
  6. if ($host == NULL && $port == NULL) {
  7.         $display = TRUE;
  8. }
  9.  
  10. if (!($display == TRUE)) {
  11.         $handle = fopen("statistics.csv", "a");
  12.         fputcsv($handle, $host);
  13.         fputcsv($handle, $port);
  14.         fclose($handle);
  15. } else {
  16.         $handle = fopen("statistics.csv", "r");
  17.         while (!feof($handle)) {
  18.                 $line = fgetcsv($handle, 1024);
  19.                 // What now?
  20. }
  21. ?>

Alright, so I got this far, how would I display the data on the page? Since I appended new lines onto statistics.csv, how would I output that to be displayed?
I'd take baby steps in something like this; do something like:
Code: PHP
  1. print_r( $line );
Since fgetcsv(); returns an array, it's nice to know what structure it uses. print_r(); will display this structure to you in a nicely formatted style.
bw81@ulysses-forums ~ % whoami
Homepage

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: http.Post
« Reply #7 on: June 02, 2014, 09:55:03 pm »
I'd take baby steps in something like this; do something like:
Code: PHP
  1. print_r( $line );
Since fgetcsv(); returns an array, it's nice to know what structure it uses. print_r(); will display this structure to you in a nicely formatted style.

Alright, I got it to print out all entries, but is there a function to return a server's ip address? (In Garry's Mod.)

EDIT: Nevermind, I somehow broke it. How do you loop the data until all of it has been printed?
« Last Edit: June 02, 2014, 11:09:50 pm by Neku »
Out of the Garry's Mod business.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: http.Post
« Reply #8 on: June 03, 2014, 09:31:56 am »
Alright, I got it to print out all entries, but is there a function to return a server's ip address? (In Garry's Mod.)

EDIT: Nevermind, I somehow broke it. How do you loop the data until all of it has been printed?
Foreach loops are great for arrays and objects.
Code: PHP
  1. foreach ( <array> as <varname> ) {
  2.     echo <varname>;
  3.     echo "<br/>";
  4. }

Replace <array> with your array you want to loop through and <varname> with something logical. Don't forget your $'s! :P
If you want the key name as well, replace <varname> with $key => $value. Both of these will be accessible as variables later on.
You could use them like this:
Code: PHP
  1. foreach ( <array> as $key => $value ) {
  2.     echo $key . ": " . $value;
  3.     echo "<br/>";
  4. }
Do note that the PHP docs warn of $key and $value, as well as <varname> if you used that approach, will still be set after the foreach is done They recommend using unset(); to clear those variables, and I do too.
Do something like:
Code: PHP
  1. // foreach loop
  2. unset( <varname> );
or, for the $key => $value method:
Code: PHP
  1. // foreach loop
  2. unset( $key );
  3. unset( $value );
« Last Edit: June 03, 2014, 10:35:45 am by Princess Twilight Sparkle »
bw81@ulysses-forums ~ % whoami
Homepage

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: http.Post
« Reply #9 on: June 03, 2014, 01:13:08 pm »
For a function to get the server's IP, I believe it was never really added...
If your host has -ip set on its start script (or if you do), doing something like
Code: Lua
  1. GetConVarString("ip")
might do the trick, but I don't know.
bw81@ulysses-forums ~ % whoami
Homepage

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: http.Post
« Reply #10 on: June 03, 2014, 04:02:36 pm »
Wow, thanks Circuit!

So I wrote this for lua:
Code: [Select]
hook.Add('Initialize','DM_R_U3wPd7qM0wC0764jzJ21u4G2xvx1TPq3', function() -- Test Statistics
http.Post('http://dm.x10host.com/statistics/test/statistics.php', {
port = GetConVarString( 'ip' ) .. GetConVarString( 'hostport' ),
hostname = GetHostName()
})
end)

And this for php:
Code: PHP
  1. <?php
  2.  
  3. $host = $_POST['hostname'];
  4. $port = $_POST['port'];
  5. $ppost = [$host, $port];
  6.  
  7. if ($host == NULL && $port == NULL) {
  8.         $display = TRUE;
  9. }
  10.  
  11. if (!($display == TRUE)) {
  12.         $handle = fopen("statistics.csv", "a");
  13.         fputcsv($handle, $ppost);
  14.         fclose($handle);
  15. } else {
  16.         $handle = fopen("statistics.csv", "r");
  17.         $line = fgetcsv($handle, 1024);
  18.         $linenum = 0;
  19.         foreach ( $line as $key => $value ) {
  20.                 if ($linenum == 0) {
  21.                         print "Hostname: " . $value . "<BR>";
  22.                 }
  23.                 if ($linenum == 1) {
  24.                         print "Port: " . $value . "<BR>" . "-----" . "<BR>";
  25.                 }
  26.                 unset( $key );
  27.                 unset( $value );
  28.                 if (!($linenum == 1)) {
  29.                         $linenum = $linenum + 1;
  30.                 } else {
  31.                         $linenum = 0;
  32.                 }
  33.         }
  34.  
  35.         fclose($handle);
  36. }
  37. ?>
« Last Edit: June 03, 2014, 04:08:05 pm by Neku »
Out of the Garry's Mod business.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: http.Post
« Reply #11 on: June 03, 2014, 06:14:23 pm »
Wow, thanks Circuit!

So I wrote this for lua:
-snip-

And this for php:
-snip-
Looks good, but I noticed you don't actually have "statistics.csv" on your webspace.
Is that intentional for the time being, or is it accidental?
Upon visiting the statistics.php page, I get these errors:
Quote
Warning: fopen(statistics.csv): failed to open stream: No such file or directory in /home/dmx10ho2/public_html/statistics/test/statistics.php on line 16
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /home/dmx10ho2/public_html/statistics/test/statistics.php on line 17
Warning: Invalid argument supplied for foreach() in /home/dmx10ho2/public_html/statistics/test/statistics.php on line 19
Warning: fclose() expects parameter 1 to be resource, boolean given in /home/dmx10ho2/public_html/statistics/test/statistics.php on line 35

Just wanted to point that out. :P

But anyway, you're welcome! I'm always here to help! :D
« Last Edit: June 03, 2014, 06:15:56 pm by Princess Twilight Sparkle »
bw81@ulysses-forums ~ % whoami
Homepage

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: http.Post
« Reply #12 on: June 03, 2014, 06:38:59 pm »
Statistics.csv won't be created until the lua I posted is run on either a client or a server.
Out of the Garry's Mod business.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: http.Post
« Reply #13 on: June 04, 2014, 01:10:02 am »
Statistics.csv won't be created until the lua I posted is run on either a client or a server.
Ah, OK- wondering if you'd tested it or not.
Anyway, from what I can tell, it all looks great!

IMO, I'd have used an SQL database, but to each his own.

I'm glad you got it mostly figured out! If you need any help, just give me a-- great, how do I finish this now? UNINTENTIONAL RHYME TRAP!!!
Just give me a shout if you need me... there.
bw81@ulysses-forums ~ % whoami
Homepage

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: http.Post
« Reply #14 on: June 04, 2014, 01:14:50 pm »
I sent an example POST request using a Chrome extension (so clear statistics.csv), and everything seems to work OK.
Though, from what I see in your Lua (noticing this now):
Code: Lua
  1. port = GetConVarString( 'ip' ) .. GetConVarString( 'hostport' )
would return something like:
<ip><port>
(ex. 127.0.0.11234)
Wondering if you wanted that over:
<ip>:<port>
(ex. 127.0.0.1:1234)
bw81@ulysses-forums ~ % whoami
Homepage

  • Print