Created
December 27, 2017 08:43
-
-
Save aynm142/67c3fa070de5d13d7bdf56cd0b0728b7 to your computer and use it in GitHub Desktop.
class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services\Hotelbeds\Hotel\Search; | |
use Amadeus\Client; | |
use Amadeus\Client\Params; | |
use Amadeus\Client\RequestOptions\Fare\MPDate; | |
use Amadeus\Client\RequestOptions\Fare\MPItinerary; | |
use Amadeus\Client\RequestOptions\Fare\MPLocation; | |
use Amadeus\Client\RequestOptions\Fare\MPPassenger; | |
use Amadeus\Client\RequestOptions\FareMasterPricerTbSearch; | |
use App\Contracts\Services\Hotel\HotelbedsServiceInterface; | |
use Carbon\Carbon; | |
use DateTime; | |
use hotelbeds\hotel_api_sdk\helpers\Availability; | |
use Mockery\Exception; | |
use hotelbeds\hotel_api_sdk\HotelApiClient; | |
use hotelbeds\hotel_api_sdk\model\Destination; | |
use hotelbeds\hotel_api_sdk\model\Occupancy; | |
use hotelbeds\hotel_api_sdk\model\Pax; | |
use hotelbeds\hotel_api_sdk\model\Rate; | |
use hotelbeds\hotel_api_sdk\model\Stay; | |
use hotelbeds\hotel_api_sdk\types\ApiVersion; | |
use hotelbeds\hotel_api_sdk\types\ApiVersions; | |
use hotelbeds\hotel_api_sdk\messages\AvailabilityRS; | |
class HotelbedsService implements HotelbedsServiceInterface | |
{ | |
const ENDPOINT_URL = "https://api.test.hotelbeds.com/hotel-content-api/1.0/hotels?fields=all&from={from}&to={to}"; | |
/** @var Client */ | |
private $endpointStatus = "https://api.test.hotelbeds.com/hotel-api/1.0/status"; | |
private $endpointContent = self::ENDPOINT_URL; | |
private $url = "https://api.test.hotelbeds.com"; | |
private $apiKeyActivities = "6mze84qrztsrqqzacubnk7qk"; | |
private $secretKeyActivities = "nDQ4cNq9qA"; | |
private $apiKeyHotels = '4hegmwg2ufwjj9mr5btqy5en'; | |
private $secretKeyHotels = "S4uRMXcQgY"; | |
private $signatureActivities; | |
private $signatureHotels; | |
public function __construct() | |
{ | |
$this->signatureActivities = hash("sha256", $this->apiKeyActivities . $this->secretKeyActivities . time()); | |
$this->signatureHotels = hash("sha256", $this->apiKeyHotels . $this->secretKeyHotels . time()); | |
} | |
public function __clone() | |
{ | |
$this->signatureHotels = hash("sha256", $this->apiKeyHotels . $this->secretKeyHotels . time()); | |
$this->endpointContent = self::ENDPOINT_URL; | |
} | |
public function checkStatus() | |
{ | |
try { | |
// Get cURL resource | |
$curl = curl_init(); | |
// Set options | |
curl_setopt_array($curl, [ | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_URL => $this->endpointStatus, | |
CURLOPT_HTTPHEADER => [ | |
'Accept:application/json', | |
'Api-key:' . $this->apiKeyActivities . '', | |
'X-Signature:' . $this->signatureActivities . '' | |
], | |
]); | |
// Send the request & save response to var | |
$response = curl_exec($curl); | |
// Check HTTP status code | |
if (!curl_error($curl)) { | |
switch ($http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE)) { | |
case 200: #OK | |
echo "Server JSON Response:" . $response; | |
break; | |
default: | |
echo "Unexpected HTTP code ", $http_code, "\n"; | |
echo $response; | |
} | |
} | |
// Close request | |
curl_close($curl); | |
} catch (Exception $ex) { | |
printf("Error while sending request, reason: %s\n", $ex->getMessage()); | |
} | |
} | |
public function pars(array $searchParams) | |
{ | |
$this->endpointContent = str_replace("{from}", $searchParams["from"], $this->endpointContent); | |
$this->endpointContent = str_replace("{to}", $searchParams["to"], $this->endpointContent); | |
file_put_contents(storage_path("linklog.txt"), "$this->endpointContent \n", FILE_APPEND); | |
try { | |
$curl = curl_init(); | |
curl_setopt_array($curl, [ | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_URL => $this->endpointContent, | |
CURLINFO_HEADER_OUT => 1, | |
CURLOPT_HTTPHEADER => [ | |
'Accept: application/json', | |
'Api-key:' . $this->apiKeyHotels . '', | |
'X-Signature:' . $this->signatureHotels . '' | |
], | |
]); | |
$response = curl_exec($curl); | |
$response = json_decode($response, true); | |
if (!curl_error($curl)) { | |
switch ($http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE)) { | |
case 200: #OK | |
if (isset($response["hotels"])) { | |
return $response; | |
} else { | |
$this->pars($searchParams); | |
} | |
default: | |
$this->pars($searchParams); | |
} | |
} | |
} catch (Exception $ex) { | |
printf("Error while sending request, reason: %s\n", $ex->getMessage()); | |
die(); | |
} | |
} | |
public function getAvail() | |
{ | |
$apiClient = new HotelApiClient($this->url, | |
$this->apiKeyHotels, | |
$this->secretKeyHotels, | |
new ApiVersion(ApiVersions::V1_0), | |
120); | |
$rqData = new Availability(); | |
$rqData->stay = new Stay(DateTime::createFromFormat("Y-m-d", "2018-02-01"), | |
DateTime::createFromFormat("Y-m-d", "2018-02-10")); | |
$rqData->destination = new Destination("PAR"); | |
$occupancy = new Occupancy(); | |
$occupancy->adults = 2; | |
$occupancy->children = 1; | |
$occupancy->rooms = 1; | |
$occupancy->paxes = [ | |
new Pax(Pax::AD, 30, "Mike", "Doe"), | |
new Pax(Pax::AD, 27, "Jane", "Doe"), | |
new Pax(Pax::CH, 8, "Mack", "Doe") | |
]; | |
$rqData->occupancies = [$occupancy]; | |
$availRS = $apiClient->Availability($rqData); | |
dd($availRS); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment