tags:

views:

67

answers:

4

I'm writing a PHP error collection widget that collects errors from an application and then by making a POST request to a URL, reports them.

How many different ways to do this are there, and how would you detect whether they are available? On different servers extensions may not be installed and security options may be turned off so I'd like to try as many as possible.

  • CURL is one; what's the best way to check whether this is installed?
  • fopen() can do URL's if it has permission but I don't think you can send POST data?
  • http://uk3.php.net/manual/en/book.http.php looks like it needs curl, so we may as well just use curl.
  • PHP sockets could be used, and just write the HTTP protocol yourself; any good libraries for this?

This service is open source under the BSD license BTW: http://elastik.sourceforge.net/

+1  A: 

Oh wait, It looks like fopen can include POST data: http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl

EDIT: couldn't get code from that URL to work without a slight edit, so here is my final code:

    if (ini_get('allow_url_fopen')) {
        $params = array('http' => array(
                'method' => 'POST',
                'content' => http_build_query($data),
                'header'=> "Content-type: application/x-www-form-urlencoded\r\n",
            ));
        $ctx = stream_context_create($params);
        $fp = fopen($url, 'rb', false, $ctx);
        if (!$fp) {
            print "FOPEN ERROR!";
            die();
        }
        fclose($fp);
    }
James
+1  A: 

The Zend_Http_Client implements four different ways of connecting to a target and can be easily extended (of course you don't need to use the complete framework for it ;).

Fge
+1  A: 

To answer at least part of your question, you should be able to detect whether curl is loaded with extension_loaded():

if (!extension_loaded('curl')) {
    # Yay curl!
}
pianohacker
Thanks! Exactly what I was looking for.
James
A: 

Snoopy is popular and fully functional library for making HTTP requests.

tszming