如何使用Ikoula的API

来自Chinese Ikoula Wiki
Lfourrea5593讨论 | 贡献2021年2月15日 (一) 16:22的版本 (创建页面,内容为“<span data-link_translate_en_title="How to use the Ikoula API" data-link_translate_en_url="How+to+use+the+Ikoula+API"></span>en:How to use the Ikoula APIen:H…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
Jump to navigation Jump to search

en:How to use the Ikoula API fr:Comment utiliser l'API d’Ikoula es:Cómo utilizar la API de Ikoula nl:Hoe de Ikoula API te gebruiken? it:Come utilizzare l'API Ikoula


Introduction

Ikoula的API产品,可让您操作及管理您帐户中的产品。 这是API URL https://api.ikoula.com

我们每个产品都有提供文档。

说明

由于安全考量,Ikoula 的API需要使用用户名,密码和签名进行身份验证。

  • 用户名是用于连接到您的Ikoula帐户或Extranet的电子邮件地址,也是您 login的传递参数名称;
  • 必须使用Ikoula提供的公钥 crypted_password 参数) 和base64_encode通过特定功能对密码进行加密
  • 根据API提供的参数生成签名。
  • 必须始终在GET中将这些参数传递给API!


注意

例如,对于您的API测试,您可以使用测试的临时用户。 在任何生产环境或非短期环境中,使用带有Ikoula公钥的密码加密都是不可少的。 如果要通过脚本或程序使用API调用,建议您为此目的创建一个专用用户,而不要使用传统的Extranet用户。 您有两种选择:

  • 直接在Extranet帐户的主页上创建子用户(请参阅下面的WIKI,以创建子用户: 如何创建一个子账户?).
  • 如有必要,请联系我们创建Extranet用户

请注意提供使用者所需服务的权利。

加密金钥

加密公钥的密码位于: https://api.ikoula.com/downloads/Ikoula.API.RSAKeyPub.pem

Wrapper PHP

<?php 

// #################################################
// ####    ..:: Ikoula Hosting Services ::..     ###
// ####	   Wrapper for https://api.ikoula.com	 ###
// #################################################

class IkoulaAPI {

   /**
    * Email of Ikoula account
    * @var string Email account
    */
    private static $email = "EMAIL_ACCOUNT_IKOULA";

   /**
    * Password of Ikoula account
    * @var string Password account
    */
    private static $password = "PASSWORD_ACCOUNT_IKOULA";

   /**
    * Ikoula API URI
    * @var string Password account
    */ 
    private static $urlApi = "https://api.ikoula.com/";

   /** Public key path for encrypt data
    * @var string Path of public key
    * @see https://api.ikoula.com/downloads/Ikoula.API.RSAKeyPub.pem
    */ 
    private static $publicKeyPath = "/path/to/ikoula/public/key/Ikoula.API.RSAKeyPub.pem";
	
    /** Fonction for request Ikoula API
     * @param string $webservice Webservice for data
     * @param string $format JSON or XML
     * @param string $type HTTP Type (GET/POST)
     * @param array $params Params to add for request
     */
     public static function requestApi($webservice, $format, $type, $params = [])
     {
		// Add connexion information
		$params['login'] = self::$email;
		$params['crypted_password'] = base64_encode(self::opensslEncryptPublic(self::$password));
		$params['format'] = $format;

		// Generate signature
		$signature = self::createSignature($params);

		// Add signature for call
		$params['signature'] = $signature;

		// Curl init
		$ch = curl_init();

		if($ch)
		{
			// Create API URI
			$url = self::$urlApi.$webservice;
	
			// Add type request
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);

			// If parameters, we encode his 
			if(is_array($params) && count($params) > 0)
				$params_str = http_build_query($params);

			// If we use post, fix params
			if(strcasecmp("POST", $type) == 0)
			{
				// Fix POST data
				curl_setopt($ch,CURLOPT_POST, true);
				curl_setopt($ch,CURLOPT_POSTFIELDS, $params_str);
			}
			else
				$url .= (strpos($url,'?') === false ? '?' : '&').$params_str;
	
			// Create curl info
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
			curl_setopt($ch, CURLOPT_HEADER, 1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch, CURLOPT_HEADER, false);
	
			// Exec request
			$data = curl_exec($ch);
	
			// Close curl object
			curl_close($ch);
	
			// Return response
			return($data);
		}

		return null;
    }

    /** Create signature with params
     * @param array $params Params to add for request
     * @return string Signature encoded
     */
    private static function createSignature($params = [])
    {
        // Signature to send
        $signature = null;

        // Verify parameters
        if (count($params) > 0)
        {
            // Sort params
            ksort($params);
            
            // Encode params
            $query = http_build_query($params);

            // Encode "plus "+"
            $query = str_replace("+", "%20", $query);

            // Transform in lowercase
            $query = strtolower($query);

            $public_key = "";

            // Verify if key file is present
            if (file_exists(self::$publicKeyPath))
            {
				// Get public key
                $public_key = trim(
                        str_replace(
                                array("\n", '-----BEGIN PUBLIC KEY-----','-----END PUBLIC KEY-----'), 
                                array('', '', ''), 
                                file_get_contents(self::$publicKeyPath)
                            )
                        );
            }

   			// SHA1 hash
            $hash = hash_hmac("SHA1", $query, $public_key, true);

    		// base64 encode
            $signature = base64_encode($hash);
        }

    	return $signature;
	}
	
	/** Fonction for crypt Ikoula password
	 * @param string $password Ikoula account password
	 * @return mixed Ikoula password encrypted, null if error
	 */
	private static function opensslEncryptPublic($password)
	{
		// Verify if key file exist
		if(file_exists(self::$publicKeyPath))
		{
			 // Verify if password is not empty
 			if(!empty($password))
			{
				// Get file content
				$publicKey = openssl_pkey_get_public('file://'.realpath(self::$publicKeyPath));
	
				// If we get file content without error
				if ($publicKey !== FALSE)      
				{
					// Encrypt password
					if(openssl_public_encrypt($password, $crypted, $publicKey) === TRUE)
						return $crypted;
					else
						return NULL;
				}
				else
					return NULL;
			}
			else
				return NULL;
		}
		else
			return NULL;
	}
}

使用例

// Fix JSON header
header("Content-type: application/json");

// Get Exch schema for prestation
echo IkoulaAPI::requestApi("wsexch/schema-subscr", "json", "GET", ['subscr_id' => 999999999]);

// Get Flex VM for Ikoula Account
echo IkoulaAPI::requestApi("wsflex/list", "json", "GET", []);

// Get Flex VM for Ikoula Account
echo IkoulaAPI::requestApi("wsflex/list", "json", "GET", []);

// Get Platform list
echo IkoulaAPI::requestApi("wsplatform/list", "json", "GET", []);

// Get Platform dossiers
echo IkoulaAPI::requestApi("wsplatform/dossiers", "json", "GET", ['platform_id' => 999999999]);

// Get Billing conso for CloudStack prestation
echo IkoulaAPI::requestApi("wscs/conso-for-billing", "json", "GET", ['subscr_id' => '999999999', 'billing_id' => '1']);

// Reboot server
echo IkoulaAPI::requestApi("wsds/reboot-apc-request", "json", "GET", ['server_ip' => 'XXX.XXX.XXX.XXX', 'tempo' => '3']);

增加功能

如果有功能要求,请联系技术支持。