Getting Work Done Through People
Getting People Done Through Work
PHP cURL & Streams wrapper
Source Code: eac_streams.class
This work is licensed under the Creative Commons GNU-LGPL License.eac_curl.class.php and eac_streams.class.php are wrapper classes for using curl or streams to make http GET, POST, or PUT requests.
New in curl.class v0.7.4 and streams.class v0.3.4 -
HEAD, DELETE, OPTIONS, and TRACE requests
eac_curl.class.php accepts any of the curl_setopt options - although not all are applicable to this class.
eac_streams.class.php is intended to be a plug-in replacement for eac_curl.class.php that can be used when the curl library is not configured in the PHP implementation. It accepts a subset of the curl_setopt options and will ignore curl options that are not applicable.
One advantage that eac_streams.class.php has over eac_curl.class.php is the "STREAMS_ASYNCRONOUS" option. When set to 1 or true, the streams process will not wait for the response from the http request and thus execute the request asyncronously.
Either class will recognize option names with "CURL_" or "STREAMS_" prefix.
Both classes support, recognize, and decode gzip encoding (compression) reducing the content length of the response by up to 80%.
Common Methods:
setOptions() - set a array of options.
setOption() - set a single option.
setCallback() - set a callback function.
copyHeaders() - copy the http request headers to send with this request.
header() - set an http header.
copyCookies() * - copy the http request cookies to send with this request. *
head() * - send an http HEAD request.
get() - send an http GET request.
post() - send an http POST request.
put() - send an http PUT request.
delete() * - send an http DELETE request.
options() * - send an http OPTIONS request.
trace() *- send an http TRACE request.
httpRequest() - send an http request (used by get(), post(), and put()).
sendLastResult() - sends the result of the last request via email.
getLastResult() - returns the result of the last request.
getInfo() - returns a status array of the last request (i.e. curl_getinfo()).
getHeaders() - returns the response headers of the last request.
getOptions() - returns the current options array.
* New in curl.class v0.7.4 and streams.class v0.3.4
<?PHP
/* ------------------------------------------------------------------------ */
// a simple GET request:
include('eac_curl.class.php');
$http = new cURL();
$result = $http->get('http://www.kevinburkholder.com/PostTest.php');
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
// set some fields
$fields = array();
$fields['FirstName'] = 'Kevin';
$fields['LastName'] = 'Burkholder';
$fields['Email'] = 'kburkholder[at]earthasylum.com';
// set some options - see http://us3.php.net/manual/en/function.curl-setopt.php
$options = array();
$options['CURLOPT_AUTOREFERER'] = 1;
$options['CURLOPT_CRLF'] = 1;
$options['CURLOPT_NOPROGRESS'] = 1;
include('eac_curl.class.php');
// instantiate and load options
$http = new cURL($options);
// another way to load options
$http->setOptions($options);
// add a new (single) option
$http->setOption('CURLOPT_BUFFERSIZE', 32768);
// copy the http request headers to the curl request
$http->copyHeaders();
// add a header
$http->header('X-EAC-CURL-Test: cURL test header');
// copy $_COOKIE to the curl request (new in curl.class v0.7.3)
$http->copyCookies();
// post to PostTest.php with fields
echo $http->post('http://www.kevinburkholder.com/PostTest.php',$fields);
// display the response headers, the cURL stats, and our options
echo '<pre>';
print_r($http->getHeaders());
print_r($http->getInfo());
print_r($http->getOptions());
echo '</pre>';
/* ------------------------------------------------------------------------ */
// $http->success = false on error, else true
// $http->error = error message (on error)
?>
<?PHP
/* ------------------------------------------------------------------------ */
// a simple GET request:
include('eac_streams.class.php');
$http = new stream();
$result = $http->get('http://www.kevinburkholder.com/PostTest.php');
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
// set some fields
$fields = array();
$fields['FirstName'] = 'Kevin';
$fields['LastName'] = 'Burkholder';
$fields['Email'] = 'kburkholder[at]earthasylum.com';
// set some options
$options = array();
$options['STREAMS_MAXREDIRS'] = 5;
$options['STREAMS_TIMEOUT'] = 60;
$options['STREAMS_ENCODING'] = null;
include('eac_streams.class.php');
// instantiate and load options
$http = new stream($options);
// another way to load options
$http->setOptions($options);
// add a new (single) option
$http->setOption('STREAMS_USERPWD', 'user:password');
// copy the http request headers to the stream request
$http->copyHeaders();
// add my own header
$http->header('X-EAC-STREAM-Test: stream test header');
// copy $_COOKIE to the streams request (new is streams.class v0.3.3)
$http->copyCookies();
// post to PostTest.php with fields
echo $http->post('http://www.kevinburkholder.com/PostTest.php',$fields);
// display the response headers, the stream stats, and our options
echo '<pre>';
print_r($http->getHeaders());
print_r($http->getInfo());
print_r($http->getOptions());
echo '</pre>';
/* ------------------------------------------------------------------------ */
// $http->success = false on error, else true
// $http->error = error message (on error)
?>