PHP cURL & Streams wrapper
Source Code: eac_streams.class
This work is licensed under the Creative Commons GNU-LGPL License.These classes (eac_curl.class and eac_streams.class) have been depreciated and superseded by eac_httprequest
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.
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.8 and streams.class v0.3.8 -
- copyHeaders() accepts a header name to copy a single header.
- copyCookies() accepts a cookie name to copy a single cookie.
- getInfo() accepts a field name to get a single value instead of the entire array.
- getHeaders() accepts a header name to get a single value instead of the entire array.
- getHeaders() accepts 'HTTP/1.x' as a header name matching 'HTTP/1.0 or HTTP/1.1' header.
- copyHeaders() ignores 'cookie', 'accept-encoding', 'host', and 'connection'.
- copyHeaders() only copies 'user-agent' and 'referer' if they have a value.
- Option names may begin with "CURLOPT_{option}", "STREAMS_{option}", OR "HTTP_{option}".
- Added *_REFERER option set to the curent url by default.
- With streams.class:
User authentication is no longer passed in the url. It is only passed if the request returns a 401 status and then it is passed in either a Basic or Digest authorization header depending on the WWW-Authenticate header.- When STREAMS_ASYNCRONOUS is set to true, an http request will return a stream resource that can be used to get the results later in the script. getLastResult() can be passed this resource to read and decode the result. Headers are now passed as a single string (not an array).
These classes (eac_curl.class and eac_streams.class) have been depreciated and superseded by eac_httprequest
<?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)
?>
These classes (eac_curl.class and eac_streams.class) have been depreciated and superseded by eac_httprequest
<?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)
?>