Advertisement
Guest User

Click to Edit Title

a guest
Feb 14th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. <?php
  2. $akey='api key';
  3. $asecret='secret key';
  4. // 各種設定ここから
  5. define("config_callurlmethod", "curl"); // APIを呼び出す方法を指定。現在はcurlを指定した場合はcurlを使用し、それ以外が指定された場合はfile_get_contentsを使用
  6. define("config_cloudstack_api_baseurl", "https://compute.jp-west.idcfcloud.com"); // baseURLを設定
  7. define("config_cloudstack_api_apipath", "/client/api?"); // APIパスを設定(デフォルトでは/client/api?)
  8. define("config_cloudstack_api_key", $akey); // APIキーを設定
  9. define("config_cloudstack_api_secret", $asecret); // APIシークレットを設定
  10. // 各種設定ここまで foreach($_GET as $key => $val){
  11. $command["{$key}"] = $val; // URL引数をパース
  12. }
  13. if(isset($_GET['tags'])){
  14. $tval = $_GET['tags'];
  15. $tags = '{"key":"cloud-description","value":"'.$tval.'"}';
  16. $command["tags"] = json_decode($tags);
  17. }
  18. print_r($command);
  19. CloudStackAPICall($command); // API呼び出し関数を実行
  20.  
  21. // API呼び出し関数…引数:実行したいAPIのコマンドやパラメータを連想配列に格納したもの
  22. function CloudStackAPICall($command){
  23. switch(config_callurlmethod){ // APIを呼び出す方法に応じて処理を切り替え
  24. case "curl": // curlが指定された場合
  25. $ch = curl_init(); // curlを初期化
  26. curl_setopt($ch, CURLOPT_URL, CloudStackAPICreateURL($command)); // APIを呼び出すURLを作成する関数を実行し、結果をcurlで取得するURLに設定
  27. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // curlのオプション設定
  28. $result = curl_exec($ch); // curlを実行し、結果を取得
  29. curl_close(); // curlを閉じる
  30. break;
  31.  
  32. default: // 何も指定されなかった場合
  33. $result = file_get_contents(CloudStackAPICreateURL($command)); // APIを呼び出すURLを作成する関数を実行し、結果をfile_get_contentsで取得
  34. break;
  35. }
  36. if(strcmp($command["response"], "json") == 0){ // 結果の受け取りにJSON形式が指定されている場合
  37. header('Content-Type: application/json; charset=utf-8'); // JSON用のヘッダを出力
  38. }
  39. else{ // 結果の受け取り形式が指定されていない場合
  40. header('Content-type: application/xml; charset=UTF-8'); // XML用のヘッダを出力
  41. }
  42. echo $result; // 結果を表示
  43. }
  44.  
  45. // CloudStackのAPIを呼び出すURLを作成する関数…引数:$command=実行したいAPIのコマンドやパラメータを連想配列に格納したもの、(以降は省略可能であり、省略した場合は設定から読み込む)$baseurl=baseURL、$api_key=CloudStackのAPIキー、$api_secret=CloudStackのAPIシークレット
  46. function CloudStackAPICreateURL($command, $baseurl = config_cloudstack_api_baseurl, $apipath = config_cloudstack_api_apipath, $api_key = config_cloudstack_api_key, $api_secret = config_cloudstack_api_secret){
  47. $callurl = "";
  48. foreach($command as $key => $val){ // 実行したいAPIのコマンドやパラメータを走査し、整形
  49. if(strcmp($key, "apiKey") != 0){ // APIキーは最後に追記するため無視
  50. if(strcmp($callurl, "") != 0) $callurl .= "
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement