Files
FloatBackOfffice/TEST/test_yellorequest.php
dev-chiefworks f76abffdcd first commit
2022-05-31 16:21:53 -04:00

381 lines
12 KiB
PHP

<?php
/*
curl --location --request POST 'https://adminsavvy.sworks.float.sg/TEST/test_yellorequest.php' \
--header 'Authorization: Basic ZmxvYXQ6RmwwYXQh' \
--header 'Content-Type: application/json' \
--data-raw '{
"original" : {
"address" : "10 Amos Road, Sheffield, South Yorkshire, S9 1BX",
"location" : {
"lat": 53.412535,
"lng": -1.419525
}
},
"destination" : {
"address" : "14 Aylesbury Crescent, Sheffield, South Yorkshire, S9 1JR",
"location" : {
"lat": 53.418405,
"lng": -1.419225
}
},
"raw": false
}'
*/
//header('Content-type: application/json');
function isValidJSON($str) {
json_decode($str);
return json_last_error() == JSON_ERROR_NONE;
}
function simpleXML2Array($xml){
$array = (array)$xml;
//recursive Parser
foreach ($array as $key => $value){
if(strpos(get_class($value),"SimpleXML")!==false){
$array[$key] = simpleXML2Array($value);
}
}
return $array;
}
function geocode($address) {
$httpAuthToken = '99dfe35fcb7de1ee';
// Call geocoding service
$data = http_build_query(
array(
'address' => $address,
)
);
$url = "http://10.142.0.4/api/v1/geocode?" . $data;
$opts = array(
'http' => array(
'method' => "GET",
'header' =>
"Content-Type: application/x-www-form-urlencoded\r\n" .
"Accept: application/json\r\n" .
"Authorization: Server-Token ${httpAuthToken}\r\n",
)
);
$context = stream_context_create($opts);
$body = file_get_contents($url, false, $context);
$geocoded = json_decode($body,true);
return $geocoded;
}
/*
$json_params = file_get_contents("php://input");
if (strlen($json_params) <= 0 || !isValidJSON($json_params)) {
http_response_code(400);
echo json_encode([
"code" => 400,
"error" => "Bad request"
]);
exit(0);
}
$request = json_decode($json_params, true);
*/
//$_POST["originalAddress"] = "10 Amos Road, Sheffield, South Yorkshire, S9 1BX";
//$_POST["destinationAddress"] = "14 Aylesbury Crescent, Sheffield, South Yorkshire, S9 1JR";
if (isset($_POST["submit"])) {
$xmlstr = <<<XML
<AgentBidRequest>
<Agent Id="{{agent_id}}">
<Password>{{agent_password}}</Password>
<Reference>AgentRef</Reference>
<Time>{{agent_time}}</Time>
</Agent>
<BidParameters>
<Pricing>
<Currency>USD</Currency>
</Pricing>
<Journey>
<From>
<Type>Address</Type>
<Data>1563 Van Dyke Ave, San Francisco, CA 94124, USA</Data>
<Coordinate>
<Latitude>37.7286068</Latitude>
<Longitude>-122.3919092</Longitude>
</Coordinate>
</From>
<To>
<Type>Address</Type>
<Data>536 Edinburgh St, San Francisco, CA 94112, USA</Data>
<Coordinate>
<Latitude>37.7206606</Latitude>
<Longitude>-122.4326099</Longitude>
</Coordinate>
</To>
</Journey>
<Ride Type="Passenger">
</Ride>
</BidParameters>
</AgentBidRequest>
XML;
$xmlstr = <<<XML
<AgentBookingAvailabilityRequest>
<Agent Id="{{agent_id}}">
<Password>{{agent_password}}</Password>
<Reference>AgentRef</Reference>
<Time>{{agent_time}}</Time>
</Agent>
<Vendor Id="700998008" />
<BookingParameters>
<Pricing>
<Currency>USD</Currency>
</Pricing>
<Journey>
<From>
<Type>Address</Type>
<Data>1563 Van Dyke Ave, San Francisco, CA 94124, USA</Data>
<Coordinate>
<Latitude>37.7286068</Latitude>
<Longitude>-122.3919092</Longitude>
</Coordinate>
</From>
<To>
<Type>Address</Type>
<Data>536 Edinburgh St, San Francisco, CA 94112, USA</Data>
<Coordinate>
<Latitude>37.7206606</Latitude>
<Longitude>-122.4326099</Longitude>
</Coordinate>
</To>
</Journey>
<Ride Type="Passenger"></Ride>
</BookingParameters>
</AgentBookingAvailabilityRequest>
XML;
// $originalAddress = $_POST["originalAddress"];// || "10 Amos Road, Sheffield, South Yorkshire, S9 1BX";
// $originalLat = floatval($_POST["originalLat"]);// || 53.412535;
// $originalLng = floatval($_POST["originalLng"]);// || -1.419525;
// $destinationAddress = $_POST["destinationAddress"];// || "14 Aylesbury Crescent, Sheffield, South Yorkshire, S9 1JR";
// $destinationLat = floatval($_POST["destinationLat"]); // || 53.418405;
// $destinationLng = floatval($_POST["destinationLng"]); // || -1.419225;
$raw = false;
if ($_POST["raw"]) {
$raw = true;
}
$showRequestBody = false;
if ($_POST["showRequest"]) {
$showRequestBody = true;
}
// Check if name has been entered
if (!$_POST['originalAddress']) {
$errOriginalAddress = 'Please enter original address';
} else {
$originalAddress = $_POST["originalAddress"];// || "10 Amos Road, Sheffield, South Yorkshire, S9 1BX";
$original = geocode($originalAddress);
if (isset($original['error'])) {
$errOriginalAddress = $original['error'];
} else {
$originalLat = $original["data"]["lat"];
$originalLng = $original["data"]["lng"];
}
}
if (!$_POST['destinationAddress']) {
$errDestinationAddress = 'Please enter destination address';
} else {
$destinationAddress = $_POST["destinationAddress"];// || "14 Aylesbury Crescent, Sheffield, South Yorkshire, S9 1JR";
$destination = geocode($destinationAddress);
if (isset($destination['error'])) {
$errDestinationAddress = $destination['error'];
} else {
$destinationLat = $destination["data"]["lat"];
$destinationLng = $destination["data"]["lng"];
}
}
if (!$errOriginalAddress && !$errDestinationAddress) {
/* $originalAddress = "10 Amos Road, Sheffield, South Yorkshire, S9 1BX";
$originalLat = 53.412535;
$originalLng = -1.419525;
$destinationAddress = "14 Aylesbury Crescent, Sheffield, South Yorkshire, S9 1JR";
$destinationLat = 53.418405;
$destinationLng = -1.419225;*/
$xml = new SimpleXMLElement($xmlstr);
$xml->Agent->attributes()->Id = "20088"; //300999;
$xml->Agent->Password = "BNyN3ee228MexQ3EzJfzOdb4"; //jEHjE5Kv;
// $xml->Agent->attributes()->Id = "300999";
// $xml->Agent->Password = "jEHjE5Kv";
$xml->Agent->Time = date("Y-m-d\TH:i:s.v\Z");
$xml->BookingParameters->Journey->From->Data = $originalAddress;
$xml->BookingParameters->Journey->From->Coordinate->Latitude = $originalLat;
$xml->BookingParameters->Journey->From->Coordinate->Longitude = $originalLng;
$xml->BookingParameters->Journey->To->Data = $destinationAddress;
$xml->BookingParameters->Journey->To->Coordinate->Latitude = $destinationLat;
$xml->BookingParameters->Journey->To->Coordinate->Longitude = $destinationLng;
$postdata = $xml->asXML();
// $url="https://cxs.autocab.net/api/agent";
$url="https://cxs-staging.autocab.net/api/agent";
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_HTTPHEADER => array(
"Content-Type: text/xml"
),
) );
$response = curl_exec( $curl );
curl_close( $curl );
$xmlResponse = new SimpleXMLElement($response);
$result = [
];
if ((string)$xmlResponse->Result->Success == "true") {
if ($raw) {
$dom = new DOMDocument("1.0");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xmlResponse->asXML());
$result = $dom->saveXML();
$result = simpleXML2Array($xmlResponse);
} else {
$result["offers"] = [];
array_push($result["offers"], [
"price" => (int)$xmlResponse->Pricing->Price,
"estimatedJourney" => [
"distance" => (int) $xmlResponse->EstimatedJourney->Distance,
"duration" => (int) $xmlResponse->EstimatedJourney->Duration,
],
"vendor" => (string) $xmlResponse->VendorDetails->Name
]);
/*
foreach ($xmlResponse->Offers->Offer as $offer) {
array_push($result["offers"], [
"price" => (int)$offer->Pricing->Price,
"estimatedJourney" => [
"distance" => (int) $offer->EstimatedJourney->Distance,
"duration" => (int) $offer->EstimatedJourney->Duration,
],
"vendor" => (string) $offer->VendorDetails->Name
]);
}
*/
}
} else {
$result["error"] = (string) $xmlResponse->Result->FailureReason;
}
$result = PHP_EOL.json_encode($result, JSON_PRETTY_PRINT);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Yellow Cab Offers Test">
<meta name="author" content="float.sg">
<title>Yellow Cab Offers</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<h1 class="page-header text-center">Yellow Cab Unit Test</h1>
<form class="form-horizontal" role="form" method="post" action="test_yellorequest.php">
<div class="form-group">
<label for="originalAddress" class="control-label">Original Address</label>
<div class="">
<input type="text" class="form-control" id="originalAddress" name="originalAddress" placeholder="Original Address" value="<?php echo htmlspecialchars($_POST['originalAddress']); ?>">
<small>Example: 1563 Van Dyke Ave, San Francisco, CA 94124</small>
<?php echo "<p class='text-danger'>$errOriginalAddress</p>";?>
</div>
</div>
<div class="form-group">
<label for="destinationAddress" class="control-label">Destination Address</label>
<div class="">
<input type="text" class="form-control" id="destinationAddress" name="destinationAddress" placeholder="Destination Address" value="<?php echo htmlspecialchars($_POST['destinationAddress']); ?>">
<small>Example: 536 Edinburgh St, San Francisco, CA 94112</small>
<?php echo "<p class='text-danger'>$errDestinationAddress</p>";?>
</div>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="raw" name="raw" value="<?php echo ($raw ? 'Y' : 'N')?>" >
<label class="form-check-label" for="raw">Show Raw Data</label>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="showRequest" name="showRequest" value="<?php echo ($showRequestBody ? 'Y' : 'N')?>" >
<label class="form-check-label" for="showRequest">Show Request Body</label>
</div>
<div class="form-group">
<div class="col-sm-2 col-sm-offset-10">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<?php if ($result) { ?>
<div class="form-group">
<h2>Result:</h2>
<div >
<pre class="prettyprint">
<?php echo $raw ? htmlentities($dom->saveXML()) : htmlentities($result); ?>
</pre>
</div>
</div>
<?php } ?>
<?php if ($showRequestBody) { ?>
<div class="form-group">
<h2>Request:</h2>
<div>
<pre class="prettyprint">
<?php echo htmlentities($postdata); ?>
</pre>
</div>
</div>
<?php } ?>
</form>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
</body>
</html>