Exemplo n.º 1
0
 protected function getAuthenticated()
 {
     $tokenEndpointUrl = $this->apiUrl . Transport::OAUTH_TOKEN_ENDPOINT_PATH;
     $authorizaitonEndpointUrl = $this->apiUrl . Transport::OAUTH_AUTHORIZATION_ENDPOINT_PATH;
     /**
      * Initialize the cURL session
      */
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $authorizaitonEndpointUrl);
     curl_setopt($ch, CURLOPT_POSTFIELDS, '');
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_HEADER, 1);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_VERBOSE, 1);
     $headers = array('Content-Type: application/x-www-form-urlencoded', 'Authorization: OAuth', 'Client-Id: ' . $this->clientId, 'Client-Secret: ' . $this->clientSecret, 'Grant-Type: client_credentials');
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     /**
      * Ask cURL to return the contents in a variable instead of simply echoing them to the browser.
      */
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     /**
      * Execute the cURL session
      */
     $response = curl_exec($ch);
     $httpResponseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     switch ($httpResponseCode) {
         case 500:
             $json = json_decode($response);
             curl_close($ch);
             require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
             $ehiveStatusMessage = new EHiveStatusMessage($json);
             throw new EHiveFatalServerException($ehiveStatusMessage->toString());
             break;
         case 404:
             throw new EHiveNotFoundException("Resource Not Found. Check that your request URL is valid.");
             break;
         case 403:
             $json = json_decode($response);
             curl_close($ch);
             require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
             $ehiveStatusMessage = new EHiveStatusMessage($json);
             throw new EHiveForbiddenException($ehiveStatusMessage->toString());
         case 401:
             $json = json_decode($response);
             curl_close($ch);
             require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
             $ehiveStatusMessage = new EHiveStatusMessage($json);
             throw new EHiveUnauthorizedException($ehiveStatusMessage->toString());
         case 303:
             $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
             $header = substr($response, 0, $header_size);
             $headersArray = explode("\n", $header);
             $headers = array();
             foreach ($headersArray as $header) {
                 $headerParts = explode(": ", $header);
                 $headers[$headerParts[0]] = $header;
             }
             curl_close($ch);
             $ch = curl_init();
             curl_setopt($ch, CURLOPT_URL, $tokenEndpointUrl);
             curl_setopt($ch, CURLOPT_HEADER, 0);
             curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", str_replace("\r", "", $headers["Access-Grant"]), str_replace("\r", "", $headers["Authorization"]), str_replace("\r", "", $headers["Client-Id"]), str_replace("\r", "", $headers["Grant-Type"])));
             /**
              * Ask cURL to return the contents in a variable instead of simply echoing them to the browser.
              */
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             /**
              * Execute the cURL session
              */
             $response = curl_exec($ch);
             $httpResponseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
             switch ($httpResponseCode) {
                 case 500:
                     $json = json_decode($response);
                     curl_close($ch);
                     require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
                     $ehiveStatusMessage = new EHiveStatusMessage($json);
                     throw new EHiveFatalServerException($ehiveStatusMessage->toString());
                     break;
                 case 404:
                     curl_close($ch);
                     throw new EHiveNotFoundException("Resource Not Found. Check that your request URL is valid.");
                     break;
                 case 403:
                     $json = json_decode($response);
                     curl_close($ch);
                     require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
                     $ehiveStatusMessage = new EHiveStatusMessage($json);
                     throw new EHiveForbiddenException($ehiveStatusMessage->toString());
                     break;
                 case 401:
                     $json = json_decode($response);
                     curl_close($ch);
                     require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
                     $ehiveStatusMessage = new EHiveStatusMessage($json);
                     throw new EHiveUnauthorizedException($ehiveStatusMessage->toString());
                     break;
                 case 200:
                     $json = json_decode($response);
                     curl_close($ch);
                     $apiAthorization = $this->asApiAuthorization($json);
                     $path = EHIVE_API_ROOT_DIR . Transport::OAUTH_TOKEN_FILE;
                     if (!($handle = fopen($path, 'w'))) {
                         throw new EHiveApiException('Cannot open "{$path}" file after retrieving an oauth token from the server-side API. Please ensure that the file has read and write permissions enabled. MESSGAE FROM SERVER: ' . $ehiveStatusMessage->errorMessage);
                     }
                     if (!fwrite($handle, $apiAthorization->oauthToken)) {
                         fclose($handle);
                         throw new EHiveApiException('Cannot write to the file "{$path}" after retrieving an oauth token from the server-side API. Please ensure that the file has write permissions enabled. MESSGAE FROM SERVER: ' . $ehiveStatusMessage->errorMessage);
                     }
                     fclose($handle);
                     return $apiAthorization;
                 default:
                     $json = json_decode($response);
                     curl_close($ch);
                     require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
                     $ehiveStatusMessage = new EHiveStatusMessage($json);
                     throw new EHiveApiException($ehiveStatusMessage->toString());
                     break;
             }
             return $json;
         default:
             $json = json_decode($response);
             curl_close($ch);
             require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
             $ehiveStatusMessage = new EHiveStatusMessage($json);
             throw new EHiveApiException('An invalid HTTP error code has been returned by the server-side API. This message has originated from the Transport.php file. This is likely to be due to an invalid URL to which the "GET" request has been sent. Please check that the URLs in the "/ehive/transport/oauth/endpoints.ini" file and the version of the client API you are using are up to date. These can be checked on http://forum.ehive.com. HTTP Response Code: ' . $httpResponseCode);
             break;
     }
 }
Exemplo n.º 2
0
 public function getMultiple($path, array $queries)
 {
     if (empty($path)) {
         return false;
     }
     /**
      * Initialize the cURL session
      */
     $ch = array();
     $mh = curl_multi_init();
     $responses = array();
     // This ensures that both the path and queries parameters exists as arrays in order to provide a form of alignment when iterating through the list of queries. It also allows for path and query pairing.
     if (is_array($path) && empty($queries)) {
         $queries = array_pad(array(), count($path), '');
     } elseif (!is_array($path) && !empty($queries)) {
         $path = array_pad(array(), count($queries), $path);
     }
     $apiAuthorization = $this->getApiAuthorization();
     foreach ($queries as $k => $query) {
         $url = $this->apiUrl . $path[$k];
         $ch[$k] = curl_init();
         $query = $this->createUrl($url, str_replace(" ", "%20", $query));
         curl_setopt($ch[$k], CURLOPT_URL, $query);
         curl_setopt($ch[$k], CURLOPT_HEADER, 0);
         $headers = array('Content-Type: application/json', 'Authorization: Basic ' . $apiAuthorization->oauthToken, 'Client-Id: ' . $apiAuthorization->clientId, 'Grant-Type: ' . $apiAuthorization->grantType);
         curl_setopt($ch[$k], CURLOPT_HTTPHEADER, $headers);
         /**
          * Ask cURL to return the contents in a variable instead of simply echoing them to the browser.
          */
         curl_setopt($ch[$k], CURLOPT_RETURNTRANSFER, 1);
         curl_multi_add_handle($mh, $ch[$k]);
     }
     // Initialise a variable to store a boolean status for activity for the current cURL request
     $active = NULL;
     do {
         $result = curl_multi_exec($mh, $active);
     } while ($result == CURLM_CALL_MULTI_PERFORM);
     while ($active && $result == CURLM_OK) {
         // The curl_multi_select is tempremental, and it's purpose is to wait until the request is complete for a child handle, however delaying program execution is a viable and recommended solution.
         // if(curl_multi_select($mh) != -1){
         usleep(10000);
         do {
             $mrc = curl_multi_exec($mh, $active);
         } while ($mrc == CURLM_CALL_MULTI_PERFORM);
         // }
     }
     foreach ($ch as $k => $v) {
         $httpResponseCode = curl_getinfo($v, CURLINFO_HTTP_CODE);
         $response = curl_multi_getcontent($v);
         switch ($httpResponseCode) {
             case 500:
                 $json = json_decode($response);
                 require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
                 $ehiveStatusMessage = new EHiveStatusMessage($json);
                 throw new EHiveFatalServerException($ehiveStatusMessage->toString());
                 break;
             case 404:
                 $json = json_decode($response);
                 require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
                 $ehiveStatusMessage = new EHiveStatusMessage($json);
                 throw new EHiveNotFoundException($ehiveStatusMessage->toString());
                 break;
             case 403:
                 $json = json_decode($response);
                 require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
                 $ehiveStatusMessage = new EHiveStatusMessage($json);
                 throw new EHiveForbiddenException($ehiveStatusMessage->toString());
                 break;
             case 401:
                 if ($this->retryAttempts < 3) {
                     $apiAuthorization = $this->getAuthenticated();
                     if (is_null($apiAuthorization->oauthToken)) {
                         throw new EHiveApiException('OAuth Token is missing after the server said it has vended it. This is a fatal error and should be reported at http://forum.ehive.com.');
                     }
                     $this->retryAttempts = $this->retryAttempts + 1;
                     $json = $this->get($path, $queries[$k]);
                 } else {
                     $json = json_decode($response);
                     require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
                     $ehiveStatusMessage = new EHiveStatusMessage($json);
                     throw new EHiveUnauthorizedException($ehiveStatusMessage->toString());
                 }
                 break;
             case 200:
                 $json = json_decode($response);
                 break;
             default:
                 $json = json_decode($response);
                 require_once EHIVE_API_ROOT_DIR . '/exceptions/EHiveStatusMessage.php';
                 $ehiveStatusMessage = new EHiveStatusMessage($json);
                 throw new EHiveApiException($ehiveStatusMessage->toString());
                 break;
         }
         $responses[$k] = $json;
         curl_multi_remove_handle($mh, $v);
     }
     curl_multi_close($mh);
     return $responses;
 }