PHP Class Piwik\Http

Used to check for the latest Piwik version and download updates.
Mostrar archivo Open project: piwik/piwik Class Usage Examples

Public Methods

Method Description
configCurlCertificate ( &$ch ) Will configure CURL handle $ch to use local list of Certificate Authorities,
downloadChunk ( string $url, string $outputPath, boolean $isContinuation ) : array Downloads the next chunk of a specific file. The next chunk's byte range is determined by the existing file's size and the expected file size, which is stored in the piwik_option table before starting a download. The expected file size is obtained through a HEAD HTTP request.
ensureDestinationDirectoryExists ( $destinationPath )
fetchRemoteFile ( string $url, string $destinationPath = null, integer $tries, integer $timeout = 10 ) : boolean Fetches a file located at $url and saves it to $destinationPath.
getModifiedSinceHeader ( ) : string Returns the If-Modified-Since HTTP header if it can be found. If it cannot be found, an empty string is returned.
getTransportMethod ( ) : string | null Returns the "best" available transport method for {@link sendHttpRequest()} calls.
getUserAgent ( )
sendHttpRequest ( string $aUrl, integer $timeout, string | null $userAgent = null, string | null $destinationPath = null, integer | null $followDepth, boolean $acceptLanguage = false, array | boolean $byteRange = false, boolean $getExtendedInfo = false, string $httpMethod = 'GET', string $httpUsername = null, string $httpPassword = null ) : boolean | string Sends an HTTP request using best available transport method.
sendHttpRequestBy ( string $method = 'socket', string $aUrl, integer $timeout, string $userAgent = null, string $destinationPath = null, resource $file = null, integer $followDepth, boolean | string $acceptLanguage = false, boolean $acceptInvalidSslCertificate = false, array | boolean $byteRange = false, boolean $getExtendedInfo = false, string $httpMethod = 'GET', string $httpUsername = null, string $httpPassword = null, array | string $requestBody = null ) : boolean Sends an HTTP request using the specified transport method.

Protected Methods

Method Description
isCurlEnabled ( )
isSocketEnabled ( )

Private Methods

Method Description
buildHeadersForPost ( $requestBody )
getProxyConfiguration ( string $url ) : array Returns Proxy to use for connecting via HTTP to given URL
parseHeaderLine ( array &$headers, string $line ) Utility function, parses an HTTP header line into key/value & sets header array with them.
truncateStr ( string $str, integer $limit ) : string Utility function that truncates a string to an arbitrary limit.

Method Details

configCurlCertificate() public static method

Will configure CURL handle $ch to use local list of Certificate Authorities,
public static configCurlCertificate ( &$ch )

downloadChunk() public static method

_Note: this function uses the **Range** HTTP header to accomplish downloading in parts. Not every server supports this header._ The proper use of this function is to call it once per request. The browser should continue to send requests to Piwik which will in turn call this method until the file has completely downloaded. In this way, the user can be informed of a download's progress. **Example Usage** browser JavaScript var downloadFile = function (isStart) { var ajax = new ajaxHelper(); ajax.addParams({ module: 'MyPlugin', action: 'myAction', isStart: isStart ? 1 : 0 }, 'post'); ajax.setCallback(function (response) { var progress = response.progress ...update progress... downloadFile(false); }); ajax.send(); } downloadFile(true); PHP controller action public function myAction() { $outputPath = PIWIK_INCLUDE_PATH . '/tmp/averybigfile.zip'; $isStart = Common::getRequestVar('isStart', 1, 'int'); Http::downloadChunk("http://bigfiles.com/averybigfile.zip", $outputPath, $isStart == 1); }
public static downloadChunk ( string $url, string $outputPath, boolean $isContinuation ) : array
$url string The url to download from.
$outputPath string The path to the file to save/append to.
$isContinuation boolean `true` if this is the continuation of a download, or if we're starting a fresh one.
return array

ensureDestinationDirectoryExists() public static method

public static ensureDestinationDirectoryExists ( $destinationPath )

fetchRemoteFile() public static method

Fetches a file located at $url and saves it to $destinationPath.
public static fetchRemoteFile ( string $url, string $destinationPath = null, integer $tries, integer $timeout = 10 ) : boolean
$url string The URL of the file to download.
$destinationPath string The path to download the file to.
$tries integer (deprecated)
$timeout integer The amount of seconds to wait before aborting the HTTP request.
return boolean `true` on success, throws Exception on failure

getModifiedSinceHeader() public static method

Returns the If-Modified-Since HTTP header if it can be found. If it cannot be found, an empty string is returned.
public static getModifiedSinceHeader ( ) : string
return string

getTransportMethod() public static method

Returns the "best" available transport method for {@link sendHttpRequest()} calls.
public static getTransportMethod ( ) : string | null
return string | null Either curl, fopen, socket or null if no method is supported.

getUserAgent() public static method

public static getUserAgent ( )

isCurlEnabled() protected static method

protected static isCurlEnabled ( )

isSocketEnabled() protected static method

protected static isSocketEnabled ( )

sendHttpRequest() public static method

Sends an HTTP request using best available transport method.
public static sendHttpRequest ( string $aUrl, integer $timeout, string | null $userAgent = null, string | null $destinationPath = null, integer | null $followDepth, boolean $acceptLanguage = false, array | boolean $byteRange = false, boolean $getExtendedInfo = false, string $httpMethod = 'GET', string $httpUsername = null, string $httpPassword = null ) : boolean | string
$aUrl string The target URL.
$timeout integer The number of seconds to wait before aborting the HTTP request.
$userAgent string | null The user agent to use.
$destinationPath string | null If supplied, the HTTP response will be saved to the file specified by this path.
$followDepth integer | null Internal redirect count. Should always pass `null` for this parameter.
$acceptLanguage boolean The value to use for the `'Accept-Language'` HTTP request header.
$byteRange array | boolean For `Range:` header. Should be two element array of bytes, eg, `array(0, 1024)` Doesn't work w/ `fopen` transport method.
$getExtendedInfo boolean If true returns the status code, headers & response, if false just the response.
$httpMethod string The HTTP method to use. Defaults to `'GET'`.
$httpUsername string HTTP Auth username
$httpPassword string HTTP Auth password
return boolean | string If `$destinationPath` is not specified the HTTP response is returned on success. `false` is returned on failure. If `$getExtendedInfo` is `true` and `$destinationPath` is not specified an array with the following information is returned on success: - **status**: the HTTP status code - **headers**: the HTTP headers - **data**: the HTTP response data `false` is still returned on failure.

sendHttpRequestBy() public static method

Sends an HTTP request using the specified transport method.
public static sendHttpRequestBy ( string $method = 'socket', string $aUrl, integer $timeout, string $userAgent = null, string $destinationPath = null, resource $file = null, integer $followDepth, boolean | string $acceptLanguage = false, boolean $acceptInvalidSslCertificate = false, array | boolean $byteRange = false, boolean $getExtendedInfo = false, string $httpMethod = 'GET', string $httpUsername = null, string $httpPassword = null, array | string $requestBody = null ) : boolean
$method string
$aUrl string
$timeout integer in seconds
$userAgent string
$destinationPath string
$file resource
$followDepth integer
$acceptLanguage boolean | string Accept-language header
$acceptInvalidSslCertificate boolean Only used with $method == 'curl'. If set to true (NOT recommended!) the SSL certificate will not be checked
$byteRange array | boolean For Range: header. Should be two element array of bytes, eg, array(0, 1024) Doesn't work w/ fopen method.
$getExtendedInfo boolean True to return status code, headers & response, false if just response.
$httpMethod string The HTTP method to use. Defaults to `'GET'`.
$httpUsername string HTTP Auth username
$httpPassword string HTTP Auth password
$requestBody array | string If $httpMethod is 'POST' this may accept an array of variables or a string that needs to be posted
return boolean true (or string/array) on success; false on HTTP response error code (1xx or 4xx)