<?php
/**
* API Endpoint Error Checker (No Async)
*
* This script checks API endpoints for errors and outputs them.
* It does not use asynchronous logic and processes requests sequentially.
*/
// Define the API endpoint URLs to check
$endpoints = [
'https://api.example.com/endpoint1',
'https://api.example.com/endpoint2',
'https://api.example.com/endpoint3',
];
// Function to check an API endpoint
function checkEndpoint($url) {
try {
$response = file_get_contents($url); // Simple HTTP request
if ($response === false) {
return "Error: Could not connect to $url";
}
$status_code = (int)substr($response, 0, 3); // Extract status code from response
$body = $response; // Use the entire response as the body
if ($status_code >= 400) {
// Check for specific error messages in the response body
$error_message = error_log_to_string($body); // Helper function
return "Error: HTTP Status Code $status_code - $error_message";
} else {
return "Success: HTTP Status Code $status_code";
}
} catch (Exception $e) {
return "Error: Exception - " . $e->getMessage();
}
}
/**
* Helper function to extract error message from a response body
*
* @param string $body The response body.
* @return string The error message.
*/
function error_log_to_string($body) {
//Basic error checking, can be improved with regex
if (strpos($body, 'error') !== false) {
return trim(substr($body, strpos($body, 'error')));
}
return $body;
}
// Loop through the endpoints and check them
foreach ($endpoints as $endpoint) {
$error = checkEndpoint($endpoint);
echo "Endpoint: $endpoint\n";
echo $error . "\n\n";
}
?>
Add your comment