Zefix_search/zefixAPI.php

142 lines
3.8 KiB
PHP
Raw Normal View History

2023-08-21 20:37:59 +02:00
<?php
2023-08-21 21:16:49 +02:00
include 'api/env_vars.php';
2023-08-21 20:37:59 +02:00
$username = getenv("username");
$password = getenv("password");
2023-08-21 21:12:21 +02:00
2023-08-21 20:37:59 +02:00
/**
* @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);
return $response;
}
function sendAPICommunityRequest(string $username, string $password): string|bool
{
2023-08-21 21:19:31 +02:00
echo $username;
echo $password;
2023-08-21 20:37:59 +02:00
// 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);
return $response;
}
function communityCSV(string $username, string $password): string {
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);
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;
}
?>