I want to build a small app in PHP.
For example, I would like to use this to simply query content via the API and output it in HTML. Possibly with its own read-only user.
I would also like to be able to edit content with another user. This should authenticate itself in my app (user/password). This should allow the user to work for a certain period of time. I know that from another CRM and its API, that you then get an access token that you save in a database or a file, for example, and with which you can then work until an expiration time.
Is that basically possible with Cockpit?
I already managed to implement an authentication with PHP and get the users api key:
The last statement to get all items from model “Seiten” doesn’t work. I get a “Permission denied” error.
function build_post_fields($data, $existingKeys = "", &$returnArray = [])
{
if ($data instanceof CURLFile or !(is_array($data) or is_object($data))) {
$returnArray[$existingKeys] = $data;
return $returnArray;
} else {
foreach ($data as $key => $item) {
build_post_fields(
$item,
$existingKeys ? $existingKeys . "[$key]" : $key,
$returnArray
);
}
return $returnArray;
}
}
function callAPI($method, $url, $data)
{
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data) {
curl_setopt(
$curl,
CURLOPT_POSTFIELDS,
build_post_fields($data)
);
}
echo $url . "<br>";
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
break;
default:
if ($data) {
$url = sprintf(
"%s?%s",
$url,
http_build_query(build_post_fields($data))
);
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
echo $url . "<br>";
}
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$headers = [];
#$headers[] = "Api-Key: " . $authToken;
#curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($curl);
if (curl_errno($curl)) {
echo "Error:" . curl_error($curl);
}
if (!$result) {
die("Connection Failure");
}
curl_close($curl);
return json_decode($result);
}
$url = "https://cockpit.local/api/user/auth";
$data = [
"user" => "test",
"password" => "asdasd",
];
$test = callAPI("POST", "https://cockpit.local/api/user/auth", $data);
$data = [
"api-key" => $test->apiKey,
];
print_r(
callAPI("GET", "https://cockpit.local/api/content/items/Seiten", $data)
);