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) |
public function sendSMS($apiKey, $smsText, $phoneNumber, $from) { $account = explode(" ", $apiKey); $parameters = array('user' => $account[0], 'pass' => $account[1], 'msg' => $smsText); $url = self::API_URL . '?' . http_build_query($parameters, '', '&'); $timeout = self::SOCKET_TIMEOUT; $result = Http::sendHttpRequestBy(Http::getTransportMethod(), $url, $timeout, $getExtendedInfo = true); }
/** * @group Core * * @dataProvider getMethodsToTest */ public function testHEADOperation($method) { if ($method == 'fopen') { return; // not supported w/ this method } $result = Http::sendHttpRequestBy($method, 'http://builds.piwik.org/latest.zip', 30, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $acceptInvalidSslCertificate = false, $byteRange = false, $getExtendedInfo = true, $httpMethod = 'HEAD'); $this->assertEquals('', $result['data']); $this->assertEquals(200, $result['status']); $this->assertTrue(isset($result['headers']['Content-Length']), "Content-Length header not set!"); $this->assertTrue(is_numeric($result['headers']['Content-Length']), "Content-Length header not numeric!"); $this->assertEquals('application/zip', $result['headers']['Content-Type']); }
private function issueApiCall($apiKey, $resource, $additionalParameters = array()) { $accountParameters = array('Key' => $apiKey); $parameters = array_merge($accountParameters, $additionalParameters); $url = self::BASE_API_URL . $resource . '?' . http_build_query($parameters, '', '&'); $timeout = self::SOCKET_TIMEOUT; try { $result = Http::sendHttpRequestBy(Http::getTransportMethod(), $url, $timeout, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $acceptInvalidSslCertificate = true); } catch (Exception $e) { $result = self::ERROR_STRING . " " . $e->getMessage(); } if (strpos($result, self::ERROR_STRING) !== false) { throw new APIException('Clockwork API returned the following error message : ' . $result); } return $result; }
/** * Returns true if the Piwik server appears to be working. * * If the Piwik server is in an error state (eg. some directories are not writable and Piwik displays error message), * or if the Piwik server is "offline", * this will return false.. * * @param $piwikServerUrl * @param bool $acceptInvalidSSLCertificates * @throws Exception * @return bool */ public static function checkPiwikServerWorking($piwikServerUrl, $acceptInvalidSSLCertificates = false) { // Now testing if the webserver is running try { $fetched = Http::sendHttpRequestBy('curl', $piwikServerUrl, $timeout = 45, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $acceptInvalidSSLCertificates); } catch (Exception $e) { $fetched = "ERROR fetching: " . $e->getMessage(); } // this will match when Piwik not installed yet, or favicon not customised $expectedStringAlt = 'plugins/CoreHome/images/favicon.ico'; // this will match when Piwik is installed and favicon has been customised $expectedString = 'misc/user/'; // see checkPiwikIsNotInstalled() $expectedStringAlreadyInstalled = 'piwik-is-already-installed'; $expectedStringNotFound = strpos($fetched, $expectedString) === false && strpos($fetched, $expectedStringAlt) === false && strpos($fetched, $expectedStringAlreadyInstalled) === false; $hasError = false !== strpos($fetched, PAGE_TITLE_WHEN_ERROR); if ($hasError || $expectedStringNotFound) { throw new Exception("\nPiwik should be running at: " . $piwikServerUrl . " but this URL returned an unexpected response: '" . $fetched . "'\n\n"); } }
private function executeNotAsyncHttp($url, Output $output) { try { Log::debug("Execute HTTP API request: " . $url); $response = Http::sendHttpRequestBy('curl', $url, $timeout = 0, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $this->acceptInvalidSSLCertificate); $output->write($response); } catch (\Exception $e) { $message = "Got invalid response from API request: {$url}. "; if (isset($response) && empty($response)) { $message .= "The response was empty. This usually means a server error. This solution to this error is generally to increase the value of 'memory_limit' in your php.ini file. Please check your Web server Error Log file for more details."; } else { $message .= "Response was '" . $e->getMessage() . "'"; } $output->write($message); Log::debug($e); } }
/** * We check that HTTPS is not supported with the "socket" method */ public function testSocketHttpsWorksEvenWithInvalidCertificate() { $result = Http::sendHttpRequestBy('socket', 'https://divezone.net', 10); $this->assertNotEmpty($result); }
/** * Returns true if the Piwik server appears to be working. * * @param $piwikServerUrl * @return bool */ public static function checkPiwikServerWorking($piwikServerUrl) { // Now testing if the webserver is running try { $fetched = Http::sendHttpRequestBy('curl', $piwikServerUrl, $timeout = 45, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $acceptInvalidSslCertificate = true); } catch (Exception $e) { $fetched = "ERROR fetching: " . $e->getMessage(); } $expectedString = 'plugins/CoreHome/images/favicon.ico'; if (strpos($fetched, $expectedString) === false) { throw new Exception("\nPiwik should be running at: " . $piwikServerUrl . " but this URL returned an unexpected response: '" . $fetched . "'\n\n"); } }
private function executeNotAsyncHttp($url, Output $output) { $piwikUrl = $this->urlToPiwik ?: SettingsPiwik::getPiwikUrl(); if (empty($piwikUrl)) { $piwikUrl = 'http://' . Url::getHost() . '/'; } $url = $piwikUrl . $url; if (Config::getInstance()->General['force_ssl'] == 1) { $url = str_replace("http://", "https://", $url); } if ($this->runAsSuperUser) { $tokenAuths = self::getSuperUserTokenAuths(); $tokenAuth = reset($tokenAuths); if (strpos($url, '?') === false) { $url .= '?'; } else { $url .= '&'; } $url .= 'token_auth=' . $tokenAuth; } try { Log::debug("Execute HTTP API request: " . $url); $response = Http::sendHttpRequestBy('curl', $url, $timeout = 0, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $this->acceptInvalidSSLCertificate); $output->write($response); } catch (\Exception $e) { $message = "Got invalid response from API request: {$url}. "; if (isset($response) && empty($response)) { $message .= "The response was empty. This usually means a server error. This solution to this error is generally to increase the value of 'memory_limit' in your php.ini file. Please check your Web server Error Log file for more details."; } else { $message .= "Response was '" . $e->getMessage() . "'"; } $output->write($message); Log::debug($e); } }
/** * Issues a request to $url */ private function request($url) { $url = $this->piwikUrl . $url . self::APPEND_TO_API_REQUEST; if ($this->shouldStartProfiler) { $url .= "&xhprof=2"; } //$this->log($url); try { $response = Http::sendHttpRequestBy('curl', $url, $timeout = 300, $userAgent = null, $destinationPath = null, $file = null, $followDepth = 0, $acceptLanguage = false, $acceptInvalidSSLCertificate = $this->acceptInvalidSSLCertificate); } catch (Exception $e) { return $this->logNetworkError($url, $e->getMessage()); } if ($this->checkResponse($response, $url)) { return $response; } return false; }
/** * Downloads data from the given URL via a POST request. If a destination path is given, the downloaded data * will be stored in the given path and returned otherwise. * * Make sure to call {@link authenticate()} to download paid plugins. * * @param string $url An absolute URL to the marketplace including domain. * @param null|string $destinationPath * @param null|int $timeout Defaults to 60 seconds see {@link self::HTTP_REQUEST_METHOD} * @return bool|string Returns the downloaded data or true if a destination path was given. * @throws \Exception */ public function download($url, $destinationPath = null, $timeout = null) { $method = Http::getTransportMethod(); if (!isset($timeout)) { $timeout = static::HTTP_REQUEST_TIMEOUT; } $post = null; if ($this->accessToken) { $post = array('access_token' => $this->accessToken); } $file = Http::ensureDestinationDirectoryExists($destinationPath); $response = Http::sendHttpRequestBy($method, $url, $timeout, $userAgent = null, $destinationPath, $file, $followDepth = 0, $acceptLanguage = false, $acceptInvalidSslCertificate = false, $byteRange = false, $getExtendedInfo = false, $httpMethod = 'POST', $httpUsername = null, $httpPassword = null, $post); return $response; }