Zefix_search/zefixAPI.php

168 lines
4.7 KiB
PHP

<?php
include 'env_vars.php';
$username = getenv("username");
$password = getenv("password");
function sendAPICompanyInfoRequest(string $username, string $password, string $uid): string|bool {
// API endpoint
$apiUrl = 'https://www.zefix.admin.ch/ZefixPublicREST/api/v1/company/uid/'.$uid;
// 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;
}
/**
* @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/ZefixREST/api/v1/firm/search.json';
// 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
{
// API endpoint
$apiUrl = 'https://www.zefix.admin.ch/ZefixREST/api/v1/community.json';
// 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 {
$response = sendAPICommunityRequest($username, $password);
$communityArray = json_decode($response, true);
$csvOutput = 'id,bfsId,Kanton,Gemeindename,registryOfCommerceId,replacedById,alternateNames\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/ZefixREST/api/v1/legalForm.json';
// 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;
}
?>