-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
137 lines (110 loc) · 3.26 KB
/
index.php
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
require 'vendor/autoload.php';
use DOMWrap\Document;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
# Input Data
$awb_code = 235;
$awb_number = 51737512;
# CURL query
$ch = curl_init("https://www.turkishcargo.com.tr/en/online-services/shipment-tracking?quick=True&awbInput={$awb_code}-{$awb_number}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
// $page = file_get_contents("page-example.html");
# Create object from HTML
$doc = new Document();
$doc->html($page);
# Create spreadsheet
$spreadsheet = new Spreadsheet();
/**
* Filling spreadsheets
*/
# Table #1
$table = $doc->find('.shipment-tracking')->eq(0)->find('table');
$spreadsheet->setActiveSheetIndex(0);
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Table 1');
$data = $table->find('tr');
# Fill data
$index = 1;
foreach ($data as $node => $value) {
$cells = $value->find('td');
$title = $cells[0]->textContent;
$value = $cells[1]->textContent;
$sheet->setCellValue('A' . $index, $title);
$sheet->setCellValue('B' . $index, $value);
$index++;
};
# Table #2
$table = $doc->find('.shipment-tracking')->eq(1)->find('table');
$spreadsheet->createSheet();
$spreadsheet->setActiveSheetIndex(1);
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Table 2');
$headings = $table->find('th');
$data = $table->find('tr');
# Fill headings
$index = 1;
foreach ($headings as $node) {
$title = $node->textContent;
$letter = Coordinate::stringFromColumnIndex($index);
$sheet->setCellValue($letter . '1', $title);
$index++;
};
# Fill data
$index = 1;
foreach ($data as $node => $value) {
$cells = $value->find('td');
$indexCell = 1;
foreach ($cells as $cell) {
$letter = Coordinate::stringFromColumnIndex($indexCell);
# Test for checked value
# In this case, it doesn't work because "checked" attribute
# and class "active" assigned by javascript
if (count($cell->find('label'))) {
if ($cell->find('label')->eq(0)->hasClass('active')) {
$value = '+';
} else {
$value = '-';
}
} else {
$value = $cell->textContent;
}
$sheet->setCellValue($letter . $index, $value);
$indexCell++;
}
$index++;
};
# Table #3
$table = $doc->find('.shipment-tracking')->eq(3)->find('table');
$spreadsheet->createSheet();
$spreadsheet->setActiveSheetIndex(2);
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Table 3');
$headings = $table->find('th');
$data = $table->find('tr');
# Fill headings
$index = 1;
foreach ($headings as $node) {
$title = $node->textContent;
$letter = Coordinate::stringFromColumnIndex($index);
$sheet->setCellValue($letter . '1', $title);
$index++;
};
# Fill data
$index = 1;
foreach ($data as $node => $value) {
$cells = $value->find('td');
$indexCell = 1;
foreach ($cells as $cell) {
$letter = Coordinate::stringFromColumnIndex($indexCell);
$value = $cell->textContent;
$sheet->setCellValue($letter . $index, $value);
$indexCell++;
}
$index++;
};
# Write and save file
$writer = new Xlsx($spreadsheet);
$writer->save('output.xlsx');