Zefix_search/api/zefixAPI.php

147 lines
3.9 KiB
PHP
Raw Normal View History

2023-08-21 20:37:59 +02:00
<?php
include 'env_vars.php';
$username = getenv("username");
$password = getenv("password");
/**
* @param string $
* @param string $
* @param array $data
* @return bool|string
*/
function sendAPICompanySearchRequest(string $username, string $password, array $data): string|bool
{
// API endpoint
$apiUrl = 'https://www.zefix.admin.ch/ZefixPublicREST/api/v1/company/search';
// Headers
$headers = array(
"accept: application/json",
"Content-Type: application/json"
);
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification
// Execute cURL session and get the response
$response = curl_exec($ch);
// Check for cURL errors
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
// Close cURL session
curl_close($ch);
// Close cURL
curl_close($ch);
return $response;
}
function sendAPICommunityRequest(string $username, string $password): string|bool
{
// API endpoint
$apiUrl = 'https://www.zefix.admin.ch/ZefixPublicREST/api/v1/community';
// Headers
$headers = array(
"accept: application/json",
"Content-Type: application/json"
);
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification
// Execute cURL session and get the response
$response = curl_exec($ch);
// Check for cURL errors
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
// Close cURL session
curl_close($ch);
// Close cURL
curl_close($ch);
return $response;
}
function communityCSV(string $username, string $password): string {
2023-08-21 20:48:36 +02:00
echo "$response";
2023-08-21 20:49:28 +02:00
$response = sendAPICommunityRequest($username, $password);
2023-08-21 20:37:59 +02:00
$communityArray = json_decode($response, true);
$csvOutput = 'bfsId,Kanton,Gemeindename,registryOfCommerceId\n';
// Create CSV rows
foreach ($communityArray as $item) {
$csvOutput .= implode(',', $item) . "\n";
}
return $csvOutput;
}
function sendAPILegalFormRequest(string $username, string $password): string|bool
{
// API endpoint
$apiUrl = 'https://www.zefix.admin.ch/ZefixPublicREST/api/v1/legalForm';
// Headers
$headers = array(
"accept: application/json",
"Content-Type: application/json"
);
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification
// Execute cURL session and get the response
$response = curl_exec($ch);
// Check for cURL errors
if (curl_errno($ch)) {
echo 'Curl error: ' . curl_error($ch);
}
// Close cURL session
curl_close($ch);
// Close cURL
curl_close($ch);
return $response;
}
function legalFormCSV(string $username, string $password): string {
$response = sendAPILegalFormRequest($username, $password);
$legalformArray = json_decode($response, true);
$csvOutput = 'id,name\n';
// Create CSV rows
foreach ($legalformArray as $item) {
$csvOutput .= $item["id"].",".$item["name"]["de"] . "\n";
}
return $csvOutput;
}
?>