Example #1
0
 /**
  * request character data from CCP API
  * @param int $characterId
  * @return array
  */
 public function getPublicCharacterData($characterId)
 {
     $characterData = [];
     $apiPath = self::getEnvironmentData('CCP_XML') . '/eve/CharacterInfo.xml.aspx';
     $baseOptions = $this->getRequestOptions();
     $requestOptions = ['method' => 'GET', 'content' => ['characterID' => (int) $characterId]];
     $requestOptions = array_merge($baseOptions, $requestOptions);
     $apiResponse = Lib\Web::instance()->request($apiPath, $requestOptions);
     if ($apiResponse['body'] && ($xml = simplexml_load_string($apiResponse['body']))) {
         if (isset($xml->result) && is_object($rowApiData = $xml->result->children())) {
             foreach ($rowApiData as $item) {
                 // map attributes to array
                 if (count($item->children()) == 0) {
                     $characterData[$item->getName()] = strval($item);
                 }
             }
         }
     }
     $data = (new Mapper\CcpCharacterMapper($characterData))->getData();
     return $data;
 }
Example #2
0
 /**
  * perform curl() request
  * -> caches response by returned HTTP Cache header data
  * @param string $url
  * @param array|null $options
  * @param array $additionalOptions
  * @param int $retryCount request counter for failed crest call
  * @return array|FALSE|mixed
  */
 public function request($url, array $options = null, $additionalOptions = [], $retryCount = 0)
 {
     $f3 = \Base::instance();
     if (!$f3->exists($hash = $this->getCacheKey($url, $options))) {
         // retry same request until request limit is reached
         $retry = false;
         $result = parent::request($url, $options);
         $result['timeout'] = false;
         $statusCode = $this->getStatusCodeFromHeaders($result['headers']);
         switch ($statusCode) {
             case 100:
             case 200:
                 // request succeeded -> check if response should be cached
                 $ttl = $this->getCacheTimeFromHeaders($result['headers']);
                 if ($ttl > 0 && !empty(json_decode($result['body'], true))) {
                     $f3->set($hash, $result, $ttl);
                 }
                 break;
             case 401:
             case 415:
                 // unauthorized
                 $errorMsg = $this->getErrorMessageFromJsonResponse($statusCode, $options['method'], $url, json_decode($result['body']));
                 LogController::getLogger('ERROR')->write($errorMsg);
                 break;
             case 500:
             case 501:
             case 502:
             case 503:
             case 505:
                 $retry = true;
                 if ($retryCount == self::RETRY_COUNT_MAX) {
                     $errorMsg = $this->getErrorMessageFromJsonResponse($statusCode, $options['method'], $url, json_decode($result['body']));
                     LogController::getLogger('ERROR')->write($errorMsg);
                     // trigger error
                     if ($additionalOptions['suppressHTTPErrors'] !== true) {
                         $f3->error($statusCode, $errorMsg);
                     }
                 }
                 break;
             case 504:
             case 0:
                 $retry = true;
                 if ($retryCount == self::RETRY_COUNT_MAX) {
                     // timeout -> response should not be cached
                     $result['timeout'] = true;
                     $errorMsg = $this->getErrorMessageFromJsonResponse(504, $options['method'], $url, json_decode($result['body']));
                     // log error
                     LogController::getLogger('ERROR')->write($errorMsg);
                     if ($additionalOptions['suppressHTTPErrors'] !== true) {
                         $f3->error(504, $errorMsg);
                     }
                 }
                 break;
             default:
                 // unknown status
                 $errorMsg = $this->getErrorMessageFromJsonResponse($statusCode, $options['method'], $url);
                 LogController::getLogger('ERROR')->write($errorMsg);
                 break;
         }
         if ($retry && $retryCount < self::RETRY_COUNT_MAX) {
             $retryCount++;
             $this->request($url, $options, $additionalOptions, $retryCount);
         }
     } else {
         $result = $f3->get($hash);
     }
     return $result;
 }
Example #3
0
 /**
  * set new ingame waypoint
  * @param Model\CharacterModel $character
  * @param int $systemId
  * @param array $options
  * @return array
  */
 public function setWaypoint(Model\CharacterModel $character, $systemId, $options = [])
 {
     $crestUrlParts = parse_url(self::getCrestEndpoint());
     $waypointData = [];
     if ($crestUrlParts) {
         $accessToken = $character->getAccessToken();
         $endpoints = $this->getEndpoints($accessToken);
         // get endpoint list for "ui" endpoints
         $uiEndpoints = $endpoint = $this->walkEndpoint($endpoints, $accessToken, ['decode', 'character', 'ui']);
         if (isset($uiEndpoints['setWaypoints']) && isset($uiEndpoints['setWaypoints']['href'])) {
             $endpointUrl = $uiEndpoints['setWaypoints']['href'];
             $systemEndpoint = self::getCrestEndpoint() . '/solarsystems/' . $systemId . '/';
             // request body
             $content = ['clearOtherWaypoints' => (bool) $options['clearOtherWaypoints'], 'first' => (bool) $options['first'], 'solarSystem' => ['href' => $systemEndpoint, 'id' => (int) $systemId]];
             $requestOptions = ['timeout' => self::CREST_TIMEOUT, 'method' => 'POST', 'user_agent' => $this->getUserAgent(), 'header' => ['Scope: characterNavigationWrite', 'Authorization: Bearer ' . $character->getAccessToken(), 'Host: ' . $crestUrlParts['host'], 'Content-Type: application/vnd.ccp.eve.PostWaypoint-v1+json;charset=utf-8'], 'content' => json_encode($content, JSON_UNESCAPED_SLASHES)];
             $apiResponse = Lib\Web::instance()->request($endpointUrl, $requestOptions);
             if (isset($apiResponse['body'])) {
                 $responseData = json_decode($apiResponse['body']);
                 if (empty($responseData)) {
                     $waypointData['systemId'] = (int) $systemId;
                 } elseif (isset($responseData->message) && isset($responseData->key)) {
                     // waypoint could not be set...
                     $error = (object) [];
                     $error->type = 'error';
                     $error->message = $responseData->key;
                     $waypointData['error'] = $error;
                 }
             }
         }
     }
     return $waypointData;
 }