diff options
author | Christian A. Weber | 2018-08-17 16:53:26 +0200 |
---|---|---|
committer | Christian A. Weber | 2018-08-17 16:53:26 +0200 |
commit | 6df69a1c2ce720a9a7e8c92fcd3665c2b8c86a3d (patch) | |
tree | 62e7f73bb4d3333001aaa52b1cd25b26ee58d4fa | |
parent | 3db4daffff1cbb6d2262a1caa873569b0d0a315a (diff) | |
download | itools-6df69a1c2ce720a9a7e8c92fcd3665c2b8c86a3d.tar.gz itools-6df69a1c2ce720a9a7e8c92fcd3665c2b8c86a3d.tar.bz2 itools-6df69a1c2ce720a9a7e8c92fcd3665c2b8c86a3d.zip |
new $p['writefunction'] function to be called whenever data is received (for server-sent-events etc.)
-rw-r--r-- | it_url.class | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/it_url.class b/it_url.class index 8542fff..6b2593c 100644 --- a/it_url.class +++ b/it_url.class @@ -92,19 +92,20 @@ function is_reachable($timeout = 5) * If the protocol is not http, only features of get_multi are supported. * * @param $p parameter array with the following keys - * @param $p['url'] url to get, defaults to constructor URL - * @param $p['headers'] optional associative array of HTTP headers to send - * @param $p['safety'] DEPRECATED. 0 = ignore errors, 1 = errors, 2 = fatals - * @param $p['it_error'] extra arguments for it_error or false to ignore errors - * @param $p['timeout'] timeout per read in seconds, defaults to 5. fractions allowed. silent, see $p['safety'] - * @param $p['totaltimeout'] timeout for the whole function call - * @param $p['maxlength'] maximum length of response - * @param $p['filemtime'] Add HTTP header to only fetch when newer than this, otherwise return true instead of data - * @param $p['data'] POST data array with key-value pairs - * @param $p['files'] [fieldname => filename] of files to upload - * @param $p['retries'] Number of retries if download fails, default 1 - * @param $p['retrysleep'] Number of seconds to wait before retry, fractions ok - * @param $p['compression'] use compression (uses curl to do that) + * @param $p['url'] url to get, defaults to constructor URL + * @param $p['headers'] optional associative array of HTTP headers to send + * @param $p['safety'] DEPRECATED. 0 = ignore errors, 1 = errors, 2 = fatals + * @param $p['it_error'] extra arguments for it_error or false to ignore errors + * @param $p['timeout'] timeout per read in seconds, defaults to 5. fractions allowed. silent, see $p['safety'] + * @param $p['totaltimeout'] timeout for the whole function call + * @param $p['maxlength'] maximum length of response + * @param $p['filemtime'] Add HTTP header to only fetch when newer than this, otherwise return true instead of data + * @param $p['data'] POST data array with key-value pairs + * @param $p['files'] [fieldname => filename] of files to upload + * @param $p['writefunction'] function to be called whenever data is received (for server-sent-events etc.) + * @param $p['retries'] Number of retries if download fails, default 1 + * @param $p['retrysleep'] Number of seconds to wait before retry, fractions ok + * @param $p['compression'] use compression (uses curl to do that) * @return contents of resulting page, considering redirects, excluding headers, or false on error */ function get($p=null, $timeout=5) @@ -198,6 +199,13 @@ static function curl_opts($p=array()) if ($p['pass'] || $p['user']) $add += [ CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => $p['user'] . ':' . $p['pass'] ]; + if ($p['writefunction']) { + $add += [ + CURLOPT_RETURNTRANSFER => false, + CURLOPT_WRITEFUNCTION => $p['writefunction'], + ]; + } + if ($p['verbose'] || EDC('curlverbose')) $add += [ CURLOPT_VERBOSE => true ]; @@ -237,7 +245,7 @@ function request($p=array()) $url->headers = array(); $p['headers'] = (array)$p['headers'] + self::_default_headers($url, $p); - $opts = array(CURLOPT_HEADER => 1) + self::curl_opts($p + array('user' => $this->user, 'pass' => $this->pass, 'followlocation' => false)); + $opts = array(CURLOPT_HEADER => !$p['writefunction']) + self::curl_opts($p + array('user' => $this->user, 'pass' => $this->pass, 'followlocation' => false)); if ($p['verbose']) { $stderr = it::fopen("php://memory", "r+"); $opts += [CURLOPT_STDERR => $stderr, CURLOPT_VERBOSE => 1]; |