PHP HTTP Script

Posted by Tully on Tue 28 July 2009

This is a basic script I wrote to do GET/OPTIONS/HEAD requests to a webserver.

It uses PHP sockets for network communication.

PHP HTTP Communication using Sockets

<?php
if ($argc < 4) {
    echo "
    ---------------------------------------------------\n
    HTTP Check Headers Script (Written by Tully Rankin)\n
    Usage: headers.php [site] [get|head|options] filen\n
    Example: headers.php www.example.com get \n
    ---------------------------------------------------
    ";
    exit;
} else {
    $site = $argv[1];
}

$request = strtolower($argv[2]);
$file = $argv[3];

if ($request == "get") {
    $out = "GET $file HTTP/1.1rn";
    $out .= "Host: $sitern";
    $out .="Connection: Closernrn";
} else if ($request == "options") {
    $out = "OPTIONS $file HTTP/1.1rn";
    $out .= "Host: $sitern";
    $out .="Connection: Closernrn";
} else if ($request == "head") {
    $out = "HEAD $file HTTP/1.1rn";
    $out .= "Host: $sitern";
    $out .="Connection: Closernrn";
} else {
    echo "Incorrect Request Typen";
    exit;
}

$fp = fsockopen($site,80,$errstr,$errno,30);

if (!$fp) {
    echo "$errstr ($errno)";
} else {
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 1024);
    }
    fclose($fp);
}