ver.1サポートサイト


マイエリア(Perl)

最終更新日:2023年03月08日

ソース

#/usr/bin/perl
use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use JSON;
use Digest::HMAC_SHA1 qw(hmac_sha1 hmac_sha1_hex);
use Unicode::Escape;
use strict;
my $locator_url = 'https://www.pi-pe.co.jp/api/locator';
my $spiral_api_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
my $spiral_api_token_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
my $ua = LWP::UserAgent->new;

# ロケータAPIの呼び出し
print "Calling Locator API...\n";
my $req = HTTP::Request->new(POST => $locator_url);
$req->content_type('application/json; charset=UTF-8');
$req->header('X-SPIRAL-API' => 'locator/apiserver/request');
my $reqdata = "{'spiral_api_token':'$spiral_api_token'}";
print "=== post data ===\n$reqdata\n";
$req->content($reqdata);
my $response = $ua->request($req);
my $json_obj = from_json($response->content);
print "=== response data ===\n" . $response->content . "\n\n";
my $location = $json_obj->{'location'};

# マイエリアへのログインとセッションIDの取得
my $passkey = time();
my $signee = "$spiral_api_token&$passkey";
my $signature = hmac_sha1_hex($signee, $spiral_api_token_secret);
print "My area authentification API\n";
$req = HTTP::Request->new(POST => $location);
$req->content_type('application/json; charset=UTF-8');
$req->header('X-SPIRAL-API' => 'area/login/request');
$reqdata = "{'spiral_api_token':'$spiral_api_token','passkey':'$passkey','signature':'$signature','my_area_title':'api_test','id':'***********','password':'***********','url_type':'2'}";
print "=== post data ===\n$reqdata\n";
$req->content($reqdata);
$response = $ua->request($req);
print "=== response data ===\n" . $response->content . "\n\n";
$json_obj = from_json($response->content);
my $jsessionid = $json_obj->{'jsessionid'};

# マイページ上の一覧表データの読み出し
$passkey = time();
$signee = "$spiral_api_token&$passkey";
$signature = hmac_sha1_hex($signee, $spiral_api_token_secret);
print "Getting table data on my area\n";
$req = HTTP::Request->new(POST => $location);
$req->content_type('application/json; charset=UTF-8');
$req->header('X-SPIRAL-API' => 'table/data/request');
$reqdata = "{'spiral_api_token':'$spiral_api_token','passkey':'$passkey','signature':'$signature','jsessionid':'$jsessionid','my_area_title':'api_test','search_title':'search1','lines_per_page':'20'}";
print "=== post data ===\n$reqdata\n";
$req->content($reqdata);
$response = $ua->request($req);
print "=== response data ===\n" . $response->content . "\n\n";
$json_obj = from_json($response->content);
my @dt = @{$json_obj->{'data'}};
foreach my $d (@dt) {
    my @dd = @{$d};
    foreach my $de (@dd) {
        print "$de\t";
    }
    print "\n";
}
print "count = " . $json_obj->{'count'} . "\n";

実行結果

Calling Locator API...
=== post data ===
{'spiral_api_token':'XXXXXXXXXXXXXXXXXXXXXXXXXXX'}
=== response data ===
{"message":"OK","supported_version":["2"],"location":"https://www.pi-pe.co.jp/api/service",
"default_version":"2", "use_client_crt":"f","code":"0"}

My area authentification API
=== post data ===
{'spiral_api_token':'XXXXXXXXXXXXXXXXXXXXXXXXXXX','passkey':'1366801315','signature':'000000004e5577c1f89ffd1bf20bb49axxxxxxxx','my_area_title':'api_test','id':'***********','password':'***********'}
=== response data ===
{"message":"OK","jsessionid":"0000000000ECBD395B8E9753xxxxxxxx","code":"0","url":"https://www.pi-pe.co.jp/area/servlet/area.MyPageBundle?MyPageID=xxxx422a_mdk0nflfq6l1maxxxx"}

Getting table data on my area
=== post data ===
{'spiral_api_token':'XXXXXXXXXXXXXXXXXXXXXXXXXXX','passkey':'1366801315','signature':'000000004e5577c1f89ffd1bf20bb49axxxxxxxx','jsessionid':'0000000000ECBD395B8E9753xxxxxxxx','my_area_title':'api_test','search_title':'search1','lines_per_page':'20'}
=== response data ===
{"message":"OK","count":"3","data":[["1","product_a","商品
A","15",],["2","product_b","商品B","0",], ["3","product_c","商品
C","5",""]], "jsessionid":"0000000000ECBD395B8E9753xxxxxxxx", "code":"0",
"header":["id","商品ID","商品名","現在在庫数",""]}

1 product_a 商品A 15
2 product_b 商品B 0
3 product_c 商品C 5
count = 3