PHP网络工具 - 卷曲[关闭]

I am looking at creating a website which can preform some basic networking tests on servers/clients.

I am using php to run the following test:

  • Ping
  • Traceroute
  • Dig
  • Curl

With curl for example, if I was troubleshooting a networking problem from the command line I usually use the command:

curl -Ik <website-url/ip-address>

This returns:

HTTP/1.1 200 OK
Date: Wed, 01 Jan 2014 14:30:13 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.8
X-Pingback: http://<ip-address>/xmlrpc.php
Link: <http://<ip-address>/?p=83>; rel=shortlink
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8

So far I have created a basic curl function that just curls a website and displays that website:

public static function curl($host) {
    $ch = curl_init($host);
    $fp = fopen("index.html", "w");

    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
}

Is there any simple method to display similar information that is returned with the command line example listed above?

Just to clarify I would like to create a php function that can curl a website and if possible be passed arguments in a similar manner to the command line. So by default it just does what the above PHP code does, but if a certain parameter is checked it then returns the HTTP headers or allows curl to perform "insecure" SSL connections and transfers in as if the -I or -k parameters were passed to curl on the command line.

Also any good ideas on other networking tools to include within this tool would be great.

You can look at

http://it2.php.net/curl_setopt

to see all the possible options available on PHP's curl. Basically every option available on the command line can be achieved with

curl_setopt

From your question it seem to me you just need to test a URL and retrieve the header.

You may rewrite the function as follows, avoiding the use of a "index" file but just retrieving the headers into a variable and returning them from the function.

public static function curl($host) {
    $ch = curl_init($host);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER,    true);
    curl_setopt($ch, CURLOPT_NOBODY,            true);
    curl_setopt($ch, CURLOPT_HEADER,            true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,    false);

    $output = curl_exec($ch);
    curl_close($ch);

    return $output;
}

The options are:

RETURNTRANSFER: the data retrieved is returnd drom curl_exec

NOBODY: only the header is retrieved, not the body, equivalent to -I from the command line

SSL_VERIFYPEER: equivalent to -k from the command line

HEADER: retrieve the header