"client_credentials", "client_id" => $client_id, "client_secret" => $client_secret, "scope" => implode(" ", $scopes), ]; $output = postRestUrl($oauth_url, $headers, $post_content); $output_values = json_decode($output); if(!is_object($output_values) || !property_exists($output_values, "access_token")) { return false; } $access_token = $output_values->access_token; file_put_contents($token_file, $access_token); return $access_token; } function getRestUrl($url, $headers, $params = [], $debug = false) { global $rest_last_api_request_nexturl; //echo "called getRestUrl $url\n"; $ctx_options = ["http" => [ "ignore_errors" => true, "method" => "GET", "header" => $headers, ]]; $_url = $url; if(count($params)) { $_url .= "?".http_build_query($params); } if($debug) { var_dump($_url, $ctx_options); //exit; } $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) { 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; } elseif($response_code == "401") { $_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" => [ "ignore_errors" => true, "method" => "POST", "header" => $headers, ]]; if(!$follow_location) { $ctx_options["http"]["follow_location"] = false; } 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); } if($content_type == "application/json") { $ctx_options["http"]["content"] = json_encode($content); } if($debug) { var_dump($url, $ctx_options); exit; } $ctx = stream_context_create($ctx_options); $output = file_get_contents($url, false, $ctx); //var_dump($http_response_header); if(!$follow_location) { return $output; } foreach($http_response_header as $h_line) { $m = []; $status_code_types = "245"; if(!$follow_location) { $status_code_types = "2345"; } if(preg_match('/HTTP\/\d+(?:\.\d+) ([245]\d+)/', $h_line, $m)) { $response_code = $m[1]; if(substr($response_code, 0, 1) == "2") { return $output; } elseif(substr($response_code, 0, 1) == "3") { return $output; } elseif($response_code == "401") { return "__err__401"; } else { die("postRestUrl(): $h_line\n"); } } } } function guidv4($data = null) { // Generate 16 bytes (128 bits) of random data or use the data passed into the function. $data = $data ?? random_bytes(16); assert(strlen($data) == 16); // Set version to 0100 $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // Set bits 6-7 to 10 $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // Output the 36 character UUID. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); }