-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjapanpost-tracking.php
More file actions
50 lines (37 loc) · 2.01 KB
/
japanpost-tracking.php
File metadata and controls
50 lines (37 loc) · 2.01 KB
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
46
47
48
49
50
<?php
function getTrackingInfo(string $inquiryNumber)
{
$result = [];
$inquiryNumber = str_replace('-', '', $inquiryNumber);
$trackingPageUrl = sprintf('https://trackings.post.japanpost.jp/services/srv/search/direct?reqCodeNo1=%s&locale=ja', $inquiryNumber);
$trackingPage = file_get_contents($trackingPageUrl);
$trackingPage = preg_replace('/<script type="text\/javascript">.*<\/script>/s', '', $trackingPage);
$trackingPage = preg_replace('/<img [^>]+\/>/s', '', $trackingPage);
$dom = new DOMDocument();
$dom->loadHTML($trackingPage);
$xpath = new DOMXPath($dom);
$result['inquiryNumber'] = $xpath->evaluate('string(//form[@name="srv_searchActionForm"]//table[@summary="配達状況詳細"]/tr[2]/td[1])');
$result['itemType'] = $xpath->evaluate('string(//form[@name="srv_searchActionForm"]//table[@summary="配達状況詳細"]/tr[2]/td[2])');
$historyEntries = $xpath->query('//form[@name="srv_searchActionForm"]//table[@summary="履歴情報"]/tr');
for($i = 2; $i < $historyEntries->length; $i += 2) {
$entry = [];
$entry['zipCode'] = $xpath->evaluate('string(./td[1])', $historyEntries->item($i + 1));;
$node = $historyEntries->item($i);
$entry['date'] = $xpath->evaluate('string(./td[1])', $node);
$entry['action'] = $xpath->evaluate('string(./td[2])', $node);
$entry['detail'] = $xpath->evaluate('string(./td[3])', $node);
$entry['office'] = $xpath->evaluate('string(./td[4])', $node);
$entry['prefecture'] = $xpath->evaluate('string(./td[5])', $node);
$result['history'][] = $entry;
}
$contactEntries = $xpath->query('//form[@name="srv_searchActionForm"]//table[@summary="窓口店"]/tr');
for($i = 1; $i < $contactEntries->length; $i++) {
$entry = [];
$node = $contactEntries->item($i);
$entry['caseClass'] = $xpath->evaluate('string(./td[1])', $node);
$entry['office'] = $xpath->evaluate('string(./td[2]/a[1])', $node);
$entry['phoneNumber'] = $xpath->evaluate('string(./td[3])', $node);
$result['contact'][] = $entry;
}
return $result;
}