Piwik\Plugins\Marketplace\Api\Service::fetch PHP Method

fetch() public method

Make sure to call {@link authenticate()} to download paid plugins.
public fetch ( string $action, array $params ) : mixed
$action string eg 'plugins', 'plugins/$pluginName/info', ...
$params array eg array('sort' => 'alpha')
return mixed
    public function fetch($action, $params)
    {
        $endpoint = sprintf('%s/api/%s/', $this->domain, $this->version);
        $query = http_build_query($params);
        $url = sprintf('%s%s?%s', $endpoint, $action, $query);
        $response = $this->download($url);
        $result = json_decode($response, true);
        if (is_null($result)) {
            $message = sprintf('There was an error reading the response from the Marketplace: Please try again later.');
            throw new Service\Exception($message, Service\Exception::HTTP_ERROR);
        }
        if (!empty($result['error'])) {
            throw new Service\Exception($result['error'], Service\Exception::API_ERROR);
        }
        return $result;
    }

Usage Example

示例#1
0
文件: Client.php 项目: piwik/piwik
 private function fetch($action, $params)
 {
     ksort($params);
     // sort params so cache is reused more often even if param order is different
     $releaseChannel = $this->environment->getReleaseChannel();
     if (!empty($releaseChannel)) {
         $params['release_channel'] = $releaseChannel;
     }
     $params['prefer_stable'] = (int) $this->environment->doesPreferStable();
     $params['piwik'] = $this->environment->getPiwikVersion();
     $params['php'] = $this->environment->getPhpVersion();
     $params['mysql'] = $this->environment->getMySQLVersion();
     $params['num_users'] = $this->environment->getNumUsers();
     $params['num_websites'] = $this->environment->getNumWebsites();
     $query = http_build_query($params);
     $cacheId = $this->getCacheKey($action, $query);
     $result = $this->cache->fetch($cacheId);
     if ($result !== false) {
         return $result;
     }
     try {
         $result = $this->service->fetch($action, $params);
     } catch (Service\Exception $e) {
         throw new Exception($e->getMessage(), $e->getCode());
     }
     $this->cache->save($cacheId, $result, self::CACHE_TIMEOUT_IN_SECONDS);
     return $result;
 }
All Usage Examples Of Piwik\Plugins\Marketplace\Api\Service::fetch