-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
147 lines (106 loc) · 4.45 KB
/
functions.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
138
139
140
141
142
143
144
145
146
147
<?php
$USERNAME = "ApiDemoAccess";
$PASSWORD = "4Dp42lsk83";
$SECURITY_TOKEN = "";
// populates the request-body-example.xml file with the passed form values
function parse_answers() {
// get POST data
$clientTitle = $_POST["client_title"];
$clientFirstName = $_POST["client_firstname"];
$clientSurname = $_POST["client_surname"];
$clientAddressLine1 = $_POST["client_address_line1"];
$clientAddressLine2 = $_POST["client_address_line2"];
$clientAddressTown = $_POST["client_address_town"];
$clientAddressCounty = $_POST["client_address_county"];
$clientEmail = $_POST["client_email"];
$tripStartDate = $_POST["trip_from_date"];
$tripEndDate = $_POST["trip_to_date"];
$tripDestination = $_POST["destination_field"];
$tripPartnerTravelling = $_POST["partner_travelling_field"];
// load demo XML document
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->load("data/request-body.xml");
// personal information & address data
$dom->getElementsByTagName("firstname")->item(0)->nodeValue = $clientFirstName;
$dom->getElementsByTagName("surname")->item(0)->nodeValue = $clientSurname;
$dom->getElementsByTagName("title")->item(0)->nodeValue = $clientTitle;
$dom->getElementsByTagName("addressline1")->item(0)->nodeValue = $clientAddressLine1;
$dom->getElementsByTagName("addressline2")->item(0)->nodeValue = $clientAddressLine2;
$dom->getElementsByTagName("town")->item(0)->nodeValue = $clientAddressTown;
$dom->getElementsByTagName("county")->item(0)->nodeValue = $clientAddressCounty;
// general questions
$questions = $dom->getElementsByTagName('question');
foreach ($questions as $question) {
$questionid = $question->getElementsByTagName('questionid')->item(0)->nodeValue;
$answer = $question->getElementsByTagName('value')->item(0);
switch ($questionid) {
case "ClientEmailField":
$answer->nodeValue = $clientEmail;
break;
case "StartDateField":
$answer->nodeValue = $tripStartDate;
break;
case "EndDateField":
$answer->nodeValue = $tripEndDate;
break;
case "DestinationField":
$answer->nodeValue = $tripDestination;
break;
case "PartnerTravellingField":
$answer->nodeValue = $tripPartnerTravelling;
break;
}
}
$dom->save('data/request-body.xml');
}
function get_authentication_token() {
global $USERNAME;
global $PASSWORD;
global $SECURITY_TOKEN;
$url = 'https://slipstream.schemeserve.com/api/token/' . $USERNAME . '/' . $PASSWORD;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // remove this from production
$output = curl_exec($ch);
if (!$output) {
trigger_error('Curl error: ' . curl_error($ch), E_USER_ERROR);
}
$SECURITY_TOKEN = get_strings_between($output, "<token>", "</token>");
}
// reads the request-body-example.xml file and sends it to the server
function submit_quote() {
global $SECURITY_TOKEN;
$url = 'https://slipstream.schemeserve.com/api/cases?token=' . $SECURITY_TOKEN;
$case = file_get_contents('data/request-body.xml', true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
//curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-type: application/json') );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // remove this from production
curl_setopt($ch, CURLOPT_POSTFIELDS, $case);
$output = curl_exec($ch);
if (!$output) {
echo 'Curl error: ' . curl_error($ch);
return false;
}
}
// HELPERS
function get_strings_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) {
return "";
}
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}