OFAA OAID API v2

This commit is contained in:
Frank Schubert
2026-01-07 14:36:00 +01:00
parent 2912da08ba
commit 8cebb77bf4
5 changed files with 57 additions and 21 deletions

View File

@@ -1,4 +1,6 @@
<?php
<?php
$rest_last_api_request_nexturl = "";
function fetchAccessToken($oauth_url, $client_id, $client_secret, $scopes = ["oaid:codes:read"]) {
//echo "called fetchAccessToken $oauth_url\n";
@@ -30,6 +32,8 @@ function fetchAccessToken($oauth_url, $client_id, $client_secret, $scopes = ["oa
}
function getRestUrl($url, $headers, $params = [], $debug = false) {
global $rest_last_api_request_nexturl;
//echo "called getRestUrl $url\n";
$ctx_options = ["http" => [
"ignore_errors" => true,
@@ -49,26 +53,45 @@ function getRestUrl($url, $headers, $params = [], $debug = false) {
$ctx = stream_context_create($ctx_options);
$output = file_get_contents($_url, false, $ctx);
if($debug) {
echo $output;
}
$_return = "";
$http_status_line = "";
foreach($http_response_header as $h_line) {
$m = [];
if($debug) {
echo "HEADER: " . trim($h_line) . "\n";
}
if(preg_match('/HTTP\/\d+(?:\.\d+) ([245]\d+)/', $h_line, $m)) {
$http_status_line = $h_line;
$response_code = $m[1];
if(substr($response_code, 0, 1) == "2") {
return $output;
$_return = $output;
} elseif($response_code == "401") {
return "__err__401";
} else {
die("getRestUrl():\n$h_line\n$output");
$_return = "__err__401";
}
}
if(preg_match('/^Link: .*<([^>]+)>; rel=next/', $h_line, $m)) {
$rest_last_api_request_nexturl = $m[1];
}
}
if(!$_return) die("getRestUrl():\n$http_status_line\n$output");
return $_return;
}
function putRestUrl($url, $headers, $content, $content_type = "application/x-www-form-urlencoded", $follow_location = true, $debug = false) {
return postRestUrl($url, $headers, $content, $content_type, $follow_location, $debug, 'PUT');
}
function patchRestUrl($url, $headers, $content, $content_type = "application/x-www-form-urlencoded", $follow_location = true, $debug = false) {
return postRestUrl($url, $headers, $content, $content_type, $follow_location, $debug, 'PATCH');
}
function postRestUrl($url, $headers, $content, $content_type = "application/x-www-form-urlencoded", $follow_location = true, $debug = false, $method = "POST") {
//echo "called postResturl $url\n";
$ctx_options = ["http" => [
@@ -84,6 +107,9 @@ function postRestUrl($url, $headers, $content, $content_type = "application/x-ww
if($method == "PUT") {
$ctx_options["http"]["method"] = "PUT";
}
if($method == "PATCH") {
$ctx_options["http"]["method"] = "PATCH";
}
if($content_type == "application/x-www-form-urlencoded") {
$ctx_options["http"]["content"] = http_build_query($content);