-
Notifications
You must be signed in to change notification settings - Fork 6
/
getjson.php
132 lines (99 loc) · 3.32 KB
/
getjson.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
<?php
$config = include("config.php");
$bearerToken = $config["BearerToken"];
if (isset($_POST["tracking"])){
$trackingNum = trim($_POST["tracking"], " ");
} else {
$trackingNum = "";
}
?>
<form action="getjson.php" method="post">
<input type="text" name="tracking" placeholder="tracking number">
<input type="submit" value="Get Package">
</form>
<?php
if($trackingNum) {
echo "Tracking info for: " . $trackingNum . '<br /><br />';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.trackinghive.com/trackings?pageId=1&limit=20");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: Bearer " . $bearerToken
));
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response, false);
foreach ($json->data as $mydata) {
if ($mydata->tracking_number == $trackingNum) {
$packageid = $mydata->_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.trackinghive.com/trackings/". $packageid);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: Bearer " . $bearerToken
));
$response = curl_exec($ch);
curl_close($ch);
$data = $response;
//Declare the custom function for formatting
function pretty_print($json_data)
{
//Initialize variable for adding space
$space = 0;
$flag = false;
//Using <pre> tag to format alignment and font
echo "<pre>";
//loop for iterating the full json data
for($counter=0; $counter<strlen($json_data); $counter++)
{
//Checking ending second and third brackets
if ( $json_data[$counter] == '}' || $json_data[$counter] == ']' )
{
$space--;
echo "\n";
echo str_repeat(' ', ($space*2));
}
//Checking for double quote(“) and comma (,)
if ( $json_data[$counter] == '"' && ($json_data[$counter-1] == ',' ||
$json_data[$counter-2] == ',') )
{
echo "\n";
echo str_repeat(' ', ($space*2));
}
if ( $json_data[$counter] == '"' && !$flag )
{
if ( $json_data[$counter-1] == ':' || $json_data[$counter-2] == ':' )
//Add formatting for question and answer
echo '<span style="color:blue;font-weight:bold">';
else
//Add formatting for answer options
echo '<span style="color:red;">';
}
echo $json_data[$counter];
//Checking conditions for adding closing span tag
if ( $json_data[$counter] == '"' && $flag )
echo '</span>';
if ( $json_data[$counter] == '"' )
$flag = !$flag;
//Checking starting second and third brackets
if ( $json_data[$counter] == '{' || $json_data[$counter] == '[' )
{
$space++;
echo "\n";
echo str_repeat(' ', ($space*2));
}
}
echo "</pre>";
}
//call custom function for formatting json data
echo pretty_print($data);
//print unfiltered json
print_r($data);
}
}
}
?>