Jump to content

Archived

This topic is now archived and is closed to further replies.

GregFenton

HTTP POST method, help required

Recommended Posts

Here's a little background to my problem..

I have a cable modem that has a habit of dying on me, normally either overnight or whenever I leave the house (such as right now).

I know the modem has a HTTP-based configuration page, and I know it uses the POST method to send the reboot command.

What I wish to do is write a simple script that pings a web site (eg yahoo) every 5 minutes, and if the web site cannot be found do a HTTP POST to the cable modem with the reboot command.

How can I send a HTTP POST in PHP?

Please understand that I don't mean "How can I have a web page process HTTP POST in php?"

Hope someone out there can help..

Greg

I have looked and found how to do the same thing via telnet, however Windows XP's telnet command does not let you pipe input to it:

How do we pass content to the HTTP server via standard input? Here's how to do it with telnet (If you don't use the same fname and lname values shown here, remember to adjust the Content-Length figure so that the posttest.cgi script knows how much to read in from the standard input):[code]POST /xml/crud/posttest.cgi HTTP/1.1
Host: snee.com
Content-Length: 21

fname=Barry&lname=Wom[/code]
The above is an example of what I would need to send.

Share this post


Link to post
Share on other sites
This script will send a request to yahoo.com every 5 minutes. If the connection is ok, it will display the time taken to respond, otherwise it will echo a form, with a hidden value & an image.

If you set the action of the form to the desired location to send the reboot command, and set the hidden field with the required settings, you will be able to perform the command required.

The image that is included is there only as a means to submit the data. The image includes an onLoad command to submit the form when the image is loaded.

Hope this helps you out.

At the moment the page submits the data to itself and has an if statement in place to display a message if the data has been submitted... this can be removed once you get your head around it.

[code]
<?php    session_start();
// Reload the page every 300 seconds (5 minutes)
echo "<META HTTP-EQUIV=Refresh CONTENT='300; URL=$PHP_SELF'>";

if($_POST[reboot]=="rebootModemNow")
{
    echo "Connection lost, rebooting modem";
}
else
{

    // Checksum calculation function
    function icmpChecksum($data)
    {
    if (strlen($data)%2)
    $data .= "\x00";
    
    $bit = unpack('n*', $data);
    $sum = array_sum($bit);
    
    while ($sum >> 16)
    $sum = ($sum >> 16) + ($sum & 0xffff);
    
    return pack('n*', ~$sum);
    }
    
    // Making the package
    $type= "\x08";
    $code= "\x00";
    $checksum= "\x00\x00";
    $identifier = "\x00\x00";
    $seqNumber = "\x00\x00";
    $data= "Scarface";
    $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
    $checksum = icmpChecksum($package); // Calculate the checksum
    $package = $type.$code.$checksum.$identifier.$seqNumber.$data;
    
    // And off to the sockets
    $socket = socket_create(AF_INET, SOCK_RAW, 1);
    if(!@socket_connect($socket, "http://www.yahoo.com", null))
    {
        echo "
        <form method=post name=rebootCableModemForm action=$PHP_SELF?action=reboot>
        <input type=hidden value=rebootModemNow name=reboot>
        </form>
        <img src=../apache_pb.gif onLoad='document.rebootCableModemForm.submit();'> ";}
        
    // If you're using below PHP 5, see the manual for the microtime_float
    // function. Instead of just using the m
    //     icrotime() function.
    $startTime = microtime(true);
    @socket_send($socket, $package, strLen($package), 0);
    if (@socket_read($socket, 255)) {
    echo round(microtime(true) - $startTime, 4) .' seconds';
    }
    socket_close($socket);
}
    ?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.