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