Files
thetool/lib/Snoppapi/Snoppapi.php
Frank Schubert 976844d623 WIP Snopp Order
2026-01-22 17:07:51 +01:00

112 lines
3.2 KiB
PHP

<?php
/**
* Represents the SNOPP API.
*/
class Snoppapi {
private $log;
private $baseurl;
private $apikey;
public function __construct($baseurl, $apikey) {
$this->baseurl = $baseurl;
$this->apikey = $apikey;
$this->log = mfLoghandler::singleton();
}
public function searchAddress(Array $address_data) {
$street = trim($address_data['street']);
$zip = trim($address_data['zip']);
$city = trim($address_data['city']);
$ctx_opts = [
'http' => [
'method' => 'GET',
'ignore_errors' => true,
'header' => [
"Accept: application/json",
"X-Api-Key: {$this->apikey}",
]
]
];
$getHomesEp = $this->baseurl.SNOPP_API_EP_GET_HOMES;
//$url = $getHomesEp."?".$qs;
$url = $getHomesEp;
$ctx = stream_context_create($ctx_opts);
$this->log->debug(__METHOD__.": Getting SNOPP homes: $url");
$response = file_get_contents($url, false, $ctx);
//var_dump($response);exit;
if($response === false) {
$this->log->error("Fehler beim Homes abfragen in SNOPP");
return false;
}
$resp_data = json_decode($response);
$homes = $resp_data->result->homes;
$results = [];
foreach($homes as $home) {
if(trim($home->street) == $street
&& trim($home->zip) == $zip
&& trim($home->city) == $city
) {
$results[] = $home;
}
}
return $results;
}
public function submitOrder(Array $order_data) {
$data = [];
foreach(["oan_id", "execution_date", "product_id", "name", "street", "zip", "city"] as $field) {
if(!array_key_exists($field, $order_data) || !$order_data[$field]) {
$this->log->error(__METHOD__.": Mandatory field '$field' missing");
return false;
}
$data[$field] = $order_data[$field];
}
foreach(["phone", "mobile", "email"] as $field) {
if(array_key_exists($field, $order_data) && $order_data[$field]) {
$data[$field] = $order_data[$field];
}
}
$ctx_opts = [
'http' => [
'method' => 'POST',
'content' => json_encode($data),
'ignore_errors' => true,
'header' => [
"Accept: application/json",
"Content-type: application/json",
"X-Api-Key: {$this->apikey}",
]
]
];
$url = $this->baseurl.SNOPP_API_EP_SUBMIT_ORDER;
$ctx = stream_context_create($ctx_opts);
$this->log->debug(__METHOD__.": Ordering Snopp product: $url\n".print_r($data, true));
$response = file_get_contents($url, false, $ctx);
$this->log->debug(__METHOD__.": ".print_r($response, true));
if($response === false) {
$this->log->error("Fehler beim Bestellen in SNOPP ".print_r($response, true));
return false;
}
$resp_data = json_decode($response);
return $resp_data;
}
}