source: branches/version-2_13-dev/test/api_test.php @ 22856

Revision 22856, 7.7 KB checked in by Seasoft, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.13.0)

  • 主に空白・空白行の調整。もう少し整えたいが、一旦現状コミット。
Line 
1<?php
2/*
3 * APIの動作確認・検証用プログラム
4 *
5 * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24/**
25 * APIの動作確認・検証用プログラム
26 *  MEMO:本プログラム自体は、EC-CUBE側には一切依存しませんので、クリアな環境でテスト出来る簡易プログラムです。
27 *       初期化や入力チェックを省いている為、display_errorsを強制的にOffにしています。
28 *
29 * @package Test
30 * @author Spirit of Co.,Ltd.
31 * @version $Id$
32 */
33ini_set('display_errors', 'Off');
34if($_REQUEST['EndPoint'] && $_REQUEST['Service'] && $_REQUEST['Operation']) {
35    $url = "{$_REQUEST['EndPoint']}{$_REQUEST['type']}?Service={$_REQUEST['Service']}&Operation={$_REQUEST['Operation']}";
36    for($i =0; $i <10; $i++) {
37        if($_REQUEST['arg_key' . $i] != "") {
38            $url .= '&' . $_REQUEST['arg_key' . $i] . '=' . $_REQUEST['arg_val' . $i];
39        }
40    }
41    if($_REQUEST['mode'] == 'signature') {
42        $arrParam = array();
43        if($_REQUEST['Timestamp'] == '') {
44            $arrParam['Timestamp'] = date('Y-m-d') . 'T' . date('h:i:s') .'Z';
45        }else{
46            $arrParam['Timestamp'] = $_REQUEST['Timestamp'];
47        }
48        $arrParam['AccessKeyId'] = $_REQUEST['AccessKeyId'];
49
50        $arrParam['Service'] = $_REQUEST['Service'];
51        $arrParam['Operation'] = $_REQUEST['Operation'];
52        for($i =0; $i <10; $i++) {
53            if($_REQUEST['arg_key' . $i] != "") {
54                $arrParam[ $_REQUEST['arg_key' . $i] ] = $_REQUEST['arg_val' . $i];
55            }
56        }
57        ksort($arrParam);
58        $check_str = '';
59        foreach($arrParam as $key => $val) {
60            if($val != "") {
61                $check_str .= '&' . str_replace('%7E', '~', rawurlencode($key)) . '=' . str_replace('%7E', '~', rawurlencode($val));
62            }
63        }
64        $check_str = substr($check_str,1);
65        $arrParseUrl = parse_url($_REQUEST['EndPoint'] . $_REQUEST['type']);
66        $check_str = "GET\n" . $arrParseUrl['host'] . "\n" . $arrParseUrl['path'] . "\n" . $check_str;
67        $_REQUEST['Signature'] = base64_encode(hash_hmac('sha256', $check_str, $_REQUEST['SecretKey'], true));
68    }
69    if($_REQUEST['mode'] != 'signature') {
70        if($_REQUEST['Signature'] != "") {
71            $signature = urlencode($_REQUEST['Signature']);
72            $url .= "&AccessKeyId={$_REQUEST['AccessKeyId']}&Timestamp={$_REQUEST['Timestamp']}&Signature={$signature}";
73        }
74        $response = file_get_contents($url);
75    }
76}
77$type = $_REQUEST['type'];
78?>
79<html>
80<head>
81<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
82<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
83<script type="text/javascript">
84function var_dump(obj)
85{
86   var str = '';
87   $.each(obj, function (key, value)
88   {
89            if(typeof value == "object" && value != null) {
90                str += " Key: \"" + key + "\" {\n" + var_dump(value) + "}\n";
91            }else{
92                str += " Key: \"" + key + "\" Type: " + typeof(value) + " Value: \"" + value + "\"";
93            }
94         });
95   return str;
96}
97
98function makeSignature()
99{
100    $('#mode').val("signature");
101    $('#form').submit();
102}
103
104</script>
105</head>
106<body>
107EC-CUBE API TEST<br />
108※このプログラムにはセキュリティ考慮が一切されていませんので取り扱いには注意をして下さい。
109<hr />
110<form action="?" method="POST" id="form">
111<input type="hidden" name="mode" id="mode" value="" />
112EndPoint:<input type="text" name="EndPoint" style="width:400px;" value="<?php echo htmlspecialchars($_REQUEST['EndPoint']); ?>" /><select name="type">
113<option value="json.php" <?php if($type =='json.php'){ echo 'selected';} ?>>json.php</option>
114<option value="xml.php" <?php if($type =='xml.php'){ echo 'selected';} ?>>xml.php</option>
115<option value="php.php" <?php if($type =='php.php'){ echo 'selected';} ?>>php.php</option>
116<option value="index.php" <?php if($type =='index.php'){ echo 'selected';} ?>>index.php</option>
117</select><br />
118Service:<input type="text" name="Service" value="<?php echo htmlspecialchars($_REQUEST['Service']); ?>" /><br />
119Operation:<input type="text" name="Operation" value="<?php echo htmlspecialchars($_REQUEST['Operation']); ?>" /><br />
120<?php
121for ($i = 0; $i < 10; $i++) {
122    echo 'ExtArg[' . $i . ']:<input type="text" name="arg_key' . $i . '" value="' . htmlspecialchars($_REQUEST['arg_key' . $i]) . '" />:'
123            . '<input type="text" name="arg_val' . $i . '" value="' . htmlspecialchars($_REQUEST['arg_val' . $i]) . '" /><br />';
124}
125?>
126AccessKeyId: <input type="text" name="AccessKeyId" value="<?php echo htmlspecialchars($_REQUEST['AccessKeyId']); ?>" />&nbsp;
127SecretKey: <input type="text" name="SecretKey" value="<?php echo htmlspecialchars($_REQUEST['SecretKey']); ?>" />&nbsp;<br />
128<input type="button" value="Signature生成⇒" onclick="makeSignature();" />
129Timestamp: <input type="text" name="Timestamp" value="<?php echo htmlspecialchars($_REQUEST['Timestamp']); ?>" />&nbsp;Signature: <input type="text" name="Signature" id="Signature" value="<?php echo htmlspecialchars($_REQUEST['Signature']); ?>" readonly /><br />
130<?php if($check_str != "") {
131    echo "<pre>{$check_str}</pre><br />";
132} ?>
133<input type="submit" />
134</form>
135<hr />
136REST URI: <a href="<?php echo $url;?>">Link</a><br />
137<textarea rows="1" cols="60"><?php echo htmlspecialchars($url);?></textarea>
138<hr />
139Response:<br />
140<textarea rows="5" cols="100">
141<?php echo htmlspecialchars($response);?>
142</textarea>
143</pre>
144<hr />
145Response decode:<br />
146<pre>
147<?php
148if($type =="json.php") {
149      var_dump(json_decode($response));
150}else if($type=="xml.php") {
151      $xml = simplexml_load_string($response);
152      var_dump($xml);
153      var_dump(libxml_get_errors () );
154}else if($type=="php.php") {
155      var_dump(unserialize($response));
156}
157?>
158</pre>
159<hr />
160<?php if($type=="json.php" && $_REQUEST['Signature'] == "") {?>
161JavaScript:<div id="res"></div>
162<hr />
163<pre id="dump"></pre>
164<hr />
165
166<script type="text/javascript">//<![CDATA[
167    var query_params = {
168        Service: '<?php echo $_REQUEST['Service'];?>',
169        Operation: '<?php echo $_REQUEST['Operation'];?>'
170        <?php
171    for($i =0; $i <10; $i++) {
172        if($_REQUEST['arg_key' . $i] != "") {
173            echo ',' . $_REQUEST['arg_key' . $i] . ': \'' . $_REQUEST['arg_val' . $i] . '\'' . "\n";
174        }
175    }
176        ?>
177        };
178    $(function(){
179        var recvdata = function(data,textstatus) {
180            $('#res').text(textstatus);
181            var str = var_dump(data);
182            $('#dump').text(str);
183        }
184        var recverror = function (result, textstatus, errorThrown)
185        {
186            $('#res').text(textstatus);
187        }
188        $.ajax({
189                type: "GET",
190                url: "<?php echo $_REQUEST['EndPoint'];?>json.php",
191                dataType: 'json',
192                data: query_params,
193                success: recvdata,
194                error: recverror
195                 });
196    });
197//]]></script>
198<?php } ?>
199
200</body>
201</html>
Note: See TracBrowser for help on using the repository browser.