こんにちは、中の人です。
今回は前回引き続き、『AWS Supportをsdk for phpから使ってみた』と題して、aws sdk for phpを使って、AWS Supportにケースの作成・閲覧・返信・クローズを出来るようにします。
複数のアカウントを管理している方やちょっとした問い合わせがしたい時にログインしないといけない手間がなくなるので、一部のユーザーには重宝します。
▼ 前回の記事はこちら
AWS Supportをsdk for phpから使ってみた①
フロントをHTML5で実装し、ajaxでデータを呼び出すようにしています。
今回は第2回としてケースの内容の取得と返信を行います。
- ケースの詳細呼び出しを行います。
aws_describeCommunications.php
なお、ケースIDはケース一覧取得したものをGETで引き渡すものとします。
./aws_describeCommunications.php?case= case-***
1<?php23# credentialファイルの呼び出し45$credential_path = "./.aws/credentials";6if(!file_exists($credential_path)){7 exit("定義ファイルがありません");8}910# aws sdk for phpを呼び出し1112require_once("./vendor/autoload.php");13use Aws\Support\SupportClient;1415# credentialsのどちらかのユーザーを指定します。1617$profile = "user1";1819$client = SupportClient::factory(array(20 'profile' => $profile,21 'region' => 'us-east-1',// サポートはUS eastのみ22));2324if($_GET['case']){25 $case = $_GET['case'];26}else{27 exit();28}2930$client = SupportClient::factory(array(31 'profile' => $profile,32 'region' => 'us-east-1',// サポートはUS east以外NG33));3435$result = $client->DescribeCommunications(array(36 'caseId' => $case,37 'language' => 'ja',38));3940var_dump($result["communications"]);4142exit();設置が完了したら、ブラウザから確認します。
下記のようなデータが表示されていればOKです。
確認後、htmlからの呼び出し用に末尾の行を
1echo json_encode($result["communications"]);と変更します。
1array(7) {2 [0]=>3 array(6) {4 ["__type"]=>5 string(13) "Communication"6 ["attachmentSet"]=>7 array(0) {8 }9 ["body"]=> "ここに質問文"10 ["caseId"]=>11 string(44) "case-*********************"12 ["submittedBy"]=>13 string(19) "Amazon Web Services"14 ["timeCreated"]=>15 string(16) "2014/07/29 ****"16 }17}これでケースの詳細を取得することが出来ました。
- ケースに対して返信をします。
aws_addCommunicationToCase.php
なお、ケースIDおよび投稿内容はajaxからpostで引き渡すものとします。
1<?php23# credentialファイルの呼び出し45$credential_path = "./.aws/credentials";6if(!file_exists($credential_path)){7 exit("定義ファイルがありません");8}910# aws sdk for phpを呼び出し1112require_once("./vendor/autoload.php");13use Aws\Support\SupportClient;1415# credentialsのどちらかのユーザーを指定します。1617$profile = "user1";1819$client = SupportClient::factory(array(20 'profile' => $profile,21 'region' => 'us-east-1',// サポートはUS eastのみ22));2324$case = $_POST['case'];2526if($_POST['body']) $body = $_POST['body'];2728$client = SupportClient::factory(array(29 'profile' => $profile,30 'region' => 'us-east-1',// サポートはUS east以外NG31));3233# メッセージが無い場合はエラー3435if(!$case || !$body){36 echo "no case or no message";37 exit();38}3940$result = $client->addCommunicationToCase(array(41 'caseId' => $case,42 'communicationBody' => $body,43));4445echo json_encode($result);4647exit();投稿側のHTMLについては
aws_addCommunicationToCase.html
にて作成しております。
1<!DOCTYPE html>2<html lang="ja">3<head>4<meta charset="utf-8">5<title>ケースへ返信</title>6<script src="http://code.jquery.com/jquery.min.js"></script>7<script>8jQuery(document).on("click", "#submit", function(){9 var body = $("textarea").val();10 var dataRes = "case=" + ここにケースID + "&body=" + body;11 if(!body){12 alert("ケースへの回答を入力してください。");13 $("textarea").focus();14 return;15 } else if (confirm('ケースに回答します。\nよろしいですか?')) { 16 $.ajax({17 url: 'script/aws_addCommunicationToCase.php',18 type: "POST",19 data: dataRes,20 dataType: 'json',21 success: function( json ) {22 alert("ケースに回答を送りました。");23 },error: function(XMLHttpRequest, textStatus, errorThrown) { 24 $("#XMLHttpRequest").html("XMLHttpRequest : " + XMLHttpRequest.status);25 $("#textStatus").html("textStatus : " + textStatus);26 $("#errorThrown").html("errorThrown : " + errorThrown.message);27 },28 complete : function(dataRes) {29 console.timeEnd("json");30 console.log("jsonデータ取得完了");31 }32 }); 33 }34});35</script>36</head>3738<body> 39<ul class="form">40<li>41<textarea placeholder="ケースに返信"></textarea>42</li>43<li><span id="submit" onclick="">送信</span></li></ul> 44</body>45</html>いかがでしたでしょうか?
HTMLからケースへの返信を入力することで、ケースに対する返信をする事ができます。
次回はケースのクローズと新しいケースを開ける方法を紹介します。
お楽しみに!!